Skip to content

Instantly share code, notes, and snippets.

@nebular
Last active May 4, 2024 15:00
Show Gist options
  • Save nebular/81025fdb25ed6cc7b563437948ad3989 to your computer and use it in GitHub Desktop.
Save nebular/81025fdb25ed6cc7b563437948ad3989 to your computer and use it in GitHub Desktop.
PAtch to complete moving platforms in Hero - it detects in SelfRay whwn it lands and leaves a kinetic platform, then adjusts the speed. Note: For this to work, the kinetic platform has to calculate its speed and keep updating its own velocity field (this.velocity)
stopMoving() {
const v = this.v;
const o = this.option;
v.moveImpulse.set(0, 0, 0);
// RLP: Moving platforms also affect when not using controle
if (v.isOnMovingObject) {
// copy moving object velocity as is to V1
// - finally adjusted by this 1.1 is ugliest but for some reason the velocities dont match
this.tmpV1.copy(v.movingObjectVelocity)
.multiplyScalar(1.1); //.multiplyScalar( 0.9 )
} else {
// So now when IsOnMovingObject becomes false, the speed is not reset
// and we apply damping here
v.movingObjectVelocity.x *= 0.9;
v.movingObjectVelocity.y *= 0.9;
v.movingObjectVelocity.z *= 0.9;
// when it is close to zero - then set it to zero so it stops affecting stopMoving()
if (v.movingObjectVelocity.y < 0.0001) v.movingObjectVelocity.y = 0;
// slowdown
this.tmpV1.copy(this.velocity);//.multiplyScalar( 0.9 )
this.tmpV1.x *= 0.9;
this.tmpV1.z *= 0.9;
}
root.motor.change({
name: this.name,
//force: this.tmpV1.toArray(), forceMode:'velocity',
linearVelocity: this.tmpV1.toArray(),
//angularVelocity: this.tmpV2.toArray(),
wake: false
//noGravity:true
});
}
selfRay(rayInfo) {
const body = rayInfo.body && phy.byName(rayInfo.body);
if (rayInfo.hit) {
this.distance = rayInfo.distance; //MathTool.toFixed(r.distance-this.radius)
this.rayAngle = rayInfo.angle;
if (body) {
// it will have a velocity structure, we learn if it is moving or not
const { velocity } = body;
this.v.movingObjectVelocity.copy(velocity);
this.v.isOnMovingObject = velocity.x !== 0 || velocity.y !== 0 || velocity.z !== 0;
} else {
// No longer on a moving object - reset the flag but not the speed
this.v.isOnMovingObject = false;
}
} else {
this.distance = this.option.rayLength;//maxRayDistance;
this.rayAngle = 0;
// reset flag but not speed
this.v.isOnMovingObject = false;
}
}
getFloating() {
const v = this.v;
const o = this.option;
const floatingForce = o.springK * (o.floatingDis - this.distance) - this.velocity.y * (v.movingObjectVelocity.y !== 0
? o.dampingCmovingPlatform : o.dampingC);
v.moveImpulse.y = floatingForce * (this.mass);
}
this.option.dampingCmovingPlatform: 0, // Clear damping when on a MOVING Platform
@nebular
Copy link
Author

nebular commented May 4, 2024

there is still a little oscillation when boarding the platform - shouls be probably be integrated better in moveImpulse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment