Posts: 13
Threads: 4
Joined: Aug 2010
Reputation:
0
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?
Posts: 3,324
Threads: 96
Joined: Sep 2013
15.11.2015, 23:20
(
Последний раз редактировалось Crayder; 16.11.2015 в 15:07.
)
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
Posts: 3,324
Threads: 96
Joined: Sep 2013
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.
Posts: 1,208
Threads: 36
Joined: Apr 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
Posts: 3,324
Threads: 96
Joined: Sep 2013
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.
Posts: 2,421
Threads: 52
Joined: Mar 2009
Reputation:
0
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.
Posts: 3,324
Threads: 96
Joined: Sep 2013
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
Posts: 1,208
Threads: 36
Joined: Apr 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;
}