Failed.Bard wrote...
I tried Lightfoot8's code first, works like a charm, though admittedly I have no idea why. Vector manipulation still throws me for a loop.
I will try and give an explanation. In truth once you get use to vectors they are easy to work with and open up a lot of things you can do in your code.
A vector is nothing more then a set of coordinates that represent where one object is from another. where a position is coordinates that represent where a point is from the (0,0) point on a graph. The vector defines both a Magnitude ( length of the vector ) and an angle.
As an example I will place two objects on a grid the source PC at (4,4) facing off at a 45 degree angle to the left of due north( up). and the target NPC at (13,13) .
To get the vector that the NPC is from the PC you just subtract the cords of the source from the cords of the target.
in this case that will give us a vector of (9,9). Our magnitude of the vector will be 12.7 the distance between the two points. and the angle will be 45 degrees.
An important note here is that a lot of people make a big fuss about what angle north is at in the game and the effects it has on there calulations. In some cases it might. but it really has no effect when working with vectors. Since the vector is reltive to the two objects it really maks no differance where the 0 angle points is , As long as both angles are calculated from the same refferance, the answer will be correct.
in the Pic above my 0 degree refferance is the x axis giving me a 45 deg angle for my vector. That would give the PC a facing of 135 deg.
If the refferance is the y axis, for our 0 deg, That would make the vector a -45 deg angle and the PC's facing a 45 deg angle.
it just does not really matter what system the engine uses to calculate the angles, the differance between them will be the same. 90 degree result.
I believe it is easy to see from the pic above. how to find the angle that oNPC is from the facing of oPC. broken down it is:
1) Get the position of oPC.
2 ) Get the position of oNPC.
3 ) Get the Vector between the two.
4 ) convert the vector to an angle.
5 ) Get the facing of oPC.
6 ) subtract the facing of the PC from the vector.
Postive angles will be to left of his facing, negtive angles to the right.
//Get the position of oPC.
vector posPC = GetPosition( oPC);
//Get the position of oNPC.
vector posNPC = GetPosition( oNPC);
//Get the Vector between the two.
vector vPCtoNPC = posNPC - posPC ;
//convert the vector to an angle.
float AngleOfVector = VectorToAngle(vPCtoNPC);
//Get the facing of oPC.
float PCFacing = GetFacing(oPC);
//subtract the facing of the PC from the vector.
float AngleOfSight = AngleOfVector - PCFacing;
Modifié par Lightfoot8, 18 juin 2012 - 03:47 .