Skip to content

Commit

Permalink
finish angular velocity damping
Browse files Browse the repository at this point in the history
  • Loading branch information
Penguin-Spy committed Mar 4, 2024
1 parent 4658af7 commit cdd9eaa
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 49 deletions.
6 changes: 3 additions & 3 deletions client/ContraptionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ export default class ContraptionController extends Controller {
yaw = -Input.mouseDX()
}

if(Input.get('pitch_up')) {
pitch = 1
} else if(Input.get('pitch_down')) {
if(Input.get('pitch_up')) { // pitch is technically inverted in a contraption (TODO: make this 2 settings, one for player & one for contraption)
pitch = -1
} else if(Input.get('pitch_down')) {
pitch = 1
} else {
pitch = Input.mouseDY()
}
Expand Down
2 changes: 1 addition & 1 deletion client/screens/main_menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default {
}
},

{ $: 'span', content: "alpha-9_1", class: 'version' },
{ $: 'span', content: "alpha-10_1", class: 'version' },
{
$: 'a', content: "view source", class: 'source',
href: "https://github.com/Penguin-Spy/Voxilon", target: '_blank'
Expand Down
79 changes: 36 additions & 43 deletions common/GyroManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,14 @@ import NetworkedComponent from "/common/NetworkedComponent.js"
import * as THREE from 'three'
import { check, DT } from "/common/util.js"

const _avaliableTorque = new THREE.Vector3()
const _twist = new THREE.Vector3()
const _angularVelocity = new THREE.Vector3()
const _worldToLocalQuaternion = new THREE.Quaternion()

const _v1 = new THREE.Vector3()

const abs = Math.abs, min = Math.min, max = Math.max
const min = Math.min, max = Math.max, abs = Math.abs, sign = Math.sign

/**
* Steps a value towards zero by `step`, does not overshoot. Returns the distance to zero
* @param {number} value the value to move towards zero
* @param {number} step how much to move it by
* @returns {number} the clamped distance to zero
*/
function toZeroStep(value, step) {
if(abs(value) < step) {
return -value
} else if(value > 0) {
return -step
} else {
return step
}
}

function clamp(value, min, max) {
return max(min(value, max), min)
}

/**
* Manages recieving input and applying torque on a contraption via it's gyroscopes
* Manages recieving input and applying torque on a contraption via its gyroscopes
*/
export default class GyroManager {
/** @type {NetworkedComponent} */
Expand Down Expand Up @@ -131,45 +108,61 @@ export default class GyroManager {

// calculate output torque necessary
update() {
_avaliableTorque.copy(this.#totalTorque).multiply(this.#rigidBody.invInertia).multiplyScalar(DT)

_worldToLocalQuaternion.copy(this.#rigidBody.quaternion).conjugate()

// twist (angular impulse)
// x: pitch (+up), y: yaw (+left), z: roll (+left)
_twist.set(0, 0, 0)

// current angular velocity to local refence frame
_angularVelocity.copy(this.#rigidBody.angularVelocity).applyQuaternion(_worldToLocalQuaternion)
_angularVelocity.copy(this.#rigidBody.angularVelocity).applyQuaternion(_worldToLocalQuaternion).multiply(this.#rigidBody.inertia).multiplyScalar(60)

if(this.#pitch > 0) {
_twist.x = _avaliableTorque.x * this.#torqueSensitivity
_twist.x = this.#totalTorque.x * this.#torqueSensitivity
} else if(this.#pitch < 0) {
_twist.x = -_avaliableTorque.x * this.#torqueSensitivity
} else if(this.#dampeners) {
_twist.x = -_angularVelocity.x
_twist.x = -this.#totalTorque.x * this.#torqueSensitivity
}
if(this.#yaw > 0) {
_twist.y = _avaliableTorque.y * this.#torqueSensitivity
_twist.y = this.#totalTorque.y * this.#torqueSensitivity
} else if(this.#yaw < 0) {
_twist.y = -_avaliableTorque.y * this.#torqueSensitivity
} else if(this.#dampeners) {
_twist.y = -_angularVelocity.y
_twist.y = -this.#totalTorque.y * this.#torqueSensitivity
}
if(this.#roll > 0) {
_twist.z = -_avaliableTorque.z * this.#torqueSensitivity
_twist.z = -this.#totalTorque.z * this.#torqueSensitivity
} else if(this.#roll < 0) {
_twist.z = _avaliableTorque.z * this.#torqueSensitivity
} else if(this.#dampeners) {
_twist.z = -_angularVelocity.z
_twist.z = this.#totalTorque.z * this.#torqueSensitivity
}

if(this.#dampeners) {
const dampPitch = min(this.#totalTorque.x, abs(_angularVelocity.x)) * -sign(_angularVelocity.x)
if(_twist.x > 0) { // set to the max of input & damp if in same dir, or just the input if not
_twist.x = max(_twist.x, dampPitch)
} else if(_twist.x < 0) {
_twist.x = min(_twist.x, dampPitch)
}
const dampYaw = min(this.#totalTorque.y, abs(_angularVelocity.y)) * -sign(_angularVelocity.y)
if(_twist.y > 0) {
_twist.y = max(_twist.y, dampYaw)
} else if(_twist.y < 0) {
_twist.y = min(_twist.y, dampYaw)
} else {
_twist.y = dampYaw
}
const dampRoll = min(this.#totalTorque.z, abs(_angularVelocity.z)) * -sign(_angularVelocity.z)
if(_twist.z > 0) {
_twist.z = max(_twist.z, dampRoll)
} else if(_twist.z < 0) {
_twist.z = min(_twist.z, dampRoll)
} else {
_twist.z = dampRoll
}
}
_twist.clamp(_v1.copy(_avaliableTorque).negate(), _avaliableTorque)

// save output torque first before we modify twist for applying to the rigid body
this.outputTorque.copy(_twist).multiplyScalar(60).multiply(this.#rigidBody.inertia)
this.outputTorque.copy(_twist)

// convert twist to world frame and apply as torque
_twist.multiply(this.#rigidBody.invInertia)
_twist.multiply(this.#rigidBody.invInertia).multiplyScalar(DT)
_twist.applyQuaternion(this.#rigidBody.quaternion)
this.#rigidBody.angularVelocity.vadd(_twist, this.#rigidBody.angularVelocity)
}
Expand Down
2 changes: 1 addition & 1 deletion common/components/Gyroscope.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class Gyroscope extends NetworkedComponent {

super(data, world, boxShape, mesh.clone())

this.maxTorque = 4000 // in Newton-meters
this.maxTorque = 150 // in Newton-meters
}

serializeNetwork() { }
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "voxilon",
"version": "alpha-9_1",
"version": "alpha-10_1",
"description": "mm yesn javascrimpt videojuego",
"homepage": "https://github.com/Penguin-Spy/Voxilon",
"main": "index.js",
Expand Down

0 comments on commit cdd9eaa

Please sign in to comment.