http://gamemakersgarage.com/html5/HTML5-ShareGame.php?gameID=315This is the new physics engine. Here is the code:
//This activates when your game starts. Remember! All commands and variables are case sensitive.
//world = new b2World(new b2Vec2(0, 10), true);
LoadBox2DPhysicsEngine()
world = new b2World(new b2Vec2(0, 10), true);
//world.SetContinuousPhysics(true);
var fixDef = new b2FixtureDef;
fixDef.density = 1.0;
fixDef.friction = 0.5;
fixDef.restitution = 0.2;
var bodyDef = new b2BodyDef;
//create ground
bodyDef.type = b2Body.b2_staticBody;
// positions the center of the object (not upper left!)
bodyDef.position.x = GetGameWidth() / 2 / SCALE;
bodyDef.position.y = (GetGameHeight()-50) / SCALE;
fixDef.shape = new b2PolygonShape;
// half width, half height. eg actual height here is 1 unit
fixDef.shape.SetAsBox((600 / SCALE) / 2, (10/SCALE) / 2);
world.CreateBody(bodyDef).CreateFixture(fixDef);
//create circle
bodyDef.type = b2Body.b2_dynamicBody;
//bodyDef.bullet = true;
fixDef.shape = new b2CircleShape(20 / SCALE);
bodyDef.position.x = 250 / SCALE;
bodyDef.position.y = 250 / SCALE;
bike1 = world.CreateBody(bodyDef);
bike1.CreateFixture(fixDef)
bodyDef.position.x = 300 / SCALE;
bodyDef.position.y = 250 / SCALE;
bike2 = world.CreateBody(bodyDef);
bike2.CreateFixture(fixDef)
var joint = new b2DistanceJointDef();
joint.Initialize(bike1, bike2, new b2Vec2(250 / SCALE, 250 / SCALE), new b2Vec2(300 / SCALE, 250 / SCALE));
this.world.CreateJoint(joint);
//setup debug draw
var debugDraw = new b2DebugDraw();
debugDraw.SetSprite(GetMainContext());
debugDraw.SetDrawScale(SCALE);
debugDraw.SetFillAlpha(0.3);
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
world.SetDebugDraw(debugDraw);
myTimer = new Timer("GameTimer()", 1.0/60.0)
This code first initializes the physics engine. Then it creates the world, adds the ground, makes the two wheels of the bike and connects them by a distance joint. Then it sets up the debug drawing and starts the game timer.
You guys think this is too complex?
I do.
I'm gonna need some helping thinking of how to turn this complexity into fluff commands that still allow the great customizability.
Now when you create an object in the physics world you must:
(1) Define a body definition. Includes type and position.
(2) Define a fixture definition. Fixtures contain the attributes like density, friction, and restitution.
(3) Add shapes to the fixture definition. These shapes can be simple like a circle or square or complex like a bunch of lines or a polygon. Fixtures can hold as many shapes as you want to add.
(4) Build the completed object by running the body definition to the world.
(5) Run the fixture definition through the completed object and thus giving your object a form.
And that's how you make an object. This method allows lots of customizability.
Then you can make joints and connect objects with joints.
And there's also ways to detect collisions with objects and even to specify if objects can collide with certain other objects.
And you can even apply force and torque to objects to make them behave differently.
Now...
If you wanted to have the simplest yet most customizable commands to accomplish all of the above, what would those commands look like?