Created
May 31, 2015 17:54
-
-
Save kevinmartinjos/0950a7aae1b1a17ca652 to your computer and use it in GitHub Desktop.
A quick experiment with box2d and p5.js to cook up a game based on the angle at which polygons collide. The idea seems promising
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //aliases for box2d stuff. Makes life easier. | |
| var b2Vec2 = Box2D.Common.Math.b2Vec2 | |
| , b2AABB = Box2D.Collision.b2AABB | |
| , b2BodyDef = Box2D.Dynamics.b2BodyDef | |
| , b2Body = Box2D.Dynamics.b2Body | |
| , b2FixtureDef = Box2D.Dynamics.b2FixtureDef | |
| , b2Fixture = Box2D.Dynamics.b2Fixture | |
| , b2World = Box2D.Dynamics.b2World | |
| , b2MassData = Box2D.Collision.Shapes.b2MassData | |
| , b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape | |
| , b2CircleShape = Box2D.Collision.Shapes.b2CircleShape | |
| , b2DebugDraw = Box2D.Dynamics.b2DebugDraw | |
| , b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef | |
| ; | |
| //inorder to convert everything to box2d measurements | |
| var SCALE = 30.0; | |
| var Globals = { | |
| world: null, | |
| renderStack: [], | |
| player: null | |
| }; | |
| //A generic unit | |
| function Unit(world, x_, y_, w_, h_, mass_){ | |
| this.x = x_; | |
| this.y = y_; | |
| this.mass = mass_; | |
| this.width = w_; | |
| this.height = h_; | |
| this.body = null; | |
| this.hitPoints = 100; | |
| this.attack = 10; | |
| var fixDef = new b2FixtureDef; | |
| fixDef.density = 1.0; | |
| fixDef.restitution = 0.2; | |
| fixDef.friction = 0.3; | |
| var bodyDef = new b2BodyDef; | |
| bodyDef.position.x = this.x/SCALE; | |
| bodyDef.position.y = this.y/SCALE; | |
| bodyDef.type = b2Body.b2_dynamicBody; | |
| fixDef.shape = new b2PolygonShape; | |
| fixDef.shape.SetAsBox(this.width/(SCALE*2), this.height/(SCALE*2)); | |
| this.body = world.CreateBody(bodyDef); | |
| this.body.CreateFixture(fixDef); | |
| //so that we can access element from box2d | |
| this.body.SetUserData(this); | |
| } | |
| Unit.prototype.render = function(){ | |
| var x = this.body.GetPosition().x; | |
| var y = this.body.GetPosition().y; | |
| push(); | |
| translate(x * SCALE, y * SCALE); | |
| rotate((this.body.GetAngle())); | |
| rect(0, 0, this.width, this.height); | |
| text(this.hitPoints, 0, 0); | |
| pop(); | |
| } | |
| function init(world){ | |
| //set boundaries | |
| var fixDef = new b2FixtureDef; | |
| fixDef.density = 1.0; | |
| fixDef.restitution = 0.2; | |
| fixDef.friction = 0.3; | |
| var bodyDef = new b2BodyDef; | |
| bodyDef.position.x = 0; | |
| bodyDef.position.y = 0; | |
| bodyDef.type = b2Body.b2_staticBody; | |
| fixDef.shape = new b2PolygonShape; | |
| fixDef.shape.SetAsBox(10/SCALE, height/(SCALE)); | |
| world.CreateBody(bodyDef).CreateFixture(fixDef); | |
| bodyDef.position.x = width/SCALE; | |
| bodyDef.position.y = 0; | |
| fixDef.shape.SetAsBox(10/SCALE, height/SCALE); | |
| world.CreateBody(bodyDef).CreateFixture(fixDef); | |
| //add player and npc | |
| var npc = new Unit(world, width/2, height/2, 50, 50, 10); | |
| var player = new Unit(world, 100, 200, 25, 25, 20); | |
| player.isPlayer = true; | |
| Globals.player = player; | |
| Globals.renderStack.push(npc); | |
| Globals.renderStack.push(player); | |
| var listener = new Box2D.Dynamics.b2ContactListener; | |
| listener.BeginContact = function(contact){ | |
| var bodyA = contact.GetFixtureA().GetBody(); | |
| var bodyB = contact.GetFixtureB().GetBody(); | |
| a = bodyA.GetUserData(); | |
| b = bodyB.GetUserData(); | |
| if(bodyA.GetAngle()%1.57 > bodyB.GetAngle()%1.57){ | |
| b.hitPoints -= int(a.attack * (bodyA.GetAngle()%1.57 - bodyB.GetAngle()%1.57)*2); | |
| } | |
| else | |
| a.hitPoints -= int(b.attack * (bodyB.GetAngle()%1.57 - bodyA.GetAngle()%1.57)*2); | |
| } | |
| world.SetContactListener(listener); | |
| } | |
| function setup(){ | |
| createCanvas(800, 700); | |
| frameRate(30); | |
| rectMode(CENTER); | |
| var world = new b2World(new b2Vec2(0, 0), true); | |
| Globals.world = world; | |
| var debugDraw = new b2DebugDraw(); | |
| debugDraw.SetSprite(document.getElementById("defaultCanvas").getContext("2d")); | |
| debugDraw.SetDrawScale(SCALE); | |
| debugDraw.SetFillAlpha(0.3); | |
| debugDraw.SetLineThickness(1.0); | |
| debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit); | |
| world.SetDebugDraw(debugDraw); | |
| init(world); | |
| } | |
| function draw(){ | |
| background(220); | |
| Globals.world.DrawDebugData(); | |
| for(var i=0; i<Globals.renderStack.length; i++){ | |
| Globals.renderStack[i].render(); | |
| } | |
| Globals.world.Step(1/60, 10, 10); | |
| } | |
| function mousePressed(){ | |
| Globals.player.body.SetAwake(true); | |
| Globals.player.body.SetLinearVelocity(new b2Vec2(mouseX/SCALE - Globals.player.body.GetPosition().x, mouseY/SCALE - Globals.player.body.GetPosition().y)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment