AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved Get direction player is moving relative to other player (https://forums.alliedmods.net/showthread.php?t=330955)

Effeff 02-28-2021 18:27

Get direction player is moving relative to other player
 
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.

https://i.imgur.com/EgoyLhY.png

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.

https://i.imgur.com/wjEmX6n.png

klippy 03-01-2021 11:20

Re: Get direction player is moving relative to other player
 
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:
https://upload.wikimedia.org/wikiped...roduct.svg.png.

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.

Effeff 03-01-2021 19:32

Re: Get direction player is moving relative to other player
 
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?

klippy 03-02-2021 08:03

Re: Get direction player is moving relative to other player
 
Quote:

Originally Posted by Effeff (Post 2738840)
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:
https://i.imgur.com/aL29KL1.png
In actuality it's just this:
https://i.imgur.com/lUeaMfo.png

Effeff 03-02-2021 14:45

Re: Get direction player is moving relative to other player
 
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 :oops:

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).

asherkin 03-02-2021 18:26

Re: Get direction player is moving relative to other player
 
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.


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

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