Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm having problems with box2DWeb. I can't seem to get 2 objects on the screen at once. I can get either a static body, or a dynamic one. But when trying both at the same time it messes up and just draws the last one.

My guess it's because I reset the previous values with the new ones (obviously). I just don't know how to fix it. I am making the 2 different bodys objects, and creating them with var player/ground = new Game.Player/Game.Ground();

Dynamic:

Game.Player = function() {
fixDef.density = 1.0;
fixDef.friction = 0.5;
fixDef.restitution = 1;

this.X = 2;
this.Y = 0 / SCALE;

bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.Set(this.X, this.Y);
bodyDef.linearVelocity.Set(0, 0);

fixDef.shape = new b2PolygonShape();
fixDef.shape.SetAsBox(25 / SCALE, 25 / SCALE);

Game.World.CreateBody(bodyDef).CreateFixture(fixDef);
}

Game.Player.prototype.Draw = function() {
    b = Game.World.GetBodyList();
    var pos = b.GetPosition();

    c.save();
    c.translate(pos.x * SCALE, pos.y * SCALE);
    c.rotate(b.GetAngle());
    c.drawImage(image, -25, -25);
    c.restore();
}

var player = new Game.Player();

Static:

Game.Ground = function() {
fixDef.density = 1.0;
fixDef.friction = 0.5;
fixDef.restitution = 1;

this.X = 5;
this.Y = 0 / SCALE;

bodyDef.type = b2Body.b2_staticBody;
bodyDef.position.Set(this.X, this.Y);
bodyDef.linearVelocity.Set(0, 0);

fixDef.shape = new b2PolygonShape();
fixDef.shape.SetAsBox(25 / SCALE, 25 / SCALE);

Game.World.CreateBody(bodyDef).CreateFixture(fixDef);
}

Game.Ground.prototype.Draw = function() {
    a = Game.World.GetBodyList();
    var pos = a.GetPosition();

    c.save();
    c.translate(pos.x * SCALE, pos.y * SCALE);
    c.rotate(a.GetAngle());
    c.drawImage(image, -25, -25);
    c.restore();
}

var ground = new Game.Ground();

How can I fix this?

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.