DanBrezeanu
DanBrezeanu

Reputation: 552

OpenGL - Move ball on tilted tabletop

I have a tabletop that can rotate around OX and OZ axis and a ball on this tabletop. I need to have the ball moving around the plane accordingly to its tilt. The ball does not need to have acceleration (velocity is constant at a certain tilt).

Here is how I want the ball to move.

The sphere mesh is created like this:

{
    glm::mat4 modelMatrix = glm::mat4(1);
    modelMatrix = glm::translate(modelMatrix, glm::vec3(speedOX, speedOY + 0.5f, speedOZ));
    RenderSimpleMesh(meshes["sphere"], shaders["ShaderLab8"], modelMatrix, glm::vec3(0, 1, 1));
}

Where speedOX speedOY speedOZ is the translation vec3 for the sphere.

The plane mesh is created like this

    {
    glm::mat4 modelMatrix = glm::mat4(1);
    modelMatrix = glm::translate(modelMatrix, glm::vec3(0, 0.01f, 0));

    modelMatrix = glm::rotate(modelMatrix, RADIANS(anglePlaneOX), glm::vec3(1, 0, 0));
    modelMatrix = glm::rotate(modelMatrix, RADIANS(anglePlaneOY), glm::vec3(0, 1, 0));
    modelMatrix = glm::rotate(modelMatrix, RADIANS(anglePlaneOZ), glm::vec3(0, 0, 1));
    modelMatrix = glm::scale(modelMatrix, glm::vec3(0.125f));

    RenderSimpleMesh(meshes["plane"], shaders["ShaderLab8"], modelMatrix, glm::vec3(0.5, 0.5, 0.5));
}

The plane moves by hitting WASD keys.

    if (window->KeyHold(GLFW_KEY_W) && (anglePlaneOX > -90.0f)) {
        anglePlaneOX -= deltaTime * DELTA_SLOPE;

        /*update speedOX speedOY speedOZ */
    }
    if (window->KeyHold(GLFW_KEY_S) && (anglePlaneOX < 90.0f)) {
        anglePlaneOX += deltaTime * DELTA_SLOPE;

        /*update speedOX speedOY speedOZ */

    }
    if (window->KeyHold(GLFW_KEY_D) && (anglePlaneOZ > -90.0f)) {
        anglePlaneOZ -= deltaTime * DELTA_SLOPE;

        /*update speedOX speedOY speedOZ */
    }

      if (window->KeyHold(GLFW_KEY_A) && (anglePlaneOZ < 90.0f)) {
        anglePlaneOZ += deltaTime * DELTA_SLOPE;

        /*update speedOX speedOY speedOZ */
    }

I couldn't really find the math behind moving the ball accordingly to the tilt of the plane.

Note: the ball does not to rotate per se as I'm not doing any crazy shadowing over the ball. I just want it move (translate) on the moving plane. Also it seems that the ball "sinks" a bit in the plane as showed in the image.

Upvotes: 2

Views: 315

Answers (1)

AdaRaider
AdaRaider

Reputation: 1136

Sounds like what you want to do is model the behavior of the ball after the physics of objects moving on ramps.

You have a plane, but you can imagine this process easily lends itself to the physics of object moving down ramps as your plane makes the hypotenuse of the triangle.

There are some really great tutorials out there, this one for example; https://www.dummies.com/education/science/physics/calculating-how-far-an-object-will-slide-down-an-inclined-surface/

This one describes how to calculate the various values you will need in order to work out where the ball will be when the plane tilts and could easily be adapted to work in your simulation.

However, if you just want the ball to move along the plane so that it doesn't "sink" through the plane (not a physics simulation of the ball moving just someone controlling the ball like a character walking on the plane) you could be talking about collisions. In which case to get the ball to sit correctly on the plane you will want to implement a system for checking collisions between the ball and the plane. A common way to check collisions is to give each object in the world a bounding box, then perform collision checks on the objects bounding boxes, the simplest case being axis aligned bounding box on axis aligned bounding box (AABB). But in your case you will need something more clever than AABB on AABB collision checks because your plane will need an oriented bounding box (OBB on OBB/AABB). Bounding spheres are also a common choice to use for collision volumes.

Alternatively if you have a rotation matrix for your plane you could simply create a horizontal direction vector for the ball and multiply it by the rotation matrix before you apply it to the ball. Then you can just add that vector to the position of the ball which will give you a translation vector that points along the x axis of the plane.

Upvotes: 1

Related Questions