Skip to content
Saarrthi Rana

School project / Fall 2025

Grab and drag physics

Assignments for Game Physics 3, in C++ and OpenGL.

Role
Solo
Team
Game Physics 3 course
When
Fall 2025
C++OpenGLPhysicsCollision
Video
The skull, the box, the cylinders and the board are grabbed and dragged around. Each one follows the mouse with physics.
What it is

Grabbing objects with the mouse means working out what you clicked for each kind of shape, then pulling it around without it flying off.

I made box, cylinder and capsule shapes, wrote a hit test for each one, and used them so you can click an object and drag it with the mouse.

What I did
  1. Built the box, cylinder and capsule shapes in code. The starter only had the front of the box, and the other shapes reused the sphere.
  2. Wrote a test for each shape that checks whether a line from the mouse hits it, and where.
  3. Made clicking work on rotated or scaled objects by checking the click from each object's point of view.
  4. Added the physics for dragging, with gravity, air drag and a pull toward the mouse every frame.
How it works

Clicking a rotated box

I rotate the mouse line to match the box, so the box is straight again. Then I check where the line goes in and comes out on each side.

Dragging

Every frame the object gets gravity and drag first, then a pull that moves the spot you grabbed toward the mouse, and only then does it move. That order stops it from shaking or lagging behind.

Code

Code I wrote

Short parts of my own code from this project. I left out anything the course gave us.

Line and box hit test

Box.cpp, C++ and OpenGL

The hit test for a rotated box. I wrote the whole function.
RayIntersectionInfo Box::rayIntersectionInfo(const Ray& ray) const {
    // ...
    Quaternion invOrientation = QMath::conjugate(orientation);

    Vec3 localStart = QMath::rotate(ray.start - centre, invOrientation);
    Vec3 localDir = QMath::rotate(ray.dir, invOrientation);

    float tmin = 0.0f;
    float tmax = FLT_MAX;
    // ...
    for (int i = 0; i < 3; ++i) {
        float origin = (i == 0) ? localStart.x : (i == 1 ? localStart.y : localStart.z);
        float dir = (i == 0) ? localDir.x : (i == 1 ? localDir.y : localDir.z);
        float minVal = (i == 0) ? boundsMin.x : (i == 1 ? boundsMin.y : boundsMin.z);
        float maxVal = (i == 0) ? boundsMax.x : (i == 1 ? boundsMax.y : boundsMax.z);

        if (std::fabs(dir) < EPS) {
            // Ray parallel to this slab's planes: origin must be between them
            if (origin < minVal || origin > maxVal) {
                return info; // no intersection with box
            }
            continue;
        }

        float t1 = (minVal - origin) / dir;
        float t2 = (maxVal - origin) / dir;

        float tNear = std::fmin(t1, t2);
        float tFar = std::fmax(t1, t2);

        if (tNear > tmin) tmin = tNear;
        if (tFar < tmax) tmax = tFar;

        if (tmin > tmax) {
            // Slab intervals don't overlap → miss
            return info;
        }
    }

    info.isIntersected = true;
    info.t = tmin;
    info.intersectionPoint = ray.currentPosition(tmin);
    return info;
}

Checking the click from each object's point of view

Scene0.cpp, C++ and OpenGL

This replaced a to do comment in the starter.
            haveClickedOnSomething = false;
            pickedActor = nullptr;
            float       closestT = FLT_MAX;
            std::string pickedName;
            // ...
                if (!transformComponent || !shapeComponent || !shapeComponent->shape) {
                    continue; // skip if something is missing
                }

                // Same model matrix used in Render()
                Matrix4 modelMatrix = actor->GetModelMatrix();
                Matrix4 worldToObjectTransform = MMath::inverse(modelMatrix);

                // Transform the ray into OBJECT SPACE of this actor
                Vec3 rayStartInObjectSpace =
                    worldToObjectTransform * Vec4(rayWorldSpace.start, 1.0f);  // w = 1 for positions
                Vec3 rayDirInObjectSpace =
                    worldToObjectTransform * Vec4(rayWorldSpace.dir, 0.0f);    // w = 0 for directions

                GEOMETRY::Ray rayInObjectSpace(rayStartInObjectSpace, rayDirInObjectSpace);

                // Ray-shape intersection is done in object space
                RayIntersectionInfo rayInfo =
                    shapeComponent->shape->rayIntersectionInfo(rayInObjectSpace);

                // Check for closest positive t (hit in front of camera)
                if (rayInfo.isIntersected && rayInfo.t > 0.0f && rayInfo.t < closestT) {
                    closestT = rayInfo.t;
                    pickedActor = actor;
                    pickedName = it->first;
                    haveClickedOnSomething = true;

                    /// 
                    /// 3) Store world–space intersection point
                    ///    (anchor point for the mouse constraint)
                    /// 
                    // Remember, we did all the ray-shape intersection math in object space
                    // We need to transform that back into world space using the model matrix
                    intersectionPoint =
                        transformComponent->GetTransformMatrix() * rayInfo.intersectionPoint;
                }

Built on our instructor's OpenGL framework. I added the shapes, the hit tests, the picking and the dragging.

Next school project

OpenGL terrain and a jellyfish

Assignments from Game Graphics 2 and Game Physics 2, in C++ and OpenGL.