3
\$\begingroup\$

I'm new at Box2D and I am making a simple game where you control the rotation of the maze and the gravity leads the ball to the exit.

The game looks like: enter image description here

The problem: I want to rotate the maze, and I'm doing it well. It is a kinematic object, so it will only move when I want. But it always rotate on a static pivot, I think that's it's center of mass. Rotating the maze, the ball hits the walls and when too further to the center, it becomes hard to control the movement of the ball by rotating the maze as the centrifugue force is too high for the ball.

What I want: I want to change the maze's rotation pivot to the ball's position, every game loop/frame, so it will rotate around the ball always and I will be able to use only gravity, not centrifuge forces, to lead the ball to the exit.

What I tried: I already tried to find a function to change the pivot center, or the center of mass, but I only found to change the mass proprieties. It changes the center of mass only if it was a dynamic body. Even the kinematic body having the same localCenter.x and localCenter.y proprieties as the dynamic body, it remains on (0.0 ,0.0) when trying to change it. And when changing the localCenter on the dynamic body, it doesn't rotate at this point anyway, so I'm not sure what to do.

Is there any possibility to change the center of the kinematic body all the time? I'm using Box2D Vestion 2.3.2, as you can see on the picture, and Visual Studio 2013 with OpenGL.

Thanks!

\$\endgroup\$
  • 1
    \$\begingroup\$ I realized that I could change the gravity while rotating the camera together, with the maze static. It works perfect for this situation, but still, it would be useful to know how to change the center of mass of a kinematic object. \$\endgroup\$ – Kinosei Oct 3 '15 at 1:25
  • \$\begingroup\$ Kinematic bodies effectively have infinite mass. They have no mass properties such as center of mass/density etc. \$\endgroup\$ – Charanor Feb 5 '16 at 19:01
1
\$\begingroup\$

As you realized, changing the gravity can work for you.

It sounds like you may also be looking for a function that rotates a body around a point in world coordinates. I wrote a function for this in my fork of Box2D (RotateAboutWorldPoint) that I have translated to Box2D 2.3.2 for you as the following code:

void b2RotateAboutWorldPoint(b2Body& body, float32 amount, b2Vec2 worldPoint)
{
    const b2Transform xfm = body.GetTransform();
    const b2Vec2 p = xfm.p - worldPoint;
    const float32 c = cos(amount);
    const float32 s = sin(amount);
    const float32 x = p.x * c - p.y * s;
    const float32 y = p.x * s + p.y * c;
    const b2Vec2 pos = b2Vec2(x, y) + worldPoint;
    const float32 angle = xfm.q.GetAngle() + amount;
    body.SetTransform(pos, angle);
}

This can be used then to rotate the kinematic body that is your maze, around the position of the ball.

Also, in case you haven't done this already, you'll probably want to make your ball be a bullet object. That'd at least help with avoiding seeing as much non-physical behavior if you're rotating your maze using the SetTransform method. I believe an alternative (to using SetTransform) would be to make your maze be a dynamic object and use a revolute joint on it. Seems it'd still be a good idea though to make the ball be a bullet object.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.