Question

Working on a Jbox2D program I created 2 objects a rectangle at (0,10) meters and 10 Meters Wide and 1 meter wide, and a Ball at (1,0) that has a radius of 0.5f Meters

//in RectangleObject Class
PolygonShape cs = new PolygonShape();
cs.setAsBox(width, height);

//In CircleObject Class
CircleShape cs = new CircleShape();
cs.m_radius = radius;  

When my program runs the ball moves toward the platform and hits the rectangle like I expected but the numbers I get back are not what I expect

BallX[0] : 1
BallY[0] : 7.9964995
RectX[0] : 1
RectY[0] : 10

If the X and Y are calculated from the center of the ball then the ball should only be 0.5 away from the box at Y = 9.5. even if it uses the Diameter it should still be at most 1 meter away at Y = 9.

anyone know why its calculating the Y to be 2 meters away when the Radius is only 0.5?

Was it helpful?

Solution

The arguments for the polygon shape are "half-width" and "half-height". It's one of the strange carry-overs from box2d (see the manual here), and not the most intuitive behavior. Try:

cs.setAsBox(width / 2, height / 2);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top