How do you make a rotating attached object? -
Roiasher - 15.11.2015
Hey.
I've been looking for any tutorials, learned math all over again, but couldn't find an answer for this question.
I want to make a minigun that is attached to a vehicle - rotate with the camera.
I know I should use AttachObjectToVehicle to change the rotation itself.
I don't know how to calculate the Z axis every time.
I also know the camera's vector angles have something to do with the angle.
Help, please?
Re: How do you make a rotating attached object? -
Crayder - 15.11.2015
So basically what you are trying to do is translate the camera's rotation to the object?
Something like this (this is just concept code):
pawn Код:
new x2, y2, z2;
GetPlayerCameraFrontVector(p, x2, y2, z2)
Rotation_Z = atan2(y2, x2) + 90.0
Re: How do you make a rotating attached object? -
Roiasher - 16.11.2015
Quote:
Originally Posted by Crayder
So basically what you are trying to do is translate the camera's rotation to the object?
Something like this (this is just concept code):
[/pawn]
|
Well, that does not work.
It just takes the object, and moves it to a almost fixed position (although there IS a tiny difference, depending on the camera location).
I must repeat and say that the object is attached to a vehicle, and I feel that's the main problem in here.
Help?
Re: How do you make a rotating attached object? -
Crayder - 16.11.2015
I corrected the code in the last post. I wrote that late last night and must've forgot that the camera vector is already a directional unit so there was no need to subtract.
Re: How do you make a rotating attached object? -
AbyssMorgan - 16.11.2015
https://github.com/AbyssMorgan/ADM/b...SAM/3DTryg.inc
PHP код:
GetPlayerCameraRotation(playerid,&Float:rx,&Float:rz);
Give me the object id wants to check
Re: How do you make a rotating attached object? -
Crayder - 16.11.2015
Quote:
Originally Posted by AbyssMorgan
|
You do realize I already answered this right? This is not the time to advertise your include that is much more complicated than the code I provided above. Simple is always better in pawn, speed is everything.
Also a tip for the OP, the camera vector will only update once per second (SA-MP limitation) except when holding the aim so I suggest you hold it when you want to rotate the object.
Re: How do you make a rotating attached object? -
iggy1 - 16.11.2015
In my include (arrow.inc), I tried rotating attached objects but couldn't get them to rotate so it may not work anyway. Unless thats been fixed in server update since i wrote the include.
Only tested that on objects that are attached to players so it might work anyway on vehicles, just putting that out there because even if you get the correct code for calculating angles, it may not work.
Re: How do you make a rotating attached object? -
Roiasher - 16.11.2015
Quote:
Originally Posted by Crayder
I corrected the code in the last post. I wrote that late last night and must've forgot that the camera vector is already a directional unit so there was no need to subtract.
|
You're a genius.
It works great. (I only had to remove the 90.0 you added, as it made the minigun go 90 degrees more than the correct angle)
Can you explain me what you actually did here?
Thank you so much!
Re: How do you make a rotating attached object? -
Crayder - 16.11.2015
Quote:
Originally Posted by Roiasher
Can you explain me what you actually did here?
|
I guess I could break it down a bit.
pawn Код:
//Well first, obviously we need to know the camera's directional vector.
new x2, y2, z2;
GetPlayerCameraFrontVector(p, x2, y2, z2);
//Then we calculate the Z angle with some trigonometry.
Rotation_Z = atan2(y2, x2);
Lol, it's really that simple.
atan2 confuses me a bit. It basically just takes in the X and Y of a unit vector and translates it to angles (which are also always between -180 and 180). That's best described here:
https://en.wikipedia.org/wiki/Atan2
Re: How do you make a rotating attached object? -
AbyssMorgan - 16.11.2015
something like that ?
https://www.youtube.com/watch?v=ZZ-C_9autXg
Example:
PHP код:
//function
stock CompRotationFloat(Float:rotation,&Float:cr){
cr = rotation;
while(cr < 0.0) cr += 360.0;
while(cr >= 360.0) cr -= 360.0;
}
stock GetPlayerCameraZAngle(playerid,&Float:rz){
new Float:mx, Float:my, Float:mz;
GetPlayerCameraFrontVector(playerid,mx,my,mz);
CompRotationFloat((atan2(my,mx)-90.0),rz);
}
//test script
new alobj;
CMD:test82(playerid,params[]){
alobj = CreateDynamicObject(2985,0.0,0.0,-9000.0,0.0,0.0,0.0);
AttachDynamicObjectToVehicle(alobj,GetPlayerVehicleID(playerid),0.0,0.0,0.1,0.0,0.0,90.0);
return 1;
}
#define KEY_PRESSED(%0) (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
public OnPlayerKeyStateChange(playerid,newkeys,oldkeys){
if(KEY_PRESSED(KEY_YES)){ // 'Y'
new Float:rz, Float:rz2, vid = GetPlayerVehicleID(playerid);
GetPlayerCameraZAngle(playerid,rz);
GetVehicleZAngle(vid,rz2);
CompRotationFloat(rz-rz2+90,rz);
//+90.0 for objectid 2985
//rz-rz2 <-- Object rotation is relative to the vehicle. That is why we are doing so that was relative to the camera.
AttachDynamicObjectToVehicle(alobj,vid,0.0,0.0,0.1,0.0,0.0,rz);
}
return 1;
}