Raycasting::testPointToPoint: Difference between revisions
m (Replaced HTML with template) |
|||
| (9 intermediate revisions by 4 users not shown) | |||
| Line 1: | Line 1: | ||
{{ClientsideJsFunction}} | |||
== 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.<br><br>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.<br><br>Flags:<br>1: Intersect with map<br>2: Intersect with vehicles (used to be mission entities?) (includes train)<br>4: Intersect with peds? (same as 8)<br>8: Intersect with peds? (same as 4)<br>16: Intersect with objects<br>32: Unknown<br>64: Unknown<br>128: Unknown<br>256: Intersect with vegetation (plants, coral. trees not included)<br><br>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. | 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.<br><br>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.<br><br>Flags:<br>1: Intersect with map<br>2: Intersect with vehicles (used to be mission entities?) (includes train)<br>4: Intersect with peds? (same as 8)<br>8: Intersect with peds? (same as 4)<br>16: Intersect with objects<br>32: Unknown<br>64: Unknown<br>128: Unknown<br>256: Intersect with vegetation (plants, coral. trees not included)<br><br>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== | ==Syntax== | ||
<pre> | <pre> | ||
mp.raycasting.testPointToPoint(pos1, pos2, [ignoredEntity], [flags]) | mp.raycasting.testPointToPoint(pos1, pos2, [ignoredEntity], [flags]) | ||
</pre> | </pre> | ||
=== Required | === Required arguments === | ||
*'''pos1:''' Vector3 | *'''pos1:''' Vector3 | ||
*'''pos2:''' Vector3 | *'''pos2:''' Vector3 | ||
===Return value=== | === Optional arguments === | ||
*'''ignoredEntity:''' Entity handle or object - example: mp.players.local | |||
*'''flags:''' Int representing sum of flags - example: 17 (intersect with map [1] and objects [16]) | |||
=== Return value === | |||
*'''object''' | *'''object''' | ||
'''Example of return object''' | |||
<pre> | |||
{ | |||
"position": { | |||
"x": 13.01, | |||
"y": 12.23, | |||
"z": 70.02 | |||
}, | |||
"surfaceNormal": { | |||
"x": 0.93, | |||
"y": -0.04, | |||
"z": -0.34 | |||
}, | |||
"entity": <entity object> | |||
} | |||
</pre> | |||
==Example== | ==Example== | ||
| Line 29: | Line 52: | ||
} | } | ||
}); | }); | ||
</pre> | |||
}} | |||
This function returns undefined or a valid result, if you point with your camera on something. | |||
{{ClientsideCode| | |||
<pre> | |||
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, null, 17); // now test point to point - intersects with map and objects [1 + 16] | |||
return result; // and return the result ( undefined, if no hit ) | |||
} | |||
</pre> | </pre> | ||
}} | }} | ||
==See also== | ==See also== | ||
{{ | {{Raycast_functions_c}} | ||
[[Category:Clientside API]] | [[Category:Clientside API]] | ||
Latest revision as of 17:42, 16 March 2025
Client-Side Function
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
Optional arguments
- ignoredEntity: Entity handle or object - example: mp.players.local
- flags: Int representing sum of flags - example: 17 (intersect with map [1] and objects [16])
Return value
- object
Example of return object
{
"position": {
"x": 13.01,
"y": 12.23,
"z": 70.02
},
"surfaceNormal": {
"x": 0.93,
"y": -0.04,
"z": -0.34
},
"entity": <entity object>
}
Example
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.
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, null, 17); // now test point to point - intersects with map and objects [1 + 16]
return result; // and return the result ( undefined, if no hit )
}