Skip to content

Commit

Permalink
test(2d): add tests for update transforms methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Gugustinette committed Oct 4, 2024
1 parent 6bcc4fb commit 2ddec82
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/2d/src/core/FCollider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,17 @@ export class FCollider {
*/
updateScale() {
// If the collider is a cuboid, update its half extents
if (this.__COLLIDER__.shape.type === RAPIER.ShapeType.Cuboid) {
this.__COLLIDER__.setHalfExtents(new RAPIER.Vector2(this.component.scale.x / 2 * this.__COLLIDER_SCALE_OFFSET__.x, this.component.scale.y / 2 * this.__COLLIDER_SCALE_OFFSET__.y))
if (this.__COLLIDER__.shape instanceof RAPIER.Cuboid) {
this.__COLLIDER__.setShape(new RAPIER.Cuboid(
this.component.scale.x / 2 * this.__COLLIDER_SCALE_OFFSET__.x,
this.component.scale.y / 2 * this.__COLLIDER_SCALE_OFFSET__.y,
))
}
// If the collider is a ball, update its radius
else if (this.__COLLIDER__.shape.type === RAPIER.ShapeType.Ball) {
this.__COLLIDER__.setRadius(
// Get the maximum value of x and y
else if (this.__COLLIDER__.shape instanceof RAPIER.Ball) {
this.__COLLIDER__.setShape(new RAPIER.Ball(
Math.max(this.component.scale.x, this.component.scale.y) / 2 * this.__COLLIDER_SCALE_OFFSET__.x,
)
))
}
}

Expand Down
37 changes: 37 additions & 0 deletions packages/2d/test/browser/collider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ describe('fCollider', () => {
expect(rectangle.collider.scale.x).toEqual(1)
expect(rectangle.collider.scale.y).toEqual(1)
expect(rectangle.collider.shape).toEqual(FShapes.RECTANGLE)
// Validate scaleX and scaleY
rectangle.collider.scaleX = 2
rectangle.collider.scaleY = 3
expect(rectangle.collider.scaleX).toEqual(2)
expect(rectangle.collider.scaleY).toEqual(3)
expect(rectangle.collider.scaleX).toEqual(rectangle.collider.scale.x)
expect(rectangle.collider.scaleY).toEqual(rectangle.collider.scale.y)
})

it('should create a collider with custom transforms', () => {
Expand Down Expand Up @@ -156,4 +163,34 @@ describe('fCollider', () => {
expect(rectangle.collider.scale.x).toEqual(4)
expect(rectangle.collider.scale.y).toEqual(4)
})

it('should update collider transforms with attached component transforms', () => {
const rectangle = new FRectangle(scene, {
position: { x: 1, y: 1 },
rotation: 0.1,
scale: { x: 1, y: 0.5 },
})
rectangle.initCollider({
position: { x: -2, y: 3 },
rotation: 0.3,
scale: { x: 2, y: 2 },
})
expect(rectangle.collider).toBeDefined()
// Validate transforms
expect(rectangle.collider.position.x).toEqual(-1)
expect(rectangle.collider.position.y).toEqual(4)
expect(rectangle.collider.rotation).closeTo(0.4, 0.0001)
expect(rectangle.collider.scale.x).toEqual(2)
expect(rectangle.collider.scale.y).toEqual(1)
// Modify component transforms
rectangle.setPosition({ x: 2, y: 2 })
rectangle.setRotation(0.2)
rectangle.setScale({ x: 3, y: 4 })
// Validate transforms
expect(rectangle.collider.position.x).toEqual(0)
expect(rectangle.collider.position.y).toEqual(5)
expect(rectangle.collider.rotation).closeTo(0.5, 0.0001)
expect(rectangle.collider.scale.x).toEqual(6)
expect(rectangle.collider.scale.y).toEqual(8)
})
})
37 changes: 37 additions & 0 deletions packages/2d/test/browser/rigidbody.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ describe('fRigidBody', () => {
expect(rectangle.rigidBody.scale.x).toEqual(1)
expect(rectangle.rigidBody.scale.y).toEqual(1)
expect(rectangle.rigidBody.collider.shape).toEqual(FShapes.RECTANGLE)
// Validate scaleX and scaleY
rectangle.rigidBody.scaleX = 2
rectangle.rigidBody.scaleY = 3
expect(rectangle.rigidBody.scaleX).toEqual(2)
expect(rectangle.rigidBody.scaleY).toEqual(3)
expect(rectangle.rigidBody.scaleX).toEqual(rectangle.rigidBody.scale.x)
expect(rectangle.rigidBody.scaleY).toEqual(rectangle.rigidBody.scale.y)
})

it('should create a rigidBody with custom transforms', () => {
Expand Down Expand Up @@ -156,4 +163,34 @@ describe('fRigidBody', () => {
expect(rectangle.rigidBody.scale.x).toEqual(4)
expect(rectangle.rigidBody.scale.y).toEqual(4)
})

it('should update rigidBody transforms with attached component transforms', () => {
const rectangle = new FRectangle(scene, {
position: { x: 1, y: 1 },
rotation: 0.1,
scale: { x: 1, y: 0.5 },
})
rectangle.initRigidBody({
position: { x: -2, y: 3 },
rotation: 0.3,
scale: { x: 2, y: 2 },
})
expect(rectangle.rigidBody).toBeDefined()
// Validate transforms
expect(rectangle.rigidBody.position.x).toEqual(-1)
expect(rectangle.rigidBody.position.y).toEqual(4)
expect(rectangle.rigidBody.rotation).closeTo(0.4, 0.0001)
expect(rectangle.rigidBody.scale.x).toEqual(2)
expect(rectangle.rigidBody.scale.y).toEqual(1)
// Modify component transforms
rectangle.setPosition({ x: 2, y: 2 })
rectangle.setRotation(0.2)
rectangle.setScale({ x: 3, y: 4 })
// Validate transforms
expect(rectangle.rigidBody.position.x).toEqual(0)
expect(rectangle.rigidBody.position.y).toEqual(5)
expect(rectangle.rigidBody.rotation).closeTo(0.5, 0.0001)
expect(rectangle.rigidBody.scale.x).toEqual(6)
expect(rectangle.rigidBody.scale.y).toEqual(8)
})
})

0 comments on commit 2ddec82

Please sign in to comment.