I'm using the Javascript version of Box2D. (But this is probably a basic Box2D usage issue, as I just got started, so don't be phased if you are unfamiliar with it.)
I just downloaded Box2D and to understand the code base better I tried to run the example code at http://box2d-js.sourceforge.net/.
The example code is very basic, and I simplified it even further. It creates a world with no gravity, adds a circle to it, and updates the world every 500 milliseconds. I also copied over some basic "drawing" code.
When I try to run the code, the first "step" successfully creates and displays a circle at (100, 100). However, at every step following that, the circles position is set to (NaN, NaN) and nothing displays.
I've tracked the problem down to the CircleShape class. It appears that "CircleShape.m_body.m_position" is set to (NaN, NaN) even though "CircleShape.m_position" is set to (100, 100) as it's supposed to be. As a new user I'm not sure what this means, but NaN does not seem like a very good value for a position object to have.
My world setup code is below (for those who don't know javascript very well, I hope the Box2D patterns are still clear). I would greatly appreciate any help -- I just got started with Box2D and it looks like an incredible piece of software, but I can't even get a basic program off the ground 🙁 Thanks!
I just downloaded Box2D and to understand the code base better I tried to run the example code at http://box2d-js.sourceforge.net/.
The example code is very basic, and I simplified it even further. It creates a world with no gravity, adds a circle to it, and updates the world every 500 milliseconds. I also copied over some basic "drawing" code.
When I try to run the code, the first "step" successfully creates and displays a circle at (100, 100). However, at every step following that, the circles position is set to (NaN, NaN) and nothing displays.
I've tracked the problem down to the CircleShape class. It appears that "CircleShape.m_body.m_position" is set to (NaN, NaN) even though "CircleShape.m_position" is set to (100, 100) as it's supposed to be. As a new user I'm not sure what this means, but NaN does not seem like a very good value for a position object to have.
My world setup code is below (for those who don't know javascript very well, I hope the Box2D patterns are still clear). I would greatly appreciate any help -- I just got started with Box2D and it looks like an incredible piece of software, but I can't even get a basic program off the ground 🙁 Thanks!
Code:
var worldAABB = new b2AABB();
worldAABB.minVertex.Set(-1000, -1000);
worldAABB.maxVertex.Set(1000, 1000);
var gravity = new b2Vec2(0, 0);
var doSleep = true;
var world = new b2World(worldAABB, gravity, doSleep);
var circleSd = new b2CircleDef();
circleSd.density = 1.0;
circleSd.radius = 20;
circleSd.restitution = 1.0;
circleSd.friction = 0.1;
var circleBd = new b2BodyDef();
circleBd.AddShape(circleSd);
circleBd.position.Set(100,100);
var circleBody = world.CreateBody(circleBd);
updateTime = 500
setInterval(function(){
c.clearRect(0, 0, canvas.width, canvas.height)
drawWorld(world, c)
world.Step()
}, updateTime)
worldAABB.minVertex.Set(-1000, -1000);
worldAABB.maxVertex.Set(1000, 1000);
var gravity = new b2Vec2(0, 0);
var doSleep = true;
var world = new b2World(worldAABB, gravity, doSleep);
var circleSd = new b2CircleDef();
circleSd.density = 1.0;
circleSd.radius = 20;
circleSd.restitution = 1.0;
circleSd.friction = 0.1;
var circleBd = new b2BodyDef();
circleBd.AddShape(circleSd);
circleBd.position.Set(100,100);
var circleBody = world.CreateBody(circleBd);
updateTime = 500
setInterval(function(){
c.clearRect(0, 0, canvas.width, canvas.height)
drawWorld(world, c)
world.Step()
}, updateTime)