School project / Fall 2025
Grab and drag physics
Assignments for Game Physics 3, in C++ and OpenGL.
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.
- 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.
- Wrote a test for each shape that checks whether a line from the mouse hits it, and where.
- Made clicking work on rotated or scaled objects by checking the click from each object's point of view.
- Added the physics for dragging, with gravity, air drag and a pull toward the mouse every frame.
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 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
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
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.