SetObjectToFaceCords help - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: SetObjectToFaceCords help (
/showthread.php?tid=500373)
SetObjectToFaceCords help -
HenrySunseri - 12.03.2014
I'm trying to make it so when the player enters the 40.0 area of a coordinate, a shark will face the player, and head at him with 4.0 speed. But instead, it goes kind of sideways and bobbles towards the player. Any help would be appreciated.
Код:
public Shark()
{
for(new playerid=0; playerid<MAX_PLAYERS; playerid++)
{
if(PlayerToPoint(40.0, playerid, 757.7714, -1996.8455, -0.3859))
{
new Float:X,Y,Z;
GetPlayerPos(playerid, Float:X, Float:Y, Float:Z);
SetObjectToFaceCords(Shark, Float:X, Float:Y, Float:Z);
MoveObject(Shark, Float:X, Float:Y, Float:Z, 4.0, -1000.0, -1000.0, 0);
}
}
return 1;
}
Код:
SetObjectToFaceCords(objectid, Float:x1,Float:y1,Float:z1)
{
// SetObjectToFaceCords() By LucifeR //
// LucifeR@vgames.co.il //
// setting the objects cords
new Float:x2,Float:y2,Float:z2;
GetObjectPos(objectid, x2,y2,z2);
// setting the distance values
new Float:DX = floatabs(x2-x1);
new Float:DY = floatabs(y2-y1);
new Float:DZ = floatabs(z2-z1);
// defining the angles and setting them to 0
new Float:yaw = 0;
new Float:pitch = 0;
// check that there isnt any 0 in one of the distances,
// if there is any use the given parameters:
if(DY == 0 || DX == 0)
{
if(DY == 0 && DX > 0)
{
yaw = 0;
pitch = 0;
}
else if(DY == 0 && DX < 0)
{
yaw = 180;
pitch = 180;
}
else if(DY > 0 && DX == 0)
{
yaw = 90;
pitch = 90;
}
else if(DY < 0 && DX == 0)
{
yaw = 270;
pitch = 270;
}
else if(DY == 0 && DX == 0)
{
yaw = 0;
pitch = 0;
}
}
// calculating the angale using atan
else // non of the distances is 0.
{
// calculatin the angles
yaw = atan(DX/DY);
pitch = atan(floatsqroot(DX*DX + DZ*DZ) / DY);
// there are three quarters in a circle, now i will
// check wich circle this is and change the angles
// according to it.
if(x1 > x2 && y1 <= y2)
{
yaw = yaw + 90;
pitch = pitch - 45;
}
else if(x1 <= x2 && y1 < y2)
{
yaw = 90 - yaw;
pitch = pitch - 45;
}
else if(x1 < x2 && y1 >= y2)
{
yaw = yaw - 90;
pitch = pitch - 45;
}
else if(x1 >= x2 && y1 > y2)
{
yaw = 270 - yaw;
pitch = pitch + 315;
}
if(z1 < z2)
pitch = 360-pitch;
}
// setting the object rotation (should be twice cuz of lame GTA rotation system)
SetObjectRot(objectid, 0, 0, yaw);
SetObjectRot(objectid, 0, pitch, yaw);
return 1;
}
Re: SetObjectToFaceCords help -
HenrySunseri - 12.03.2014
http://gyazo.com/bd846a7bb55b9429610875a2c07e93b0
^ the shark follows sideways, i want it to follow face first.
desperate for help! lol
Re: SetObjectToFaceCords help -
CuervO - 12.03.2014
I don't know if I've made this function, I highly doubt it, but I also forgot who made it so, I only remember me trying to create it with regular tangents functions only to find out they really weren't really answering to my trigonometry formulas (I even had my math teacher help me out with it at the moment and neither of us could figure it out using the atan provided in PAWN! but I've read somewhere that there was atan2 and answered exactly like I wanted it to answer -- Even one of my exams in university featured a similar problem!)
pawn Код:
stock Float:GetAngleToPoint(Float:fDestX, Float:fDestY, Float:fPointX, Float:fPointY)
return atan2((fDestY - fPointY), (fDestX - fPointX)) + 90.0;
I can assure you this does work with cow object! I don't know about the others :P
EDIT: Tested it with the shark object, same result as your bigass formula, it is due the GTA SA object rotation, for the shark it will work if instead of summing 180 you sum 90