Tilting Table - kinematic body force exerted on rotation

Post Reply
Lucky
Posts: 4
Joined: Thu Feb 25, 2016 10:28 am

Tilting Table - kinematic body force exerted on rotation

Post by Lucky » Fri Feb 26, 2016 12:30 am

Hello everyone,

First of all, I'd like to apologize if a topic describing this already exists, but I've spent the past two weeks trying different resources Google threw up with no luck. I'm brand new to Bullet Physics with general knowledge of Linear Algebra. I'm designing a mobile maze game, which uses accelerometer input for board dynamic rotation because of which a ball would be forced to move by physics.
This is designed using a kinematic body for the 'table' and a dynamic rigid body for the ball, however, when table is rotated too quickly the ball shoots out into the sky.


I create objects with this code as part of create method

Code: Select all

        instances = new Array<Entity>();
        Entity object = constructors.get("ground").create();
        object.body.setCollisionFlags(object.body.getCollisionFlags() | btCollisionObject.CollisionFlags.CF_KINEMATIC_OBJECT);
        object.body.setHitFraction(0);
        object.body.setRollingFriction(0.1f);
        object.transform.setFromEulerAngles(0, 0, 0);
        object.body.proceedToTransform(object.transform);
        instances.add(object);
        dynamicsWorld.addRigidBody(object.body);
        object.body.setContactCallbackFlag(GROUND_FLAG);
        object.body.setContactCallbackFilter(0);

        object = constructors.get("sphere").create();
        object.transform.trn(0, 4.5f, 0);
        object.body.proceedToTransform(object.transform);
        object.body.setUserValue(instances.size);
        object.body.setCollisionFlags(object.body.getCollisionFlags() | btCollisionObject.CollisionFlags.CF_CUSTOM_MATERIAL_CALLBACK);
        object.body.setDamping(0.1f, 0.1f);
        object.body.setLinearVelocity(new Vector3(0, -1, 0));
        object.body.setFriction(0.1f);
        instances.add(object);
        dynamicsWorld.addRigidBody(object.body);
        object.body.setContactCallbackFlag(OBJECT_FLAG);
        object.body.setContactCallbackFilter(GROUND_FLAG);
        object.body.setActivationState(Collision.DISABLE_DEACTIVATION);
Here I rotate the box shape object with accelerometer in the render method

Code: Select all

       accelerometer.update();

        final float delta = Math.min(1f / 60f, Gdx.graphics.getDeltaTime());

        rotation.setEulerAngles(0, ((((accelerometer.axisY() * 10) / 10f) / 10) * 90), ((((accelerometer.axisX() * 10) / 10f) / 10) * 90));

        instances.get(0).transform.idt().rotate(rotation).translate(0, 0, 0);
        instances.get(0).body.setActivationState(Collision.ACTIVE_TAG);

        dynamicsWorld.stepSimulation(delta, 5, 1f / 120f);
My project is uploaded to this GitHub - https://github.com/Luucky/TiltMaze

Please feel free to check it out, the latest commit has code and commented out scraps of previous effort. Let me also mention that this project is my school project which must be finished in less than 30 days, meaning I would really appreciate any help at all. Thanks in advance.

Regards,
Lucky

Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Tilting Table - kinematic body force exerted on rotation

Post by Basroil » Fri Feb 26, 2016 12:43 pm

The jumping is just physics acting as it should, so what you'll probably need to do is change the "physics". Rather than rotating the box about it's center of gravity, rotate it about the marble's contact point. That should change the angle of the box without changing the position of the box relative with the ball (which is the cause of the jump)

Lucky
Posts: 4
Joined: Thu Feb 25, 2016 10:28 am

Re: Tilting Table - kinematic body force exerted on rotation

Post by Lucky » Sat Feb 27, 2016 12:59 am

Thanks a lot for the answer. I feel like this is a very good idea, however, there is no way I'm going to be able to implement this by myself, spent a good 5 hours already trying to find some code fragments that could help me. Do you think you could put me on the right track and possibly provide some key functions I need to call to achieve this?
Basroil wrote:The jumping is just physics acting as it should, so what you'll probably need to do is change the "physics". Rather than rotating the box about it's center of gravity, rotate it about the marble's contact point. That should change the angle of the box without changing the position of the box relative with the ball (which is the cause of the jump)

Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Tilting Table - kinematic body force exerted on rotation

Post by Basroil » Sat Feb 27, 2016 2:18 am

That part is all trig actually, it should be as simple as multiplying out the transform from one to the other. Basically you need to find the "contact" area (really just a close approximation should minimize issues) on the box and then extend the rotation from there to the center of gravity.

Another, slightly less intuitive approach would be to keep the box flat and just change the direction of gravity

Lucky
Posts: 4
Joined: Thu Feb 25, 2016 10:28 am

Re: Tilting Table - kinematic body force exerted on rotation

Post by Lucky » Mon Feb 29, 2016 1:41 am

Hey,

I've done the following, unfortunately I ran out of ideas of how to attempt changing the rotation origin. I would really appreciate some hints, functions, code scraps or useful documentations on how to do that. as I'm under deadline pressure. Thanks in adv

Also wouldn't the object have to return to straight position, then the rotation point would be changed and it would return to the previous rotation? Would this require me to somehow do it in a sub-step to avoid physics acting on the ball?

