Raised This Month: $51 Target: $400
 12% 

Some vector maths help!


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Remy Lebeau
Senior Member
Join Date: Dec 2009
Location: Sydney, Australia
Old 10-11-2013 , 00:43   Some vector maths help!
Reply With Quote #1

I have some code for pulling a player (besttarget) toward another player (client).

Code:
		new Float:pos1[3];
		new Float:pos2[3];
		
		GetClientAbsOrigin( client, pos1 );
		GetClientAbsOrigin( besttarget, pos2 );
		
		new Float:localvector[3];
		
		localvector[0] = pos1[0] - pos2[0];
		localvector[1] = pos1[1] - pos2[1];
		localvector[2] = pos1[2] - pos2[2];

		new Float:velocity1[3];
		new Float:velocity2[3];
		
		velocity1[0] += 0;
		velocity1[1] += 0;
		velocity1[2] += 300;
		
		velocity2[0] = localvector[0] * ( 100 * GravForce[ult_level] );
		velocity2[1] = localvector[1] * ( 100 * GravForce[ult_level] );
		velocity2[2] = localvector[2] * ( 100 * GravForce[ult_level] );
		
		SetEntDataVector( besttarget, m_vecBaseVelocity, velocity1, true );
		SetEntDataVector( besttarget, m_vecBaseVelocity, velocity2, true );
I want to change this so that it pushes the player (besttarget) in the opposite direction (ie, away from client).

I'm not really competent at working out the vector stuff, can someone help me work out what I need to do?
Remy Lebeau is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 10-11-2013 , 04:15   Re: Some vector maths help!
Reply With Quote #2

Are the players on the ground, above each other?
If you apply velocity to a player and they are on the ground, but the force is directed toward the ground, they won't move. What you would need to do is apply enough force to push them toward the other player, upward off the ground, toward the player. This is the most likey case.. Generally just use eye position on the pulling player, and abs origin on the client being pulled. You may even want to add some extra upward velocity to the player being pulled to get them off the ground.

There are then 2 ways to do it, though I think the simplest is to just get the 2 points, make vector from points, and kind of slingshot the person from point a to point b with some linear regression amount of force.. Apply more the farther away the payer is.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
Bimbo1
Senior Member
Join Date: Jan 2010
Location: brazil
Old 10-11-2013 , 12:49   Re: Some vector maths help!
Reply With Quote #3

invert the vector direction:
Code:
		localvector[0] = pos1[0] - pos2[0];
		localvector[1] = pos1[1] - pos2[1];
		localvector[2] = pos1[2] - pos2[2];
Code:
		localvector[0] = pos2[0] - pos1[0];
		localvector[1] = pos2[1] - pos1[1];
		localvector[2] = pos2[2] - pos1[2];
or change the gravforce values to positive values. if you do so, then you shouldn't invert the vector.
normalize the velocity vector [@edit]and add the velocity1 to velocity2, otherwise it will not work properly. moreover, m_vecBaseVelocity should be quoted.
Code:
		new Float:velocity2[3];

		new Float:a = localvector[0]*localvector[0]+localvector[1]*localvector[1]+localvector[2]*localvector[2];
		if(a != 0.0){

			new Float:e = 1.0;
			new Float:norma = a;
			while(e > 0.001){ //do you need higher precision than this? i think 0.001 is fine

				new Float:newnorma = (norma+a/norma)/2.0;
				e = norma - newnorma;
				norma = newnorma;

			}

			velocity2[0] = localvector[0] * ( 100 * GravForce[ult_level] )/norma;
			velocity2[1] = localvector[1] * ( 100 * GravForce[ult_level] )/norma;
			velocity2[2] = 300.0 + localvector[2] * ( 100 * GravForce[ult_level] )/norma;
			
			SetEntDataVector( besttarget, "m_vecBaseVelocity", velocity2, true );

		}
also, i think it's better to either set the localvelocity or add that vector to the basevelocity. setting the basevelocity might not be good in some cases.
__________________
sie sind das essen und wir sind die jäger!

Last edited by Bimbo1; 10-11-2013 at 13:43.
Bimbo1 is offline
Remy Lebeau
Senior Member
Join Date: Dec 2009
Location: Sydney, Australia
Old 10-11-2013 , 17:31   Re: Some vector maths help!
Reply With Quote #4

Thanks for the helpful replies, you've given me some things to test with. Appreciate the help!
Remy Lebeau is offline
blodia
Veteran Member
Join Date: Sep 2009
Location: UK
Old 10-11-2013 , 18:56   Re: Some vector maths help!
Reply With Quote #5

Quote:
Originally Posted by Remy Lebeau View Post
Code:
velocity1[0] += 0;
velocity1[1] += 0;
...
blodia is offline
Bimbo1
Senior Member
Join Date: Jan 2010
Location: brazil
Old 10-11-2013 , 20:37   Re: Some vector maths help!
Reply With Quote #6

lol, haha, and that's not the sole laughable part of his/her code, master blodia. i mean, that's, at least, harmless. setting the velocity and, then, overwriting the same vector again is just what the fudge.
__________________
sie sind das essen und wir sind die jäger!
Bimbo1 is offline
blodia
Veteran Member
Join Date: Sep 2009
Location: UK
Old 10-12-2013 , 18:29   Re: Some vector maths help!
Reply With Quote #7

its not about being laughable young padawan, after all this section of the forum is for those who need help and may not be great at coding. vector maths, fair enough it can be difficult (btw check out the vector stuff). not good at maths in general? fair enough BUT adding 0 to any value is as basic as maths can get ...
blodia is offline
Bimbo1
Senior Member
Join Date: Jan 2010
Location: brazil
Old 10-12-2013 , 20:32   Re: Some vector maths help!
Reply With Quote #8

ok, mighty jedi. that vector stuff would make it way simpler. as the same would be written as:
Code:
		new Float:pos1[3];
		new Float:pos2[3];
		
		GetClientAbsOrigin( client, pos1 );
		GetClientAbsOrigin( besttarget, pos2 );

		new Float:velocity[3];

		MakeVectorFromPoints(pos1, pos2, velocity);
		NormalizeVector(velocity, velocity);
		ScaleVector(velocity, 100.0 * GravForce[ult_level]);
		velocity[2] += 300.0;

		SetEntDataVector( besttarget, "m_vecBaseVelocity", velocity, true );
and, remy labeau, i didn't mean to offend you by saying your code is laughable. if i did offend you, sorry.
__________________
sie sind das essen und wir sind die jäger!
Bimbo1 is offline
Remy Lebeau
Senior Member
Join Date: Dec 2009
Location: Sydney, Australia
Old 10-13-2013 , 19:30   Re: Some vector maths help!
Reply With Quote #9

No offense taken - this is someone else's code I'm starting with

As for adding 0 - while I agree that it is pointless, it does at least serve to make it explicit that that particular vector is used solely to give the user some vertical speed (which you highlighted as a potential issue!)

Edit: So this code ^^ is what you are proposing as code that should PUSH someone directly away from you? I'll test it out. Thanks heaps guys

Last edited by Remy Lebeau; 10-13-2013 at 19:33.
Remy Lebeau is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 10-14-2013 , 07:24   Re: Some vector maths help!
Reply With Quote #10

You could always create a point_push

Honestly they work worlds better than anything you can do with sourcemod, without spending tons of time doing complex maths. Just offset the push slightly from them (towards you) and they will get moved.. Or parent it to another entity like yourself, and you wont get pushed, but they will.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:45.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode