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

Solved Get direction player is moving relative to other player


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Effeff
AlliedModders Donor
Join Date: Jan 2019
Location: discord: ff#0533
Old 02-28-2021 , 18:27   Get direction player is moving relative to other player
Reply With Quote #1

I'm trying to make fist combat a little more complex in csgo (rather than just plain old flat damage every time). One of the things I want to do is make it so that when you punch someone, your damage increases relative to your speed if you were moving toward them, and decreases relative to your speed if you were moving away from them. I will be using SDKHook_OnTakeDamage to check if attacker is moving toward or away from victim, and how quickly the attacker is moving.

I'm not certain on the next step I would take from here. The picture below is my attempt at kinda showing what I plan to do. Blue punches red. Green arrow represents the direction they are running.



I think what I need to do is create a line between the attacker's origin and the victim's origin.
Then, I would get the line perpendicular to this one. This perpendicular line represents the 100% line (no increase or decrease in damage).
Next, I would create another line using the attacker's velocity vector. This line would start from the attacker's origin.
From here, I need to do something with angles to determine how I increase/decrease damage, but I am not too sure on how to go about this.


Last edited by Effeff; 03-02-2021 at 14:45.
Effeff is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 03-01-2021 , 11:20   Re: Get direction player is moving relative to other player
Reply With Quote #2

This should be easily doable with a simple dot product between vectors a and b, where vector a is a unit vector in the direction from attacker's origin to victim's origin and b is a unit vector in the direction of attacker's velocity. You can think of a dot product as a projection of one vector onto the another one, like in this pic from Wikipedia's page on dot product:
.

If the a and b are parallel (that is, the attacker is moving towards the victim) then the dot product would be 1, and -1 if moving away (a and b are negatives of each other). Everything in between is, well, in between. To get a gradient between 0.5 and 1.5 it should be as simple as 1 + 0.5 * dot_product.
__________________

Last edited by klippy; 03-01-2021 at 11:22.
klippy is offline
Effeff
AlliedModders Donor
Join Date: Jan 2019
Location: discord: ff#0533
Old 03-01-2021 , 19:32   Re: Get direction player is moving relative to other player
Reply With Quote #3

Thanks for your advice.

I currently have this, but I am getting a number in the thousands.

PHP Code:
float GetDamageVelocityModifier(int attackerint victim)
{
    
float attackerPos[3], victimPos[3], unitVec[3], attackerVel[3], attackerNextPos[3], attackerVelVec[3];

    
GetClientAbsOrigin(attackerattackerPos);
    
GetClientAbsOrigin(victimvictimPos);
    
MakeVectorFromPoints(attackerPosvictimPosunitVec);

    
GetEntPropVector(attackerProp_Data"m_vecVelocity"attackerVel);
    
AddVectors(attackerPosattackerVelattackerNextPos);
    
MakeVectorFromPoints(attackerPosattackerNextPosattackerVelVec);

    return 
0.5 GetVectorDotProduct(unitVecattackerVelVec);

I am guessing what I forgot to do is use NormalizeVector somewhere?
Effeff is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 03-02-2021 , 08:03   Re: Get direction player is moving relative to other player
Reply With Quote #4

Quote:
Originally Posted by Effeff View Post
I am guessing what I forgot to do is use NormalizeVector somewhere?
Yeah you should normalize both vectors. Dot product takes vector magnitudes into account but you're not interested in that, hence why we want unit vectors. Also you don't need the AddVectors and MakeVectorFromPoints part - attackerVel and attackerVelVec are equivalent vectors in your case. Also as you seem to have some trouble wrapping your head arounds vectors in space, read up on free and bound vectors; I think that may help you a bit. Here we are always working with free vectors - the initial point of all vectors is the world origin (0, 0, 0), so all vectors with the same direction and magnitude are equivalent.
While you may be thinking of velocity as this:

In actuality it's just this:
__________________

Last edited by klippy; 03-02-2021 at 08:13.
klippy is offline
Effeff
AlliedModders Donor
Join Date: Jan 2019
Location: discord: ff#0533
Old 03-02-2021 , 14:45   Re: Get direction player is moving relative to other player
Reply With Quote #5

Awesome, changed it to the snippet below and it works perfectly.
Thank you so much for your explanation!

I feel a bit silly for what I did with the AddVectors part - I started with a velocity vector, used it to get the next position, and then used the two positions to just get the same velocity vector back

PHP Code:
float GetDamageVelocityModifier(int attackerint victim)
{
    
float attackerPos[3], victimPos[3], unitVec[3], attackerVel[3];
    
GetClientAbsOrigin(attackerattackerPos);
    
GetClientAbsOrigin(victimvictimPos);
    
MakeVectorFromPoints(attackerPosvictimPosunitVec);
    
GetEntPropVector(attackerProp_Data"m_vecVelocity"attackerVel);
    
NormalizeVector(unitVecunitVec);
    
NormalizeVector(attackerVelattackerVel);
    
float dp GetVectorDotProduct(unitVecattackerVel);
    
PrintToChatAll("dot product: %f"dp);
    return 
0.5 dp;

If anyone reading is curious, here's a video of what I'm doing:



(note that the damage in chat is wrong because armor appears to eat 50% of damage).
Effeff is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 03-02-2021 , 18:26   Re: Get direction player is moving relative to other player
Reply With Quote #6

If I understand your armour issue correctly, you should be able to use a OnTakeDamageAlive hook to get the real damage applied to the player.
__________________
asherkin is offline
Reply


Thread Tools
Display Modes

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 15:47.


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