Efficient Camera - Ground Rectangle collision point detection
#1

Hello, I'm looking for a good way to get the player camera front vector intersection point with the ground in order to check if that point is inside an area (a rectangle).
I've found some algorithms online that would do only if the ground surface is flat, which is not the case in SA map.
Maybe combining GetPointZPos, GetPlayerCameraFrontVector, GetPlayerCameraPos and the rectange vectors will do? I'm not familiar ColAndreas either, any help would be appreciated
Reply
#2

It's pretty simple actually.

First, you get the camera position and the front vector. The Vector will have a length of 1.0m so you should multiply all 3 components with the maximum distance you want to check (eg 5 meters).

After that you add the vector to the camera position and you'll get the target position.

Код:
new Float:cx, Float:cy, Float:cz, Float:vx, Float:vy, Float:vz;
GetPlayerCameraPos(playerid, cx, cy, cz);
GetPlayerCameraFrontVector(playerid, vx, vy, vz);

vx = cx + 5.0 * vx; // Vector length will be increased to 5 meters, then added to the camera position
vy = cy + 5.0 * vy;
vz = cz + 5.0 * vz;
vx, vy, vz will then hold the target position.

You can now use ColAndreas to obtain the hit position (if any) for the line between the Camera Position and the Target Position:

Код:
new ret = CA_RayCastLine(cx, cy, cz, vx, vy, vz, vx, vy, vz);
If ret is 0, nothing was hit. vx, vy, vz will not change and still hold the target position.

If ret is anything else than 0, the ray hit an object or water and vx, vy, vz will now contain the hit position.

Btw if ret is WATER_OBJECT (20000), it means that the ray hit water.

If you use MapAndreas you will have to use a loop to check for the nearest surface height. But I'd really suggest ColAndreas as it can be used literally anywhere, it's really precise and way faster for this specific task.

Make sure to setup ColAndreas first, see the release topic for a good guide.
Reply
#3

Maybe ?
PHP код:
GetPointInFrontOfCamera3D(playerid,&Float:tx,&Float:ty,&Float:tz,Float:radius,&Float:rx=0.0,&Float:rz=0.0); 
3DTryg.inc
Reply
#4

Much thanks, I was really overthinking it but your explanation made it look so simple. Appreciated
@AbyssMorgan will yours be more efficient?
Reply
#5

Quote:
Originally Posted by Battlezone
Посмотреть сообщение
Much thanks, I was really overthinking it but your explanation made it look so simple. Appreciated
@AbyssMorgan will yours be more efficient?
Fast, simple and effective
Reply
#6

Thank you both, will rep you when it's possible
EDIT: GetPointInFrontOfCamera3D is very slow.
I'm gonna try ColAndreas
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)