added basic physics collision checks

This commit is contained in:
Joe Weaver 2020-08-17 20:09:41 -04:00
parent f0bb31f61e
commit e6d4d75cd5
17 changed files with 313 additions and 55 deletions

View File

@ -1,6 +1,7 @@
import Scene from "../Scene"; import Scene from "../Scene";
import Viewport from "../../SceneGraph/Viewport"; import Viewport from "../../SceneGraph/Viewport";
import CanvasItem from "../../Nodes/CanvasNode" import CanvasItem from "../../Nodes/CanvasNode"
import PlayerSprite from "../../Nodes/PlayerSprite";
export default class CanvasNodeFactory { export default class CanvasNodeFactory {
private scene: Scene; private scene: Scene;

View File

@ -0,0 +1,23 @@
import Scene from "../Scene";
import Viewport from "../../SceneGraph/Viewport";
import PhysicsNode from "../../Physics/PhysicsNode";
import PhysicsManager from "../../Physics/PhysicsManager";
export default class PhysicsNodeFactory {
private scene: Scene;
private physicsManager: PhysicsManager;
constructor(scene: Scene, physicsManager: PhysicsManager){
this.scene = scene;
this.physicsManager = physicsManager;
}
add<T extends PhysicsNode>(constr: new (...a: any) => T, ...args: any): T {
let instance = new constr(...args);
instance.init(this.scene);
instance.addManager(this.physicsManager);
instance.create();
this.physicsManager.add(instance);
return instance;
}
}

View File

@ -20,6 +20,7 @@ export default class TilemapFactory {
// For each of the layers in the tilemap, create a tilemap // For each of the layers in the tilemap, create a tilemap
for(let layer of tilemapData.layers){ for(let layer of tilemapData.layers){
let tilemap = new constr(tilemapData, layer); let tilemap = new constr(tilemapData, layer);
tilemap.init(this.scene);
// Add to scene // Add to scene
this.scene.addTilemap(tilemap); this.scene.addTilemap(tilemap);

View File

@ -7,12 +7,15 @@ import CanvasNodeFactory from "./Factories/CanvasNodeFactory";
import GameState from "./GameState"; import GameState from "./GameState";
import Tilemap from "../Nodes/Tilemap"; import Tilemap from "../Nodes/Tilemap";
import TilemapFactory from "./Factories/TilemapFactory"; import TilemapFactory from "./Factories/TilemapFactory";
import PhysicsManager from "../Physics/PhysicsManager";
import PhysicsNodeFactory from "./Factories/PhysicsNodeFactory";
export default class Scene { export default class Scene {
private gameState: GameState; private gameState: GameState;
private viewport: Viewport private viewport: Viewport
private parallax: Vec2; private parallax: Vec2;
sceneGraph: SceneGraph; sceneGraph: SceneGraph;
private physicsManager: PhysicsManager;
private tilemaps: Array<Tilemap>; private tilemaps: Array<Tilemap>;
private paused: boolean; private paused: boolean;
private hidden: boolean; private hidden: boolean;
@ -20,6 +23,7 @@ export default class Scene {
// Factories // Factories
public canvasNode: CanvasNodeFactory; public canvasNode: CanvasNodeFactory;
public tilemap: TilemapFactory; public tilemap: TilemapFactory;
public physics: PhysicsNodeFactory;
constructor(viewport: Viewport, gameState: GameState){ constructor(viewport: Viewport, gameState: GameState){
this.gameState = gameState; this.gameState = gameState;
@ -29,10 +33,12 @@ export default class Scene {
this.tilemaps = new Array<Tilemap>(); this.tilemaps = new Array<Tilemap>();
this.paused = false; this.paused = false;
this.hidden = false; this.hidden = false;
this.physicsManager = new PhysicsManager();
// Factories // Factories
this.canvasNode = new CanvasNodeFactory(this, this.viewport); this.canvasNode = new CanvasNodeFactory(this, this.viewport);
this.tilemap = new TilemapFactory(this, this.viewport); this.tilemap = new TilemapFactory(this, this.viewport);
this.physics = new PhysicsNodeFactory(this, this.physicsManager);
} }
setPaused(pauseValue: boolean): void { setPaused(pauseValue: boolean): void {
@ -84,6 +90,7 @@ export default class Scene {
update(deltaT: number): void { update(deltaT: number): void {
if(!this.paused){ if(!this.paused){
this.viewport.update(deltaT); this.viewport.update(deltaT);
this.physicsManager.update(deltaT);
this.sceneGraph.update(deltaT); this.sceneGraph.update(deltaT);
} }
} }

View File

@ -4,17 +4,12 @@ import Scene from "../GameState/Scene";
export default abstract class CanvasNode extends GameNode{ export default abstract class CanvasNode extends GameNode{
protected size: Vec2; protected size: Vec2;
protected scene: Scene;
constructor(){ constructor(){
super(); super();
this.size = new Vec2(0, 0); this.size = new Vec2(0, 0);
} }
init(scene: Scene){
this.scene = scene;
}
getSize(): Vec2 { getSize(): Vec2 {
return this.size; return this.size;
} }

View File

@ -4,18 +4,28 @@ import Vec2 from "../DataTypes/Vec2";
import Map from "../DataTypes/Map"; import Map from "../DataTypes/Map";
import Receiver from "../Events/Receiver"; import Receiver from "../Events/Receiver";
import GameEvent from "../Events/GameEvent"; import GameEvent from "../Events/GameEvent";
import Scene from "../GameState/Scene";
export default abstract class GameNode{ export default abstract class GameNode{
private eventQueue: EventQueue; private eventQueue: EventQueue;
protected input: InputReceiver; protected input: InputReceiver;
protected position: Vec2; protected position: Vec2;
private receiver: Receiver; private receiver: Receiver;
protected scene: Scene;
constructor(){ constructor(){
this.eventQueue = EventQueue.getInstance(); this.eventQueue = EventQueue.getInstance();
this.input = InputReceiver.getInstance(); this.input = InputReceiver.getInstance();
this.position = new Vec2(0, 0); this.position = new Vec2(0, 0);
} }
init(scene: Scene){
this.scene = scene;
}
getScene(): Scene {
return this.scene;
}
getPosition(): Vec2 { getPosition(): Vec2 {
return this.position; return this.position;

View File

@ -1,37 +0,0 @@
import CanvasNode from "./CanvasNode";
import Vec2 from "../DataTypes/Vec2";
import Debug from "../Debug/Debug";
export default class Player extends CanvasNode{
velocity: Vec2;
speed: number;
debug: Debug;
constructor(){
super();
this.velocity = new Vec2(0, 0);
this.speed = 300;
this.size = new Vec2(50, 50);
this.debug = Debug.getInstance();
};
update(deltaT: number): void {
let dir = new Vec2(0, 0);
dir.x += this.input.isPressed('a') ? -1 : 0;
dir.x += this.input.isPressed('d') ? 1 : 0;
dir.y += this.input.isPressed('s') ? 1 : 0;
dir.y += this.input.isPressed('w') ? -1 : 0;
dir.normalize();
this.velocity = dir.scale(this.speed);
this.position = this.position.add(this.velocity.scale(deltaT));
this.debug.log("player", "Player Pos: " + this.position.toFixed());
}
render(ctx: CanvasRenderingContext2D, origin: Vec2){
ctx.fillStyle = "#FF0000";
ctx.fillRect(this.position.x - origin.x, this.position.y - origin.y, this.size.x, this.size.y);
}
}

18
src/Nodes/PlayerSprite.ts Normal file
View File

@ -0,0 +1,18 @@
import CanvasNode from "./CanvasNode";
import Vec2 from "../DataTypes/Vec2";
import Debug from "../Debug/Debug";
export default class Player extends CanvasNode{
debug: Debug;
constructor(){
super();
};
update(deltaT: number): void {}
render(ctx: CanvasRenderingContext2D, origin: Vec2){
ctx.fillStyle = "#FF0000";
ctx.fillRect(this.position.x - origin.x, this.position.y - origin.y, this.size.x, this.size.y);
}
}

View File

@ -15,7 +15,7 @@ export default abstract class Tilemap extends GameNode {
super(); super();
this.tilesets = new Array<Tileset>(); this.tilesets = new Array<Tileset>();
this.worldSize = new Vec2(0, 0); this.worldSize = new Vec2(0, 0);
this.init(tilemapData, layerData); this.parseTilemapData(tilemapData, layerData);
} }
getTilesets(): Tileset[] { getTilesets(): Tileset[] {
@ -36,7 +36,7 @@ export default abstract class Tilemap extends GameNode {
/** /**
* Sets up the tileset using the data loaded from file * Sets up the tileset using the data loaded from file
*/ */
abstract init(tilemapData: TiledTilemapData, layerData: TiledLayerData): void; abstract parseTilemapData(tilemapData: TiledTilemapData, layerData: TiledLayerData): void;
abstract render(ctx: CanvasRenderingContext2D, origin: Vec2, viewportSize: Vec2): void; abstract render(ctx: CanvasRenderingContext2D, origin: Vec2, viewportSize: Vec2): void;
} }

View File

@ -5,7 +5,7 @@ import Tileset from "../../DataTypes/Tilesets/Tileset";
export default class OrthogonalTilemap extends Tilemap { export default class OrthogonalTilemap extends Tilemap {
init(tilemapData: TiledTilemapData, layer: TiledLayerData): void { parseTilemapData(tilemapData: TiledTilemapData, layer: TiledLayerData): void {
this.worldSize.set(tilemapData.width, tilemapData.height); this.worldSize.set(tilemapData.width, tilemapData.height);
this.data = layer.data; this.data = layer.data;
tilemapData.tilesets.forEach(tilesetData => this.tilesets.push(new Tileset(tilesetData))); tilemapData.tilesets.forEach(tilesetData => this.tilesets.push(new Tileset(tilesetData)));

View File

@ -0,0 +1,29 @@
import Collider from "./Collider";
import Vec2 from "../../DataTypes/Vec2";
export default class AABB extends Collider {
isCollidingWith(other: Collider): boolean {
if(other instanceof AABB){
if(other.position.x > this.position.x && other.position.x < this.position.x + this.size.x){
return other.position.y > this.position.y && other.position.y < this.position.y + this.size.y;
}
}
return false;
}
willCollideWith(other: Collider, thisVel: Vec2, otherVel: Vec2): boolean {
if(other instanceof AABB){
let thisPos = new Vec2(this.position.x + thisVel.x, this.position.y + thisVel.y);
let otherPos = new Vec2(other.position.x + otherVel.x, other.position.y + otherVel.y);
if(otherPos.x > thisPos.x && otherPos.x < thisPos.x + this.size.x){
return otherPos.y > thisPos.y && otherPos.y < thisPos.y + this.size.y;
}
}
return false;
}
update(deltaT: number): void {}
}

View File

@ -0,0 +1,18 @@
import GameNode from "../../Nodes/GameNode";
import Vec2 from "../../DataTypes/Vec2";
export default abstract class Collider extends GameNode {
protected size: Vec2;
getSize(): Vec2 {
return this.size;
}
setSize(size: Vec2): void {
this.size = size;
}
abstract isCollidingWith(other: Collider): boolean;
abstract willCollideWith(other: Collider, thisVel: Vec2, otherVel: Vec2): boolean;
}

View File

@ -0,0 +1,89 @@
import PhysicsNode from "./PhysicsNode";
import Vec2 from "../DataTypes/Vec2";
export default class PhysicsManager {
physicsNodes: Array<PhysicsNode>;
movements: Array<MovementData>;
constructor(){
this.physicsNodes = new Array();
this.movements = new Array();
}
add(node: PhysicsNode): void {
this.physicsNodes.push(node);
}
addMovement(node: PhysicsNode, velocity: Vec2){
this.movements.push(new MovementData(node, velocity));
}
update(deltaT: number): void {
for(let node of this.physicsNodes){
node.update(deltaT);
}
let staticSet = new Array<PhysicsNode>();
let dynamicSet = new Array<PhysicsNode>();
// TODO: REALLY bad, the physics system has to be improved, but that isn't the focus for now
for(let node of this.physicsNodes){
if(node.isMoving){
dynamicSet.push(node);
node.isMoving = false;
} else {
staticSet.push(node);
}
}
// For now, we will only have the moving player, don't bother checking for collisions with other moving things
for(let movingNode of dynamicSet){
// Get velocity of node
let velocity = null;
for(let data of this.movements){
if(data.node === movingNode){
velocity = new Vec2(data.velocity.x, data.velocity.y);
}
}
for(let staticNode of staticSet){
if(movingNode.getCollider().willCollideWith(staticNode.getCollider(), velocity, new Vec2(0, 0))){
this.handleCollision(movingNode, staticNode, velocity);
}
}
movingNode.finishMove(velocity);
}
// Reset movements
this.movements = new Array();
}
handleCollision(movingNode: PhysicsNode, staticNode: PhysicsNode, velocity: Vec2){
let ASize = movingNode.getCollider().getSize();
let A = new Vec2(movingNode.getPosition().x + ASize.x, movingNode.getPosition().y + ASize.y);
let BSize = staticNode.getCollider().getSize();
let B = new Vec2(staticNode.getPosition().x + BSize.x, staticNode.getPosition().y + BSize.y);
let firstContact = new Vec2(0, 0);
firstContact.x = (B.x-(BSize.x/2) - (A.x + (ASize.x/2)))/(velocity.x - 0);
firstContact.y = (B.y-(BSize.y/2) - (A.y + (ASize.y/2)))/(velocity.y - 0);
if(firstContact.x < 1 || firstContact.y < 1){
// We collided
let firstCollisionTime = Math.min(firstContact.x, firstContact.y);
velocity.scale(firstCollisionTime);
}
}
}
class MovementData{
node: PhysicsNode;
velocity: Vec2;
constructor(node: PhysicsNode, velocity: Vec2){
this.node = node;
this.velocity = velocity;
}
}

View File

@ -0,0 +1,44 @@
import Collider from "./Colliders/Collider";
import GameNode from "../Nodes/GameNode";
import PhysicsManager from "./PhysicsManager";
import Vec2 from "../DataTypes/Vec2";
export default abstract class PhysicsNode extends GameNode {
protected collider: Collider = null;
protected children: Array<GameNode>;
private manager: PhysicsManager;
isMoving: boolean;
constructor(){
super();
this.children = new Array();
this.isMoving = false;
}
addManager(manager: PhysicsManager): void {
this.manager = manager;
}
isCollidable(): boolean {
return this.collider !== null;
}
getCollider(): Collider {
return this.collider;
}
move(velocity: Vec2): void {
this.isMoving = true;
this.manager.addMovement(this, velocity);
}
finishMove(velocity: Vec2): void {
this.position.add(velocity);
for(let child of this.children){
child.getPosition().add(velocity);
}
}
abstract create(): void;
}

44
src/Player.ts Normal file
View File

@ -0,0 +1,44 @@
import PhysicsNode from "./Physics/PhysicsNode";
import Vec2 from "./DataTypes/Vec2";
import Debug from "./Debug/Debug";
import AABB from "./Physics/Colliders/AABB";
import PlayerSprite from "./Nodes/PlayerSprite";
export default class Player extends PhysicsNode {
velocity: Vec2;
speed: number;
debug: Debug;
constructor(){
super();
this.velocity = new Vec2(0, 0);
this.speed = 300;
this.collider = new AABB();
this.collider.setSize(new Vec2(50, 50));
this.position = new Vec2(0, 0);
this.debug = Debug.getInstance();
}
create(): void {
let sprite = this.scene.canvasNode.add(PlayerSprite);
sprite.setPosition(this.position);
sprite.setSize(50, 50);
this.children.push(sprite);
}
update(deltaT: number): void {
let dir = new Vec2(0, 0);
dir.x += this.input.isPressed('a') ? -1 : 0;
dir.x += this.input.isPressed('d') ? 1 : 0;
dir.y += this.input.isPressed('s') ? 1 : 0;
dir.y += this.input.isPressed('w') ? -1 : 0;
dir.normalize();
this.velocity = dir.scale(this.speed);
this.move(this.velocity.scale(deltaT));
this.debug.log("player", "Player Pos: " + this.position.toFixed());
}
}

View File

@ -1,6 +1,5 @@
import GameLoop from "./Loop/GameLoop"; import GameLoop from "./Loop/GameLoop";
import Scene from "./GameState/Scene"; import Player from "./Player";
import Player from "./Nodes/Player";
import UIElement from "./Nodes/UIElement"; import UIElement from "./Nodes/UIElement";
import ColoredCircle from "./Nodes/ColoredCircle"; import ColoredCircle from "./Nodes/ColoredCircle";
import Color from "./Utils/Color"; import Color from "./Utils/Color";
@ -24,7 +23,7 @@ function main(){
pauseMenu.setParallax(0, 0); pauseMenu.setParallax(0, 0);
// Initialize GameObjects // Initialize GameObjects
let player = mainScene.canvasNode.add(Player); let player = mainScene.physics.add(Player);
mainScene.getViewport().follow(player); mainScene.getViewport().follow(player);
let recordButton = uiLayer.canvasNode.add(Button); let recordButton = uiLayer.canvasNode.add(Button);
@ -79,11 +78,7 @@ function main(){
pauseMenu.disable(); pauseMenu.disable();
} }
for(let i = 0; i < 10; i++){ mainScene.tilemap.add(OrthogonalTilemap, "assets/tilemaps/MultiLayer.json");
mainScene.canvasNode.add(ColoredCircle);
}
backgroundScene.tilemap.add(OrthogonalTilemap, "assets/tilemaps/MultiLayer.json");
for(let i = 0; i < 30; i++){ for(let i = 0; i < 30; i++){
let cc = foregroundLayer.canvasNode.add(ColoredCircle); let cc = foregroundLayer.canvasNode.add(ColoredCircle);

View File

@ -1,7 +1,10 @@
{ {
"files": [ "files": [
"src/main.ts", "src/main.ts",
"src/Player.ts",
"src/DataTypes/Tilesets/TiledData",
"src/DataTypes/Tilesets/Tileset",
"src/DataTypes/Collection.ts", "src/DataTypes/Collection.ts",
"src/DataTypes/Map.ts", "src/DataTypes/Map.ts",
"src/DataTypes/Queue.ts", "src/DataTypes/Queue.ts",
@ -9,10 +12,15 @@
"src/DataTypes/Vec2.ts", "src/DataTypes/Vec2.ts",
"src/DataTypes/Vec4.ts", "src/DataTypes/Vec4.ts",
"src/Debug/Debug.ts",
"src/Events/EventQueue.ts", "src/Events/EventQueue.ts",
"src/Events/GameEvent.ts", "src/Events/GameEvent.ts",
"src/Events/Receiver.ts", "src/Events/Receiver.ts",
"src/GameState/Factories/CanvasNodeFactory.ts",
"src/GameState/Factories/PhysicsNodeFactory.ts",
"src/GameState/Factories/TilemapFactory.ts",
"src/GameState/GameState.ts", "src/GameState/GameState.ts",
"src/GameState/Scene.ts", "src/GameState/Scene.ts",
@ -21,20 +29,33 @@
"src/Loop/GameLoop.ts", "src/Loop/GameLoop.ts",
"src/Nodes/Tilemaps/OrgthogonalTilemap.ts",
"src/Nodes/UIElements/Button.ts",
"src/Nodes/UIElements/Label.ts",
"src/Nodes/CanvasNode.ts",
"src/Nodes/ColoredCircle.ts", "src/Nodes/ColoredCircle.ts",
"src/Nodes/GameNode.ts", "src/Nodes/GameNode.ts",
"src/Nodes/Player.ts", "src/Nodes/PlayerSprite.ts",
"src/Nodes/Tilemap.ts",
"src/Nodes/UIElement.ts", "src/Nodes/UIElement.ts",
"src/Physics/Colliders/AABB.ts",
"src/Physics/Colliders/Collider.ts",
"src/Physics/PhysicsManager.ts",
"src/Physics/PhysicsNode.ts",
"src/Playback/Recorder.ts", "src/Playback/Recorder.ts",
"src/ResourceManager/ResourceManager.ts",
"src/SceneGraph/SceneGraph.ts", "src/SceneGraph/SceneGraph.ts",
"src/SceneGraph/SceneGraphArray.ts", "src/SceneGraph/SceneGraphArray.ts",
"src/SceneGraph/Viewport.ts", "src/SceneGraph/Viewport.ts",
"src/Utils/Color.ts", "src/Utils/Color.ts",
"src/Utils/MathUtils.ts", "src/Utils/MathUtils.ts",
"src/Utils/RandUtils.ts" "src/Utils/RandUtils.ts",
"src/Utils/StringUtils.ts"
], ],
"compilerOptions": { "compilerOptions": {
"noImplicitAny": true, "noImplicitAny": true,