Rotate level based on device rotation

Post Reply
inzombiak
Posts: 16
Joined: Wed Nov 25, 2015 3:34 pm

Rotate level based on device rotation

Post by inzombiak »

Hi,

I'm currently using Bullet to develop a game for the iOS. My goal is to rotate the level based of the devices rotatation. I've managed to do this by setting the levels rotation directly using setWorldTransform. While this gets the job done it doesn't interact with other object that are placed on it.

I know this is because of the way I'm rotating the board. Since I'm not applying and forces, objects can end up inside the game board or underneath it if I rotate too fast.

I've thought of applying a force to the board based on speed of rotation of the device, but it seems messy. The force would have to rely on the shape of the board and objects on it, right? The more object the more force needed to rotate it the right amount.
Another idea was rotating the camera and the vector of gravity, but that would cause issues with objects that shouldn't be rotated.

Any help or guidance would be appreciated.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Rotate level based on device rotation

Post by benelot »

Hi inzombiak,

I am not entirely sure what the desired effect should be. You say you want to rotate the level/board based on the device rotation. What you would want to get is a board rotation and all the objects on it rotate with it? I see that you also want to change the gravity vector. But what you intend to do then is a simple change of camera view? I am not entirely sure.

The problem you are facing with the objects ending up inside the board could be mitigated with a higher time step (more performance needed), but I think you first have to clarify what you intend to do.
inzombiak
Posts: 16
Joined: Wed Nov 25, 2015 3:34 pm

Re: Rotate level based on device rotation

Post by inzombiak »

Sorry, been struggling with it for a while so its very simple in my head.

Imagine you have a maze and there is a ball in it. You have to get the ball into a hole by rotating the maze around the x and z axes. The amount you rotate the board by is based on the iOS devices alignment.

The way I'm doing this now is I get the devices orientation, then directly change the rotation of the level by changing its transform matrix. Since I have no control over the maximum speed the device can be rotated at, very high speeds result in object on the plane either getting stuck inside it or falling underneath it.

EDIT: Added rotation code

Code: Select all

     if(!m_body || !m_owner)
           return;
    
    btQuaternion att(rotateEuler.x, rotateEuler.z, rotateEuler.y);
    btTransform trans;
    auto ms = m_body->getMotionState();
    if(!ms)
    {
        std::cout << "NO MOTION STATE" <<std::endl;
        return;
    }

    trans.setIdentity();
    ms->getWorldTransform(trans);
    trans.setRotation(att);
    m_body->setCenterOfMassTransform(trans);
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Rotate level based on device rotation

Post by benelot »

Ok, now it is clear to me. I had this issue as well with one of my examples for the bullet physics example browser. It is bad you can not only change view angle and gravity angle, that would easily solve it. You could calculate the transform of the ball as it is influenced by the board. Then you can directly edit the worldtransform of the ball to correct for the tilt. It is basically the vector between center of tilt and the ball rotated around the angle of the tilt. That will then be your new position for the ball. But using this method, you would not be able to make a ball jump off the board by tilting (I have no idea if this is necessary).
inzombiak
Posts: 16
Joined: Wed Nov 25, 2015 3:34 pm

Re: Rotate level based on device rotation

Post by inzombiak »

Sorry for the late reply.

The gravity/camera rotation thing worked. Now I need to apply force to the objects based on tilt speed. I'm not sure if itll work but the idea is to loop over all non static bodies and apply a force to each one based on tilt direction. Ill update this post with my full code once I'm done.
Post Reply