PDA

View Full Version : Check if player is near the door


GoldenLion
04/12/2016, 11:23 AM
Hello, I've made a command which lets you break a window of a car, but you can only break the driver's window. Is it possible to check if the player is at the driver's door? I need that because it doesn't look too good if you are behind a car, then you do /breakwindow and it breaks the driver's window while you are not even near the driver's door. I've ******d and found that GetVehicleModelInfo function, but I've got no clue what to do with that. Anyways would that be possible to do?

Yaa
04/12/2016, 11:40 AM
deleted

Bolex_
04/12/2016, 11:41 AM
http://forum.sa-mp.com/showpost.php?p=1957918&postcount=7

or try to get PlayerPosition and Is payer on driver seat!

lollypap54
04/12/2016, 11:45 AM
Closest thing you can get to driver's door is front seat on GetVehicleModelInfo function, so you can write:

new Float:x, Float:y, Float:z;
GetVehicleModelInfo(yourvehmodel, VEHICLE_MODEL_INFO_FRONTSEAT, x, y, z);

I have never used this function, I'm just trying to get you on the road, try test that function, what it returns...

https://wiki.sa-mp.com/wiki/Vehicle_information_types
Here's types of that function.

After you get that window position, do IsPlayerInRangeOfPoint.

GoldenLion
04/12/2016, 11:46 AM
Well, I don't really know what is sine and cosine, because I haven't learned that yet so I guess I don't know how to do that lol.

EDIT:
Closest thing you can get to driver's door is front seat on GetVehicleModelInfo function, so you can write:

new Float:x, Float:y, Float:z;
GetVehicleModelInfo(yourvehmodel, VEHICLE_MODEL_INFO_FRONTSEAT, x, y, z);

I have never used this function, I'm just trying to get you on the road, try test that function, what it returns...

https://wiki.sa-mp.com/wiki/Vehicle_information_types
Here's types of that function.

After you get that window position, do IsPlayerInRangeOfPoint.
Wait I guess that's exactly what I need... Let me try.

GoldenLion
04/12/2016, 02:48 PM
OK, but if I'd use GetVehicleModelInfo and get the position of the front seat, what would I need to do so I could use IsPlayerInRangeOfPoint with it? Like get the coordinates of the vehicle's front seat seat in the world. You got the idea, like you could set player's position to the coordinates and all that stuff.

RyderX
04/12/2016, 02:52 PM
Try it?

GoldenLion
04/12/2016, 03:02 PM
Try what? It's getting the position of the model's front seat, not the vehicle's.

Vince
04/12/2016, 03:21 PM
Yes, you need to translate the relative coordinates to absolute coordinates but you must also take into account the current facing angle of the vehicle.


worldX = x + floatsin(-a, degrees) * offset_x;
worldY = y + floatcos(-a, degrees) * offset_y;
worldZ = z + offset_z;


Where x, y and z are retrieved from GetVehiclePos, a is retrieved from GetVehicleZAngle and offset_x, offset_y and offset_z are the relative coordinates for the drivers seat retrieved from GetVehicleModelInfo. I made some calculations on paper and I think this is correct.

Threshold
04/12/2016, 03:39 PM
offset_x would have to be the information retrieved from info types VEHICLE_MODEL_INFO_FRONTSEAT and VEHICLE_MODEL_INFO_SIZE. (Half the vehicle width because you will be calculating from the centre of the vehicle)

Basically
offset_x = frontseat_x - (width_x / 2);

Correct me if I'm wrong, but don't you need to account for the other offsets in X and Y calculations?
worldX = x + (floatsin(-a, degrees) * offset_y) + (floatcos(-a, degrees) * offset_x);
worldY = y + (floatcos(-a, degrees) * offset_y) - (floatsin(-a, degrees) * offset_x);
worldZ = z + offset_z;

Or have I completely failed in remembering how maths works? :S

GoldenLion
04/12/2016, 04:25 PM
Vince's one works, but only if the IsPlayerInRangeOfPoint range is 1.5 and that lets you break the window from the both sides of the vehicle, the driver and the passenger side. How can I make the world position go a little bit to the left on the car so it will be on the window? I don't know how to explain, but let's say right now it's on the seat, I want the position to be on the window, so like "move the position" to the left a little bit so it will be at the window. I don't know how to do that as I haven't learned these sines and cosines yet and all that stuff because I'm probably like 2 times younger than everyone here. :P Also how is it possible to make the player face the car?

Threshold
04/12/2016, 04:51 PM
Try placing a checkpoint with Vince's code and see where it is on the vehicle.

SetPlayerCheckpoint(playerid, worldX, worldY, worldZ);

All you need to do is decrease the offset_x value if you want it to be further out from the car.

GoldenLion
04/12/2016, 05:22 PM
I decreased offset_x, but it doesn't work like that. If I face north and decrease it then it moves to the right and if I face east it moves to the back of the vehicle if I decrease it and so on. Any other ideas?

Pottus
04/12/2016, 06:07 PM
This is never going to work perfectly unless the car on a level surface you should look at z-offsets to fix your range problem.

Threshold
05/12/2016, 03:44 AM
I tested this out and it worked pretty well. Vince's method didn't seem to work for me for some reason, so I used the one that I posted. The only exceptions in this case are very wide vehicles such as the AT-400, or any other kind of plane. Boats don't really have a driver door, so their results won't be relevant. Basically any wide vehicles will need to be giving their own offset values.

CMD:driverdoor(playerid, params[])
{
new vID, Float:dooroffset = 0.0;
if(sscanf(params, "iF(0.0)", vID, dooroffset)) return SendClientMessage(playerid, -1, "/driverdoor [vid] [offset]");
new Float:worldX, Float:worldY, Float:worldZ;
GetVehicleDriverDoorPos(vID, worldX, worldY, worldZ, dooroffset);
CreatePickup(1239, 1, worldX, worldY, worldZ);
return 1;
}

GetVehicleDriverDoorPos(vehicleid, &Float:worldX, &Float:worldY, &Float:worldZ, Float:user_offset = 0.0)
{
new model = GetVehicleModel(vehicleid);
if(model == 0) return 0;
new
Float:offset_x, Float:offset_y, Float:offset_z,
Float:x, Float:y, Float:z, Float:a
;
GetVehicleModelInfo(model, VEHICLE_MODEL_INFO_FRONTSEAT, a, offset_y, offset_z);
GetVehicleModelInfo(model, VEHICLE_MODEL_INFO_SIZE, offset_x, a, a);
offset_x = (-offset_x / 2) - user_offset;
GetVehiclePos(vehicleid, x, y, z);
GetVehicleZAngle(vehicleid, a);

worldX = x + (floatsin(-a, degrees) * offset_y) + (floatcos(-a, degrees) * offset_x);
worldY = y + (floatcos(-a, degrees) * offset_y) - (floatsin(-a, degrees) * offset_x);
worldZ = z + offset_z;
return 1;
}

This will create a pickup at the driver's door, with the exception of vehicles that extend outwards past the driver's door in width. (Mostly airplanes, as mentioned above)

Also, some vehicles such as the coach or AT-400 have doors that aren't aligned with the front seat, so you'll need to account for that too.

Note that 'user_offset' is the distance you want away from the door's position. Setting that offset to 0.0 will give you the coordinates of the driver's door, a good offset to check if a player is near the driver's door would be about 0.2.

GoldenLion
05/12/2016, 01:30 PM
Thank you very much, it works. But how is it possible to make a player face a specific vehicle? That would make the /breakwindow command perfect. That's my last question here. :P

Konstantinos
05/12/2016, 01:40 PM
Setting the angle directly:

new Float: px, Float: py, Float: pz;
GetPlayerPos(playerid, px, py, pz);
SetPlayerFacingAngle(playerid, AngleToPoint(px, py, worldX, worldY));


Float: AngleToPoint(Float: X1, Float: Y1, Float: X2, Float: Y2) // By Nero_3D
{
return atan((Y2 - Y1) / (X2 - X1)) + ((X2 < X1) ? 90 : 270);
}

GoldenLion
05/12/2016, 02:12 PM
Setting the angle directly:

new Float: px, Float: py, Float: pz;
GetPlayerPos(playerid, px, py, pz);
SetPlayerFacingAngle(playerid, AngleToPoint(px, py, worldX, worldY));


Float: AngleToPoint(Float: X1, Float: Y1, Float: X2, Float: Y2) // By Nero_3D
{
return atan((Y2 - Y1) / (X2 - X1)) + ((X2 < X1) ? 90 : 270);
}

Thank you.