Skip to content

Commit 2d22191

Browse files
feat(physx): add objects velocity (#76)
1 parent bf49313 commit 2d22191

5 files changed

Lines changed: 62 additions & 18 deletions

File tree

src/ecs/components/BoundingBoxComponent.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Type } from "../../core";
1+
import { Type, Vec2 } from "../../core";
22
import { PhysicsObject, Physx } from "../../physx";
33
import { BaseComponent } from "./BaseComponent";
44
import { TransformComponent } from "./TransformComponent";
@@ -60,10 +60,13 @@ export class BoundingBoxComponent extends BaseComponent implements ICollidableCo
6060
* @param physx - the physics engine where to push the physical object for the next update cycle
6161
*/
6262
public update(physx: Physx): void {
63+
const velocity: Vec2 = this.getContainer()?.getComponent<TransformComponent>('TransformComponent')?.velocity ?? new Vec2();
64+
6365
physx.pushPhysicalObject({
6466
object: {
6567
aabb: [this.aabb.x, this.aabb.y, this.aabb.width, this.aabb.height],
6668
isContainer: this.isContainer,
69+
velocity
6770
},
6871
onCollisionCallback: (object: PhysicsObject) => this.onCollision(object),
6972
});

src/physx/Physx.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
import { Vec2 } from "../core";
2+
13
export type AABB = [number, number, number, number];
24

35
export interface PhysicsObject {
46
// x, y, w, h
57
aabb: AABB;
68

9+
/**
10+
* The velocity of a physics object
11+
*/
12+
velocity: Vec2;
13+
714
/**
815
* Set to true if the PhysicsObject should act as a container for other objects to not escape from
916
*
@@ -12,6 +19,11 @@ export interface PhysicsObject {
1219
isContainer?: boolean;
1320
}
1421

22+
export interface onCollisionCallbackParams {
23+
otherObject: PhysicsObject;
24+
// collisionPlane: Vec2;
25+
}
26+
1527
export interface PhysicalObjectCallbackAggregate {
1628
/**
1729
* The object to push in the Physical World
@@ -23,7 +35,7 @@ export interface PhysicalObjectCallbackAggregate {
2335
* @param postSimulation
2436
* @returns
2537
*/
26-
onCollisionCallback: (postSimulation: PhysicsObject) => void;
38+
onCollisionCallback: (other: PhysicsObject) => void;
2739
}
2840

2941
export class Physx {
@@ -41,7 +53,7 @@ export class Physx {
4153
this.physicalWorld.forEach((physicalObject, idx) => {
4254
this.physicalWorld.forEach((otherPhysicalObject, otherIdx) => {
4355
if (idx !== otherIdx && this.checkCollision(physicalObject.object, otherPhysicalObject.object)) {
44-
physicalObject.onCollisionCallback(otherPhysicalObject.object);
56+
physicalObject.onCollisionCallback(otherPhysicalObject.object);
4557
};
4658
})
4759
});

test/performance/physx.perf.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Physx } from '../../src';
1+
import { Physx, Vec2 } from '../../src';
22
const bench = require('fastbench');
33

44
const physxEngine = new Physx();
@@ -7,7 +7,8 @@ export default bench([
77
function addComponents(done: Function) {
88
physxEngine.pushPhysicalObject({
99
object: {
10-
aabb: [Math.random(), Math.random(), Math.random(), Math.random()]
10+
aabb: [Math.random(), Math.random(), Math.random(), Math.random()],
11+
velocity: new Vec2(Math.random(), Math.random())
1112
},
1213
onCollisionCallback: () => { }
1314
})

test/unit/ecs/components/BoundingBoxComponent.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BaseEntity, BoundingBoxComponent, GameObject, ICollidableComponent, Physx, Vec2 } from "../../../../src";
1+
import { BaseEntity, BoundingBoxComponent, GameObject, ICollidableComponent, Physx, StaticObject, Vec2 } from "../../../../src";
22

33
describe('ecs/components/BoundingBoxComponent', () => {
44
let bbComponent = new BoundingBoxComponent();
@@ -87,7 +87,8 @@ describe('ecs/components/BoundingBoxComponent', () => {
8787
expect(physx.physicalWorld).toEqual(expect.arrayContaining([{
8888
object: {
8989
aabb: test.expected,
90-
isContainer: false
90+
isContainer: false,
91+
velocity: new Vec2()
9192
},
9293
onCollisionCallback: expect.any(Function)
9394
}]));
@@ -117,6 +118,22 @@ describe('ecs/components/BoundingBoxComponent', () => {
117118
expect(cbBBComponentA).toHaveBeenCalled();
118119
expect(cbBBComponentB).toHaveBeenCalled();
119120
});
121+
122+
it('Should get the velocity from a parent entity when present', () => {
123+
const entity = new StaticObject();
124+
entity.transform.velocity = new Vec2(5, 5);
125+
entity.boundingBox.aabb.width = 10;
126+
entity.boundingBox.aabb.height = 10;
127+
128+
entity.boundingBox.update(physx);
129+
130+
expect(physx.physicalWorld).toEqual(expect.arrayContaining([{
131+
object: expect.objectContaining({
132+
velocity: new Vec2(5, 5)
133+
}),
134+
onCollisionCallback: expect.any(Function)
135+
}]));
136+
})
120137
});
121138

122139
describe('.isContainer', () => {

test/unit/physx/Physx.test.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AABB, PhysicalObjectCallbackAggregate, PhysicsObject, Physx } from "../../../src"
1+
import { AABB, PhysicalObjectCallbackAggregate, PhysicsObject, Physx, Vec2 } from "../../../src"
22

33
describe('physx/Physx', () => {
44
let physx = new Physx();
@@ -11,7 +11,8 @@ describe('physx/Physx', () => {
1111
it('Should add the physicx object to the physics objects list', () => {
1212
const physicsObject: PhysicalObjectCallbackAggregate = {
1313
object: {
14-
aabb: [10, 10, 25, 25]
14+
aabb: [10, 10, 25, 25],
15+
velocity: new Vec2()
1516
},
1617
onCollisionCallback: (postSimulation) => {},
1718
};
@@ -25,14 +26,16 @@ describe('physx/Physx', () => {
2526
it('Should trigger the Physics object callback if a collision is detected', () => {
2627
const physicsObject: PhysicalObjectCallbackAggregate = {
2728
object: {
28-
aabb: [10, 10, 25, 25]
29+
aabb: [10, 10, 25, 25],
30+
velocity: new Vec2()
2931
},
3032
onCollisionCallback: jest.fn(() => { }),
3133
};
3234

3335
const physicsObject2: PhysicalObjectCallbackAggregate = {
3436
object: {
35-
aabb: [15, 15, 25, 25]
37+
aabb: [15, 15, 25, 25],
38+
velocity: new Vec2()
3639
},
3740
onCollisionCallback: jest.fn(() => { }),
3841
};
@@ -49,14 +52,16 @@ describe('physx/Physx', () => {
4952
it('Should trigger the Physics object callback if a reverse collision is detected', () => {
5053
const physicsObject: PhysicalObjectCallbackAggregate = {
5154
object: {
52-
aabb: [19, 70, 20, 2,]
55+
aabb: [19, 70, 20, 2],
56+
velocity: new Vec2()
5357
},
5458
onCollisionCallback: jest.fn(() => { }),
5559
};
5660

5761
const physicsObject2: PhysicalObjectCallbackAggregate = {
5862
object: {
59-
aabb: [0, 0, 20, 150]
63+
aabb: [0, 0, 20, 150],
64+
velocity: new Vec2()
6065
},
6166
onCollisionCallback: jest.fn(() => { }),
6267
};
@@ -73,14 +78,16 @@ describe('physx/Physx', () => {
7378
it('Should not trigger a collision for object that are not colliding', () => {
7479
const physicsObject: PhysicalObjectCallbackAggregate = {
7580
object: {
76-
aabb: [10, 10, 25, 25]
81+
aabb: [10, 10, 25, 25],
82+
velocity: new Vec2()
7783
},
7884
onCollisionCallback: jest.fn(() => { }),
7985
};
8086

8187
const physicsObject2: PhysicalObjectCallbackAggregate = {
8288
object: {
83-
aabb: [45, 45, 25, 25]
89+
aabb: [45, 45, 25, 25],
90+
velocity: new Vec2()
8491
},
8592
onCollisionCallback: jest.fn(() => { }),
8693
};
@@ -111,14 +118,16 @@ describe('physx/Physx', () => {
111118
const physicsObject: PhysicalObjectCallbackAggregate = {
112119
object: {
113120
aabb: test.containerAabb,
121+
velocity: new Vec2(),
114122
isContainer: true
115123
},
116124
onCollisionCallback: jest.fn(() => { }),
117125
};
118126

119127
const physicsObject2: PhysicalObjectCallbackAggregate = {
120128
object: {
121-
aabb: test.otherAabb
129+
aabb: test.otherAabb,
130+
velocity: new Vec2()
122131
},
123132
onCollisionCallback: jest.fn(() => { }),
124133
};
@@ -136,14 +145,16 @@ describe('physx/Physx', () => {
136145
const physicsObject: PhysicalObjectCallbackAggregate = {
137146
object: {
138147
aabb: [10, 10, 25, 25],
139-
isContainer: true
148+
isContainer: true,
149+
velocity: new Vec2()
140150
},
141151
onCollisionCallback: jest.fn(() => { }),
142152
};
143153

144154
const physicsObject2: PhysicalObjectCallbackAggregate = {
145155
object: {
146-
aabb: [15, 15, 5, 5]
156+
aabb: [15, 15, 5, 5],
157+
velocity: new Vec2()
147158
},
148159
onCollisionCallback: jest.fn(() => { }),
149160
};

0 commit comments

Comments
 (0)