How does SWEPT AABB collision work in 3D space with objects of different sizes?

Started by
1 comment, last by scott8 1 month ago

I finally got my coordinate system figured out. Forgot the fact that Vulkan renders objects as XZY, but my translations were all XYZ. Now I have a system basically checks if my player's collision box is between the min and max of another object's AABB.

My problem is, how do I handle cases where I'm colliding with something small than the player but on an elevated Y plane. Like a bullet for example. Where they X and Z collide, but the payer's Ymin and Ymax are Small and larger than the bullet's ymin and ymax.

Like this this:




As you can see here, there are collisions on the X and Z, but not Y.

I don't want a super complex collision system. This game is just supposed to be 2.5D. 2D objects with a Z Depth.

What's the best way to go about this?

Advertisement

In the case of small round objects (bullets, hand grenades, etc) here's what I did:

  1. Rather than a true AABB, i used a structure like this
    1. XMFLOAT3 vCenter
    2. float XScalar, float YScalar, float ZScalar

This gives you the center of AABB, and then three vectors that can be stretched on each axis (X,Y,Z). You can recreate and draw an actual AABB with this information . For example the first ‘X’ in the upper right of your drawing could be calculated as vCenter+XScalar*XAxis+YScalar*YAxis+zScalar*ZAxis . XAxis =1,0,0 etc. You have to use +/- versions of the scalars to get all 8 vertices of an AABB. You don't need to do this, I did to prove to myself that the same info was there.

I then calculated the time required for a point to hit the plane of an AABB using vector projection.. A plane is different than a face of an AABB. A plane is infinite, but the face is not (Analogous to a line and a line segment).

if the point hits the AABB plane within time dT, a collision might occur. To be on the face, the projection onto the other vectors (Y,Z) at the calculated time have to be less than their scalars.

In the picture below , if point 1 moves distance F' in time dt, it's on the FACE of an oriented bounding box. I projected the position onto the other vector, and is was less than the scalar (dx).

I also calculated the time for point 2 to hit the PLANE of the AABB. I projected the position of point 2 at that time onto the other vector, and you can see it's too large. So it hits the PLANE, but not the face. So no collision in this time step.

You can modify this to handle collisions with objects with spherical volume, like grenades. If the grenade has a radius 0.5 units, then start by projecting point 1 directly down onto the AABB's first vector, then move 0.5 units in the the direction (green line) of the bounding box's first vector. Calculate the time required starting from this position.

In my code, I made the box stationary by adding its velocity to the velocity of my point.

This topic is closed to new replies.

Advertisement