Code: Select all

        accelerometer.update();

        final float delta = Math.min(1f / 60f, Gdx.graphics.getDeltaTime());

        rotation.setEulerAngles(0, ((((accelerometer.axisY() * 10) / 10f) / 10) * 90), ((((accelerometer.axisX() * 10) / 10f) / 10) * 90));

        instances.get(1).body.getWorldTransform(contactMatrix);
        contactMatrix.setTranslation(contactMatrix.getTranslation(new Vector3(0, 0, 0).sub(upVector(instances.get(0).transform))));
        instances.get(0).transform.idt().rotate(rotation);
        instances.get(0).body.setCenterOfMassTransform(contactMatrix);
Thanks in advance!
Basroil wrote:That part is all trig actually, it should be as simple as multiplying out the transform from one to the other. Basically you need to find the "contact" area (really just a close approximation should minimize issues) on the box and then extend the rotation from there to the center of gravity.

Another, slightly less intuitive approach would be to keep the box flat and just change the direction of gravity

d3x0r
Posts: 51
Joined: Tue Dec 11, 2012 9:59 pm

Re: Tilting Table - kinematic body force exerted on rotation

Post by d3x0r » Mon Feb 29, 2016 10:20 pm

instead of modifying the table's position spacially and kepeing the same gravity vector, can't you just update what the ball thinks of as down?
body.setGravity

http://bulletphysics.org/Bullet/BulletF ... 72663e0ddc

then you don't have to worry about translating the ball as the table moves up/down

User avatar
drleviathan
Posts: 527
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Tilting Table - kinematic body force exerted on rotation

Post by drleviathan » Mon Feb 29, 2016 10:42 pm

d3x0r, I could probably come up with some snippets that would be helpful but it is not clear to me how the game is supposed to work:

The camera view... does it look down from above at a fixed position or does it follow the ball?

When tossing a ball around on a real tilt maze there are some frame of reference consequences that you might not want. For example, if the ball were far from the center of the maze it might be possible to tilt the floor up so fast that it would launch the ball into the air, or down so fast that the ball is left in the air and must fall back onto the maze. Is this desired behavior for your game or do you just want to tilt the maze such that the ball does not experience any vertical forces (except for gravity)?

The answer to the above question matters because how you solve the problem could maintain or remove the effects of the non-inertial reference frame, and thus the balls motion.

If you don't want the surface motion to throw the ball, then Basroil's suggestion of modulating the gravitational field on the ball is the easiest solution.

If you want to be able to toss the ball with the platform then yes you'll need to do some fancy math and special logic in order to prevent the ball from tunnelling through the platform when it moves too quickly.

Lucky
Posts: 4
Joined: Thu Feb 25, 2016 10:28 am

Re: Tilting Table - kinematic body force exerted on rotation

Post by Lucky » Tue Mar 01, 2016 2:14 am

The camera is going to be switchable between a view on the whole maze (fixed) and one zoomed in onto the ball, following it.

Consequences you have expressed are the exact problem that bothers me to which I cannot come up with a solution, due to my poor knowledge of how bullet works, my trigonometry isn't the best either. id like the game to work like one of those mazes you had as a cbild, where the objective was to get the marble to the end pot. This issue keeps me from continuing with my project, thus I'd would greatly appreciate hints on some methods, cuz after trying so far the rotation point doesnto move at all, no matter what I set the center of mass to.

Finally, I'd really prefer not to change gravity on a static table.

I hope I explained the problem well enough and would like to thank you in advance.

Regards,
Lucky

drleviathan wrote:d3x3r, I could probably come up with some snippets that would be helpful but it is not clear to me how the game is supposed to work:

The camera view... does it look down from above at a fixed position or does it follow the ball?

When tossing a ball around on a real tilt maze there are some frame of reference consequences that you might not want. For example, if the ball were far from the center of the maze it might be possible to tilt the floor up so fast that it would launch the ball into the air, or down so fast that the ball is left in the air and must fall back onto the maze. Is this desired behavior for your game or do you just want to tilt the maze such that the ball does not experience any vertical forces (except for gravity)?

The answer to the above question matters because how you solve the problem could maintain or remove the effects of the non-inertial reference frame, and thus the balls motion.

If you don't want the surface motion to throw the ball, then Basroil's suggestion of modulating the gravitational field on the ball is the easiest solution.

If you want to be able to toss the ball with the platform then yes you'll need to do some fancy math and special logic in order to prevent the ball from tunnelling through the platform when it moves too quickly.

Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Tilting Table - kinematic body force exerted on rotation

Post by Basroil » Tue Mar 01, 2016 3:44 am

You should start by reading https://msdn.microsoft.com/en-us/librar ... 28373.aspx and checking the source code. You can see how they deal with cameras and physics for exactly what you want to do.

After that, go to a library and try to find "Introduction to Robotics: Mechanics and Control (3rd Edition)" by J. Craig . Yes, it's a robotics book, but the transformation methods are explained in the best way I've seen in any book. It sounds like you don't know the difference between various representations of systems in local and world, and that will help a lot. After you learn how to deal with coordinate axis changes, the rest is just transformation chains being fed into the kinematic controller.

Still though, changing gravity is the easiest way to do it, as long as the ball is the only dynamic object, and the table rotates about the ball, there is almost no physical difference between a rotating table and gravity rotating.

Post Reply