/
hinge-joint.ts
64 lines (58 loc) · 1.99 KB
/
hinge-joint.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @packageDocumentation
* @module physics2d.box2d
*/
import b2 from '@cocos/box2d';
import { IHingeJoint } from '../../spec/i-physics-joint';
import { HingeJoint2D } from '../../framework';
import { b2Joint } from './joint-2d';
import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';
import { toRadian } from '../../../core';
export class b2HingeJoint extends b2Joint implements IHingeJoint {
enableLimit (v: boolean) {
if (this._b2joint) {
(this._b2joint as b2.RevoluteJoint).EnableLimit(v);
}
}
setLowerAngle (v: number) {
this.updateLimits();
}
setUpperAngle (v: number) {
this.updateLimits();
}
updateLimits () {
if (this._b2joint) {
const comp = this._jointComp as HingeJoint2D;
(this._b2joint as b2.RevoluteJoint).SetLimits(toRadian(comp.lowerAngle), toRadian(comp.upperAngle));
}
}
// motor
enableMotor (v: boolean) {
if (this._b2joint) {
(this._b2joint as b2.RevoluteJoint).EnableMotor(v);
}
}
setMaxMotorTorque (v: number) {
if (this._b2joint) {
(this._b2joint as b2.RevoluteJoint).SetMaxMotorTorque(v);
}
}
setMotorSpeed (v: number) {
if (this._b2joint) {
(this._b2joint as b2.RevoluteJoint).SetMotorSpeed(v);
}
}
_createJointDef () {
const comp = this._jointComp as HingeJoint2D;
const def = new b2.RevoluteJointDef();
def.localAnchorA.Set(comp.anchor.x / PHYSICS_2D_PTM_RATIO, comp.anchor.y / PHYSICS_2D_PTM_RATIO);
def.localAnchorB.Set(comp.connectedAnchor.x / PHYSICS_2D_PTM_RATIO, comp.connectedAnchor.y / PHYSICS_2D_PTM_RATIO);
def.enableMotor = comp.enableMotor;
def.maxMotorTorque = comp.maxMotorTorque;
def.motorSpeed = toRadian(comp.motorSpeed);
def.enableLimit = comp.enableLimit;
def.lowerAngle = comp.lowerAngle;
def.upperAngle = comp.upperAngle;
return def;
}
}