Entity Colliders
Add Circle Collider to the Bird
Now we need to update the Bird class to setup a circle collider:
+import { CircleCollider } from "#/components/circle-collider";2import { SpriteAnimation } from "#/components/sprite-animation";3import { SpriteData } from "#/components/sprite-data";4import { Vector2d } from "#/components/vector2d";56type BirdOptions = {7spriteSheet: HTMLImageElement;8spriteData: SpriteData;9position: Vector2d;10flapAnimation: SpriteAnimation;+circlCollider: CircleCollider;12};1314export class Bird {15state = BirdState.Idle;16spriteSheet: HTMLImageElement;17spriteData: SpriteData;18position: Vector2d;19flapAnimation: SpriteAnimation;20velocity = new Vector2d();+circleCollider: CircleCollider;2223// ...2425constructor(options: BirdOptions) {26this.spriteSheet = options.spriteSheet;27this.spriteData = options.spriteData;28this.position = options.position;29this.flapAnimation = options.flapAnimation;+this.circleCollider = options.circlCollider;31}3233// ...34}
Then update main.ts
:
1import spriteSheetUrl from "#/assets/image/spritesheet.png";+import { CircleCollider } from "#/components/circle-collider";3import { SpriteAnimation } from "#/components/sprite-animation";45// ...67const bird = new Bird({8spriteSheet: spriteSheet,9position: new Vector2d(config.gameWidth / 4, config.gameHeight / 2),10spriteData: new SpriteData(11spriteMap.bird.idle.sourceX,12spriteMap.bird.idle.sourceY,13spriteMap.bird.idle.width,14spriteMap.bird.idle.height15),16flapAnimation: new SpriteAnimation(17new SpriteAnimationDetails(18spriteMap.bird.animations.flap.sourceX,19spriteMap.bird.animations.flap.sourceY,20spriteMap.bird.animations.flap.width,21spriteMap.bird.animations.flap.height,22spriteMap.bird.animations.flap.frameWidth,23spriteMap.bird.animations.flap.frameHeight24),250.326),+circlCollider: new CircleCollider(0, 0, 12),28});2930// ...
Add Box Collider to the Ground
Let's update the Ground constructor to take a box collider, then update the instance.
+import { BoxCollider } from "#/components/box-collider";2import { SpriteData } from "#/components/sprite-data";3import { Vector2d } from "#/components/vector2d";45type GroundOptions = {+boxCollider: BoxCollider;7position: Vector2d;8spriteData: SpriteData;9spriteSheet: HTMLImageElement;1011/**12* This is in pixels **per frame**.13*/14scrollSpeed: number;15};1617export class Ground {+boxCollider: BoxCollider;19position: Vector2d;20spriteData: SpriteData;21spriteSheet: HTMLImageElement;22scrollSpeed: number;2324// Track the current scroll position separately from the position.25public scrollPositionX = 0;2627constructor(options: GroundOptions) {+this.boxCollider = options.boxCollider;29this.position = options.position;30this.spriteData = options.spriteData;31this.spriteSheet = options.spriteSheet;32this.scrollSpeed = options.scrollSpeed;33}3435// ...36}
1import spriteSheetUrl from "#/assets/image/spritesheet.png";+import { BoxCollider } from "#/components/box-collider";3import { CircleCollider } from "#/components/circle-collider";45// ...67const ground = new Ground({+boxCollider: new BoxCollider(+0,+0,+spriteMap.ground.width,+spriteMap.ground.height+),14position: new Vector2d(0, config.gameHeight - spriteMap.ground.height),15spriteData: spriteMap.ground,16spriteSheet: spriteSheet,17scrollSpeed: 120,18});
Next, let's add support for toggling the rendering of the colliders when we press the d
key. It would be nice to get a little visual feedback on whether or not our colliders look correct.