Getting XY in front of an object.
#1

Hey folks!
Im working on an include, and i would like to know, how to calculate the XY pos in front of an object?
My solution was this:

Code:
new Float:Tx, Float:Ty, Float:Tz;
new Float:TRY,Float:TRZ;
GetObjectPos(Targets[targetid][main_object_id], Tx, Ty, Tz);
GetObjectRot(Targets[targetid][main_object_id], TRY, TRY, TRZ);

Tx += (distance * floatsin(-TRY, degrees));
Ty += (distance * floatcos(-TRY, degrees));

MoveObject(Targets[targetid][main_object_id], Tx, Ty, Tz, speed);
It works when the object is facing to 180°‎‎ (opposite direction of North), but when i change the objects Y rotation, it moves to the wrong diretcion.

Hope u can help me, thanks!
Reply
#2

You're overwriting the TRY variable each time you pass it to the function so in actual fact you'll only get the Z rotation. In front of an object can seem like it's behind for you... if that makes sense.

EDIT:

Think of x as the object

i X

the front could be to the right of this page if you was standing in position i whereas in the following example:

X i

The front would seem like it was in fact behind the object in your perspective.

I would recommend using a rotation of just 0.0 unless there is an actual way of getting the front of a object(not that I know of).

Yes, do what Pottus said. That way it'll always be in front of you.
Reply
#3

That is because your angles should be the same value for both the players facing angle try this.

pawn Code:
stock bool:GetPosInFrontOfPoint(&Float:OutputX, &Float:OutputY, Float:Angle, Float:Distance)
{
    if(!Distance) return false;
    OutputX += floatmul(Distance, floatsin(-Angle, degrees));
    OutputY += floatmul(Distance, floatcos(-Angle, degrees));
    return true;
}
Is a bad idea to do it the way your trying to do it as you might need the same function it will be easier to update the object position with less copy paste code. Does this happen to be for a map editor ?
Reply
#4

Thank you guys. Finally i realised what was my problem. I get the y rot instead of z. By the way my code was worked well, and yours is working too, Pottus.
Thanks!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)