About Store Forum Documentation Contact



Post Reply 
Need help with .dir
Author Message
Aniketos Offline
Member

Post: #1
Need help with .dir
I want to basically play different animations based on the direction of the target in comparison to the attacker. So lets say the attacker (player) is attacking the targets back I want to play animation a and if he is attacking him from the front play animation b basically.

I guess I have to do some calculations with the attackers dir (Players[0].cskel.getPoint("main").dir) and targets dir (attackTarget->cskel.getPoint("main").dir).

Although I can't figure out the exact formula does anyone have any idea?
12-17-2010 06:28 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #2
RE: Need help with .dir
To get direction from point A to point B:

Code:
Vec dir = b - a;
dir.normalize();

or shorthand:

Code:
Vec dir = !(b - a);
12-17-2010 06:50 AM
Find all posts by this user Quote this message in a reply
Aniketos Offline
Member

Post: #3
RE: Need help with .dir
(12-17-2010 06:50 AM)Driklyn Wrote:  To get direction from point A to point B:

Code:
Vec dir = b - a;
dir.normalize();

or shorthand:

Code:
Vec dir = !(b - a);

I already though something like that but how do I interpret the answer I mean it will be an vector. How do I know if the target is basically facing me or looking the other way?
12-17-2010 07:11 AM
Visit this user's website Find all posts by this user Quote this message in a reply
fatcoder Offline
Member

Post: #4
RE: Need help with .dir
Dot product of the two vectors will give you the angle between them.

If the angle is greater than zero, then they are pointing within the same 180 degree arc... in other words, your target is facing away from you. If the angle is less than zero, then your target is facing you... so run! wink
(This post was last modified: 12-17-2010 07:45 AM by fatcoder.)
12-17-2010 07:45 AM
Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #5
RE: Need help with .dir
Look into Angle and AngleDelta. Something like this (I haven't tested this, likely to fail):

Code:
Flt angA = Angle(a, Players[0].orn());
Flt angB = Angle(b, attackTarget->orn());

Flt angle = RadToDeg(AngleDelta(angA, angB));

if (angle <= 45 && angle > -45) {
    // hit from front
} else if (angle <= 135 && angle > 45) {
    // hit from left
} else if (angle <= -45 && angle > -135) {
    // hit from right
} else {
    // hit from behind
}
12-17-2010 08:06 AM
Find all posts by this user Quote this message in a reply
Aniketos Offline
Member

Post: #6
RE: Need help with .dir
(12-17-2010 07:45 AM)fatcoder Wrote:  Dot product of the two vectors will give you the angle between them.

If the angle is greater than zero, then they are pointing within the same 180 degree arc... in other words, your target is facing away from you. If the angle is less than zero, then your target is facing you... so run! wink

I can't figure out how the dot product will help out I did some testing and it didn't really give me the rights results.

I did something like this: Dot(attackTarget->ctrl.actor.pos(), Players[0].ctrl.actor.pos());

       

(12-17-2010 08:06 AM)Driklyn Wrote:  Look into Angle and AngleDelta. Something like this (I haven't tested this, likely to fail):

Code:
Flt angA = Angle(a, Players[0].orn());
Flt angB = Angle(b, attackTarget->orn());

Flt angle = RadToDeg(AngleDelta(angA, angB));

if (angle <= 45 && angle > -45) {
    // hit from front
} else if (angle <= 135 && angle > 45) {
    // hit from left
} else if (angle <= -45 && angle > -135) {
    // hit from right
} else {
    // hit from behind
}

I couldn't really figure this out either :( whats the a in this this line: Flt angA = Angle(a, Players[0].orn()); ? and b in the line below.
12-17-2010 09:38 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #7
RE: Need help with .dir
I suppose a could be Players[0].pos() or Players[0].cskel.getPoint("main").pos, then b would obviously be the same except for attackTarget instead. I haven't messed with Angle or AngleDelta much before, if at all, so I'm unsure if my exact code will work or not, it might take some tweaking, but you should be able to get the angle you need by using AngleDelta.

Maybe this post will help? http://www.esenthel.com/community/showth...angledelta
12-18-2010 12:54 AM
Find all posts by this user Quote this message in a reply
fatcoder Offline
Member

Post: #8
RE: Need help with .dir
You need to use the normalized direction (as driklyn pointed out earlier) in the dot product. Not the position.
12-18-2010 01:34 AM
Find all posts by this user Quote this message in a reply
Aniketos Offline
Member

Post: #9
RE: Need help with .dir
EDIT: Nvm I got it now changed the degrees to less then 90 for back and more then 90 for front and it works now. Thnks for the help guys wink

Vec playervec = attackTarget->cskel.getPoint("main").dir;
playervec.normalize();
Vec targetvec = Players[0].cskel.getPoint("main").dir;
targetvec.normalize();

Flt Dotprod = Dot(targetvec, playervec);
Flt radialen = Acos(Dotprod);
Flt degrees = RadToDeg(radialen);

if (degrees >= 0 && degrees <= 90)
{
attack_dir = "back";
}
else if( degrees > 90 )
{
attack_dir = "front";
}
(This post was last modified: 12-18-2010 02:38 AM by Aniketos.)
12-18-2010 02:35 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #10
RE: Need help with .dir
Cool, you finally got it working.

On a side note, I believe the two .normalize() calls are unnecessary since you are using cskel.getPoint("main").dir to begin with. I'm pretty sure this is already normalized. Just a minor optimization if you want to use it.
12-18-2010 06:38 AM
Find all posts by this user Quote this message in a reply
fatcoder Offline
Member

Post: #11
RE: Need help with .dir
Correct... EE always gives normalized vectors. The only time you need to normalize is if you fiddle with it. Good to see you got it working.
12-18-2010 08:11 AM
Find all posts by this user Quote this message in a reply
Aniketos Offline
Member

Post: #12
RE: Need help with .dir
Strange because while I was debugging the values of attackTarget->cskel.getPoint("main").dir; got changed (slightly) after I called: playervec.normalize();. I'll have to experiment with this more and see the results.
12-18-2010 01:05 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply