Is player standing at vehicle's trunk or hood
#1

Hey! I want to add to my command /trunk part of the code which will get vehicle's trunk X,Y and Z coordinates. I know I have to use GetVehicleModelInfo and GetVehiclePos, and use sinus with that, but I don't know how. Can anyone please help me? I really want to create that.
Reply
#2

You can always try getting a vehicle's position, getting the X and Y position in front of it, and the X and Y position behind it, see if they're close to one of them, and that's it. I know how to use mathematical things such as 'sin' and 'cos' etc, but I just don't know how to use it in SA-MP terms. I'm probably gonna start learning now and I might be able to get back to you if nobody else does.
Reply
#3

new Float:X1,Y1,Z1,X2,Y2,Z2;
GetVehiclePos(vehicleid,X1,Y1,Z1);
GetPlayerPos(playerid,X2,Y2,Z2);

then you need to test:
If(Y2-Y1) = a positive amount then hes at the trunk/hood.
If(Y2-Y1) = a negative amount then hes at the trunk/hood.

should be working like that if the car is at its location always X=0 Y=0 Z=0
-Y is the back of the car
+Y is the front of the car
Reply
#4

Not exactly. It's very dependent on which way the car is facing...
Reply
#5

Getvehiclerotation then aswell
Reply
#6

Something like that maybe... but it's hard to know, it's kind of approximate.
pawn Code:
stock GetXYBehindVehicle(vehicleid, &Float:x2, &Float:y2, Float:distance)
{
    new Float:a;
    GetVehiclePos(vehicleid, x2, y2, a);
    GetVehicleZAngle(vehicleid, a);
    x2 += (distance * floatsin(-a+180, degrees));
    y2 += (distance * floatcos(-a+180, degrees));
}

stock GetTrunkPos(vehicleid, &Float: x3, &Float:y3, &Float:z3)
{
    new Float: x, Float: y;
    new Float: sizeX, Float: sizeY, Float:sizeZ[2];
   
    GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_SIZE, sizeX, sizeY, sizeZ[0]);
   
    GetXYBehindVehicle(vehicleid, x, y, sizeY/2);
    x3 = x;
    y3 = y;
   
    GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_REAR_BUMPER_Z , sizeX, sizeY, sizeZ[1]);
   
    z3 = (sizeZ[1] + sizeZ[0]/2);
}
Reply
#7

Quote:
Originally Posted by [XST]O_x
View Post
Something like that maybe... but it's hard to know, it's kind of approximate.
pawn Code:
stock GetXYBehindVehicle(vehicleid, &Float:x2, &Float:y2, Float:distance)
{
    new Float:a;
    GetVehiclePos(vehicleid, x2, y2, a);
    GetVehicleZAngle(vehicleid, a);
    x2 += (distance * floatsin(-a+180, degrees));
    y2 += (distance * floatcos(-a+180, degrees));
}

stock GetTrunkPos(vehicleid, &Float: x3, &Float:y3, &Float:z3)
{
    new Float: x, Float: y;
    new Float: sizeX, Float: sizeY, Float:sizeZ[2];
   
    GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_SIZE, sizeX, sizeY, sizeZ[0]);
   
    GetXYBehindVehicle(vehicleid, x, y, sizeY/2);
    x3 = x;
    y3 = y;
   
    GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_REAR_BUMPER_Z , sizeX, sizeY, sizeZ[1]);
   
    z3 = (sizeZ[1] + sizeZ[0]/2);
}
what will those two stocks return?
Reply
#8

Will return nothing, but will store information in variables passed by reference
Reply
#9

Quote:
Originally Posted by dominik523
View Post
what will those two stocks return?
You only need GetTrunkPos.

An example to use:
pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
   if(!strcmp(cmdtext,"/mytrunkpos")) {
    new Float:x, Float:y, Float:z;
    GetTrunkPos(GetPlayerVehicleID(playerid), x, y, z);
    new string[64]; format(string ,sizeof(string), "%.1f, %.1f, %.1f", x, y, z);
    SendClientMessage(playerid, -1, string);
    return 1; }
    return 0;
}
Reply
#10

so I have something like this:
Code:
{
	new string[128], done, idx;
	new engine, lights, alarm, doors, bonnet, boot, objective;
	if(!IsPlayerLoggedIn(playerid)) return SendClientMessage(playerid, COLOR_GREY, "You need to login first before using any command.");
	if(PlayerInfo[playerid][pVeh] || PlayerInfo[playerid][pVeh2] || PlayerInfo[playerid][pVeh3] || PlayerInfo[playerid][pVeh4] || PlayerInfo[playerid][pVeh5])
    {
        new Float:vx, Float:vy, Float:vz;
        // veh 1
	    GetTrunkPos(PlayerInfo[playerid][pVeh],vx,vy,vz);
	    if(IsPlayerInRangeOfPoint(playerid,1.0, vx,vy,vz))
	    {
	       ...
	    }
will adding isPlayerInRangeOfPoint after GetTrunkPos work?
Reply
#11

Quote:
Originally Posted by dominik523
View Post
so I have something like this:
Code:
{
	new string[128], done, idx;
	new engine, lights, alarm, doors, bonnet, boot, objective;
	if(!IsPlayerLoggedIn(playerid)) return SendClientMessage(playerid, COLOR_GREY, "You need to login first before using any command.");
	if(PlayerInfo[playerid][pVeh] || PlayerInfo[playerid][pVeh2] || PlayerInfo[playerid][pVeh3] || PlayerInfo[playerid][pVeh4] || PlayerInfo[playerid][pVeh5])
    {
        new Float:vx, Float:vy, Float:vz;
        // veh 1
	    GetTrunkPos(PlayerInfo[playerid][pVeh],vx,vy,vz);
	    if(IsPlayerInRangeOfPoint(playerid,1.0, vx,vy,vz))
	    {
	       ...
	    }
will adding isPlayerInRangeOfPoint after GetTrunkPos work?
It should. Try it for yourself and tell me if it works.
If you want to check how accurate it is, try creating an object in these positions and see if it's created close to the trunk.

EDIT:
pawn Code:
stock GetXYBehindVehicle(vehicleid, &Float:x2, &Float:y2, Float:distance)
{
    new Float:a;
    GetVehiclePos(vehicleid, x2, y2, a);
    GetVehicleZAngle(vehicleid, a);
    x2 += (distance * floatsin(-a+180, degrees));
    y2 += (distance * floatcos(-a+180, degrees));
}

stock GetTrunkPos(vehicleid, &Float: x3, &Float:y3, &Float:z3)
{
    new Float: x, Float: y;
    new Float: sizeX, Float: sizeY, Float:sizeZ[2];
   
    GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_SIZE, sizeX, sizeY, sizeZ[0]);
   
    GetXYBehindVehicle(vehicleid, x, y, sizeY/2);
    x3 = x;
    y3 = y;
   
    GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_REAR_BUMPER_Z , sizeX, sizeY, sizeZ[1]);
   
    z3 = (sizeZ[1] + sizeZ[0] + 3.1);
}
Use this instead. Much more accurate.
Reply
#12

damn, it won't work :/
It says that I'm not near vehicle. This is part of /trunk:
Code:
if(PlayerInfo[playerid][pVeh] || PlayerInfo[playerid][pVeh2] || PlayerInfo[playerid][pVeh3] || PlayerInfo[playerid][pVeh4] || PlayerInfo[playerid][pVeh5])
    {
        new Float:vx, Float:vy, Float:vz;
        // veh 1
	    GetTrunkPos(PlayerInfo[playerid][pVeh],vx,vy,vz);
	    if(IsPlayerInRangeOfPoint(playerid,1.0, vx,vy,vz))
	    {
	        idx = PlayerInfo[playerid][pVeh];
	        if(IsBike(idx)) return SendClientMessage(playerid, COLOR_GREY, "This vehicle does not have trunk.");
			GetVehicleParamsEx(idx, engine, lights, alarm, doors, bonnet, boot, objective);
			if(!boot)
			{

			    SetVehicleParamsEx(idx, engine, lights, alarm, doors, bonnet, 1, objective);
			    format(string, sizeof(string), "* %s opens their vehicle's trunk.", RPN(playerid));
				SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
			}
			else
			{
			    SetVehicleParamsEx(idx, engine, lights, alarm, doors, bonnet, 0, objective);
			    format(string, sizeof(string), "* %s closes their vehicle's trunk.", RPN(playerid));
				SendNearbyMessage(playerid, 15, string, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE, COLOR_PURPLE);
			}
			done = 1;
			return 1;
	    }
stocks are the same as you wrote them
Reply
#13

Try enlarging the radius. Try something like 3-4. 1.0 is very tiny and the player almost has to be in the exact same spot.
Reply
#14

I set it to 3, and it won't work :/
Reply
#15

Use this instead:
https://sampforum.blast.hk/showthread.php?tid=413618
Reply
#16

Its working! thank you guys so much for helping me.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)