Raycasting::testPointToPoint

From RAGE Multiplayer Wiki
Revision as of 12:29, 21 December 2018 by Unknown (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Client-Side
Function

Js.png JavaScript



Description

This function casts a ray from Point1 to Point2 and returns the position and entity of what's in the way, or undefined if the way is cleared.

Flags are intersection bit flags. They tell the ray what to care about and what not to care about when casting. Passing -1 will intersect with everything, presumably.

Flags:
1: Intersect with map
2: Intersect with vehicles (used to be mission entities?) (includes train)
4: Intersect with peds? (same as 8)
8: Intersect with peds? (same as 4)
16: Intersect with objects
32: Unknown
64: Unknown
128: Unknown
256: Intersect with vegetation (plants, coral. trees not included)

NOTE: Raycasts that intersect with mission_entites (flag = 2) has limited range and will not register for far away entites. The range seems to be about 30 metres.

Syntax

mp.raycasting.testPointToPoint(pos1, pos2, [ignoredEntity], [flags]);

Required Arguments

  • pos1: Vector3
  • pos2: Vector3
  • ignoreEntity: Entity handle or object
  • flags: Int

Return value

  • object

Example

Client-Side
mp.events.add('render', () => {
	const startPosition = mp.players.local.getBoneCoords(12844, 0.5, 0, 0);
	const endPosition = new mp.Vector3(0, 0, 75);

	const hitData = mp.raycasting.testPointToPoint(startPosition, endPosition);
	if (!hitData) {
		mp.game.graphics.drawLine(startPosition.x, startPosition.y, startPosition.z, endPosition.x, endPosition.y, endPosition.z, 255, 255, 255, 255); // Is in line of sight
	} else {
		mp.game.graphics.drawLine(startPosition.x, startPosition.y, startPosition.z, endPosition.x, endPosition.y, endPosition.z, 255, 0, 0, 255); // Is NOT in line of sight
	}
});


This function returns undefined or a valid result, if you point with your camera on something.

Client-Side
function pointingAt(distance) {
    const camera = mp.cameras.new("gameplay"); // gets the current gameplay camera

    let position = camera.getCoord(); // grab the position of the gameplay camera as Vector3

    let direction = camera.getDirection(); // get the forwarding vector of the direction you aim with the gameplay camera as Vector3

    let farAway = new mp.Vector3((direction.x * distance) + (position.x), (direction.y * distance) + (position.y), (direction.z * distance) + (position.z)); // calculate a random point, drawn on a invisible line between camera position and direction (* distance)

    let result = mp.raycasting.testPointToPoint(position, farAway, [1, 16]); // now test point to point

    return result; // and return the result ( undefined, if no hit )
}

See also

Template:Worldprobe s function c