@dimforge/rapier3d
Version:
3-dimensional physics engine in Rust - official JS bindings.
100 lines • 3.12 kB
JavaScript
import { VectorOps } from "../math";
import { FeatureType } from "./feature";
/**
* A ray. This is a directed half-line.
*/
export class Ray {
/**
* Builds a ray from its origin and direction.
*
* @param origin - The ray's starting point.
* @param dir - The ray's direction of propagation.
*/
constructor(origin, dir) {
this.origin = origin;
this.dir = dir;
}
pointAt(t) {
return {
x: this.origin.x + this.dir.x * t,
y: this.origin.y + this.dir.y * t,
// #if DIM3
z: this.origin.z + this.dir.z * t,
// #endif
};
}
}
/**
* The intersection between a ray and a collider.
*/
export class RayIntersection {
constructor(timeOfImpact, normal, featureType, featureId) {
/**
* The type of the geometric feature the point was projected on.
*/
this.featureType = FeatureType.Unknown;
/**
* The id of the geometric feature the point was projected on.
*/
this.featureId = undefined;
this.timeOfImpact = timeOfImpact;
this.normal = normal;
if (featureId !== undefined)
this.featureId = featureId;
if (featureType !== undefined)
this.featureType = featureType;
}
static fromRaw(raw) {
if (!raw)
return null;
const result = new RayIntersection(raw.time_of_impact(), VectorOps.fromRaw(raw.normal()), raw.featureType(), raw.featureId());
raw.free();
return result;
}
}
/**
* The intersection between a ray and a collider (includes the collider handle).
*/
export class RayColliderIntersection {
constructor(collider, timeOfImpact, normal, featureType, featureId) {
/**
* The type of the geometric feature the point was projected on.
*/
this.featureType = FeatureType.Unknown;
/**
* The id of the geometric feature the point was projected on.
*/
this.featureId = undefined;
this.collider = collider;
this.timeOfImpact = timeOfImpact;
this.normal = normal;
if (featureId !== undefined)
this.featureId = featureId;
if (featureType !== undefined)
this.featureType = featureType;
}
static fromRaw(colliderSet, raw) {
if (!raw)
return null;
const result = new RayColliderIntersection(colliderSet.get(raw.colliderHandle()), raw.time_of_impact(), VectorOps.fromRaw(raw.normal()), raw.featureType(), raw.featureId());
raw.free();
return result;
}
}
/**
* The time of impact between a ray and a collider.
*/
export class RayColliderHit {
constructor(collider, timeOfImpact) {
this.collider = collider;
this.timeOfImpact = timeOfImpact;
}
static fromRaw(colliderSet, raw) {
if (!raw)
return null;
const result = new RayColliderHit(colliderSet.get(raw.colliderHandle()), raw.timeOfImpact());
raw.free();
return result;
}
}
//# sourceMappingURL=ray.js.map