offsets when attaching an object to a vehicle
#1

Hello, i have a problem. I am working on a siren system to attach to a vehicle. All good, I use EditDynamicObject to let the player put the siren how he wants on the vehicle, but the problem is I don't know how to calculate the offsets to use in AttachDynamicObjectToVehicle.. i tried to do something like ObjectX - VehicleX to find the offsets but it doesn't work.

Any ideas?
Reply
#2

You need to do Object Pos - Vehicle Pos (x, y and z seperately) - that's correct. But for that to work the vehicle must face north. If you attach an object the coords you need are relative to the vehicle's axis. What you calculated is relative to the World.

If you have a custom mod garage or something, the easiest way would be to make the vehicle face north.

If the vehicle is rotated on the Z axis, you can get the vehicle Z angle and rotate the object coords by the opposite of that (using sin and cos), then rotate the object as well so that the Z rotation fits the new position.

Код:
GetObjectAttachmentOffset(objectid, vehicleid, &Float:att_x, &Float:att_y, &Float:att_z, &Float:att_rx, &Float:att_ry, &Float:att_rz)
{
	new Float:v_x, Float:v_y, Float:v_z, Float:v_rz,
	Float:o_x, Float:o_y, Float:o_z, Float:o_rx, Float:o_ry, Float:o_rz;

	GetVehiclePos(vehicleid, v_x, v_y, v_z);
	GetVehicleZAngle(vehicleid, v_rz);

	GetObjectPos(objectid, o_x, o_y, o_z);
	GetObjectRot(objectid, o_rx, o_ry, o_rz);

	new Float:o_distance = VectorSize(v_x - o_x, v_y - o_y, 0.0), // Calculate the X/Y distance from the vehicle to the object so we can place it again in the new direction
		Float:o_angle = (atan2(o_y - v_y, o_x - v_x) - 90.0) - v_rz; // Get the object's angle relative to the vehicle's angle

	o_x = v_x + o_distance * floatsin(-o_angle, degrees); // Rotate the object position by the difference calculated above (o_angle)
	o_y = v_y + o_distance * floatcos(-o_angle, degrees);

	o_rz -= v_rz; // Adjust the Z angle of the object to face the original direction (rotate it by the opposite of the vehicle's angle)

	att_x = o_x - v_x;
	att_y = o_y - v_y;
	att_z = o_z - v_z;

	att_rx = o_rx;
	att_ry = o_ry;
	att_rz = o_rz;

	return 1;
}
This should do it.
It calculates the offset of an object to the vehicle relative to the vehicle's rotation.
So if you create an object on the vehicle (no matter the vehicle's Z rotation) it will give you the attachment offsets for exactly that position.

Make sure the vehicle is on a flat surface otherwise the position will be a bit off.
Reply
#3

I like that function, could be used in Texture Studio. I just always make sure the vehicle is facing north and in the correct position it isn't as clever as this technique but it was consistent.

https://github.com/Pottus/Texture-St...o/vehicles.pwn
Read from line 1697 down.
Reply
#4

Quote:
Originally Posted by Pottus
Посмотреть сообщение
I like that function, could be used in Texture Studio. I just always make sure the vehicle is facing north and in the correct position it isn't as clever as this technique but it was consistent.
Yea for an editor that shouldn't matter as you can control it.

I also have one that respects x, y and z rotation. That should be even better for TS, can PM it to you if you want.

The problem with some vehicles is, that even when they are on a flat surface they have a slight x or y rotation and then the objects will be tilted left/right or to the front. For example bikes or (the worst kind) boats.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)