|
| 1 | +/* eslint-disable new-cap */ |
| 2 | +// We have to disable linting rules since Box2D functions do not |
| 3 | +// follow the same guidelines as the rest of the codebase. |
| 4 | + |
| 5 | +import { |
| 6 | + type b2Body, |
| 7 | + type b2Shape, |
| 8 | + type b2Fixture, |
| 9 | + b2BodyType, |
| 10 | + b2CircleShape, |
| 11 | + b2PolygonShape, |
| 12 | + b2Vec2, |
| 13 | +} from '@box2d/core'; |
| 14 | +import { type ReplResult } from '../../typings/type_helpers'; |
| 15 | + |
| 16 | +import { ACCURACY, type Force, type ForceWithPos } from './types'; |
| 17 | +import { type PhysicsWorld } from './PhysicsWorld'; |
| 18 | + |
| 19 | +export class PhysicsObject implements ReplResult { |
| 20 | + private body: b2Body; |
| 21 | + private shape: b2Shape; |
| 22 | + private fixture: b2Fixture; |
| 23 | + private forcesCentered: Force[] = []; |
| 24 | + private forcesAtAPoint: ForceWithPos[] = []; |
| 25 | + |
| 26 | + constructor( |
| 27 | + position: b2Vec2, |
| 28 | + rotation: number, |
| 29 | + shape: b2Shape, |
| 30 | + isStatic: boolean, |
| 31 | + world: PhysicsWorld, |
| 32 | + ) { |
| 33 | + this.body = world.createBody({ |
| 34 | + type: isStatic ? b2BodyType.b2_staticBody : b2BodyType.b2_dynamicBody, |
| 35 | + position, |
| 36 | + angle: rotation, |
| 37 | + }); |
| 38 | + this.shape = shape; |
| 39 | + |
| 40 | + this.fixture = this.body.CreateFixture({ |
| 41 | + shape: this.shape, |
| 42 | + density: 1, |
| 43 | + friction: 1, |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + public getFixture() { |
| 48 | + return this.fixture; |
| 49 | + } |
| 50 | + |
| 51 | + public getMass() { |
| 52 | + return this.body.GetMass(); |
| 53 | + } |
| 54 | + |
| 55 | + public setDensity(density: number) { |
| 56 | + this.fixture.SetDensity(density); |
| 57 | + this.body.ResetMassData(); |
| 58 | + } |
| 59 | + |
| 60 | + public setFriction(friction: number) { |
| 61 | + this.fixture.SetFriction(friction); |
| 62 | + } |
| 63 | + |
| 64 | + public getPosition() { |
| 65 | + return this.body.GetPosition(); |
| 66 | + } |
| 67 | + |
| 68 | + public setPosition(pos: b2Vec2) { |
| 69 | + this.body.SetTransformVec(pos, this.getRotation()); |
| 70 | + } |
| 71 | + |
| 72 | + public getRotation() { |
| 73 | + return this.body.GetAngle(); |
| 74 | + } |
| 75 | + |
| 76 | + public setRotation(rot: number) { |
| 77 | + this.body.SetAngle(rot); |
| 78 | + } |
| 79 | + |
| 80 | + public getVelocity() { |
| 81 | + return this.body.GetLinearVelocity(); |
| 82 | + } |
| 83 | + |
| 84 | + public setVelocity(velc: b2Vec2) { |
| 85 | + this.body.SetLinearVelocity(velc); |
| 86 | + } |
| 87 | + |
| 88 | + public getAngularVelocity() { |
| 89 | + return this.body.GetAngularVelocity(); |
| 90 | + } |
| 91 | + |
| 92 | + public setAngularVelocity(velc: number) { |
| 93 | + this.body.SetAngularVelocity(velc); |
| 94 | + } |
| 95 | + |
| 96 | + public addForceCentered(force: Force) { |
| 97 | + this.forcesCentered.push(force); |
| 98 | + } |
| 99 | + |
| 100 | + public addForceAtAPoint(force: Force, pos: b2Vec2) { |
| 101 | + this.forcesAtAPoint.push({ |
| 102 | + force, |
| 103 | + pos, |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + private applyForcesToCenter(world_time: number) { |
| 108 | + this.forcesCentered = this.forcesCentered.filter( |
| 109 | + (force: Force) => force.start_time + force.duration > world_time, |
| 110 | + ); |
| 111 | + |
| 112 | + const resForce = this.forcesCentered |
| 113 | + .filter((force: Force) => force.start_time < world_time) |
| 114 | + .reduce( |
| 115 | + (res: b2Vec2, force: Force) => res.Add(force.direction.Scale(force.magnitude)), |
| 116 | + new b2Vec2(), |
| 117 | + ); |
| 118 | + |
| 119 | + this.body.ApplyForceToCenter(resForce); |
| 120 | + } |
| 121 | + |
| 122 | + private applyForcesAtAPoint(world_time: number) { |
| 123 | + this.forcesAtAPoint = this.forcesAtAPoint.filter( |
| 124 | + (forceWithPos: ForceWithPos) => forceWithPos.force.start_time + forceWithPos.force.duration > world_time, |
| 125 | + ); |
| 126 | + |
| 127 | + this.forcesAtAPoint.forEach((forceWithPos) => { |
| 128 | + const force = forceWithPos.force; |
| 129 | + this.body.ApplyForce( |
| 130 | + force.direction.Scale(force.magnitude), |
| 131 | + forceWithPos.pos, |
| 132 | + ); |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + public applyForces(world_time: number) { |
| 137 | + this.applyForcesToCenter(world_time); |
| 138 | + this.applyForcesAtAPoint(world_time); |
| 139 | + } |
| 140 | + |
| 141 | + public isTouching(obj2: PhysicsObject) { |
| 142 | + let ce = this.body.GetContactList(); |
| 143 | + while (ce !== null) { |
| 144 | + if (ce.other === obj2.body && ce.contact.IsTouching()) { |
| 145 | + return true; |
| 146 | + } |
| 147 | + ce = ce.next; |
| 148 | + } |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + public toReplString = () => ` |
| 153 | + Mass: ${this.getMass() |
| 154 | + .toFixed(ACCURACY)} |
| 155 | + Position: [${this.getPosition().x.toFixed( |
| 156 | + ACCURACY, |
| 157 | + )},${this.getPosition().y.toFixed(ACCURACY)}] |
| 158 | + Velocity: [${this.getVelocity().x.toFixed( |
| 159 | + ACCURACY, |
| 160 | + )},${this.getVelocity().y.toFixed(ACCURACY)}] |
| 161 | + |
| 162 | + Rotation: ${this.getRotation() |
| 163 | + .toFixed(ACCURACY)} |
| 164 | + AngularVelocity: [${this.getAngularVelocity() |
| 165 | + .toFixed(ACCURACY)}]`; |
| 166 | + |
| 167 | + public scale_size(scale: number) { |
| 168 | + if (this.shape instanceof b2CircleShape) { |
| 169 | + this.shape.m_radius *= scale; |
| 170 | + } else if (this.shape instanceof b2PolygonShape) { |
| 171 | + let centroid: b2Vec2 = this.shape.m_centroid; |
| 172 | + let arr: b2Vec2[] = []; |
| 173 | + this.shape.m_vertices.forEach((vec) => { |
| 174 | + arr.push( |
| 175 | + new b2Vec2( |
| 176 | + centroid.x + scale * (vec.x - centroid.x), |
| 177 | + centroid.y + scale * (vec.y - centroid.y), |
| 178 | + ), |
| 179 | + ); |
| 180 | + }); |
| 181 | + this.shape = new b2PolygonShape() |
| 182 | + .Set(arr); |
| 183 | + } |
| 184 | + let f: b2Fixture = this.fixture; |
| 185 | + this.body.DestroyFixture(this.fixture); |
| 186 | + this.fixture = this.body.CreateFixture({ |
| 187 | + shape: this.shape, |
| 188 | + density: f.GetDensity(), |
| 189 | + friction: f.GetFriction(), |
| 190 | + }); |
| 191 | + } |
| 192 | +} |
0 commit comments