I've been trying for weeks to get AABB collision set up on my DX11 project.

So far, I have a 'condition' set up in place, so that when both cubes collide the 'light' cube will show within the scene. At this moment, the cubes pass through each other, which causes the light to show, but I can't figure out how to actually stop them from passing through each other. When cube1 hits cube2, they'll simply collide and won't be able to pass through each other.

This manages the user input

//Manage the users input via keyboard
class Vector3 {
    float x;
    float y;
    float z;

class UserInput
    float xPos;
    float yPos;
    float zPos;
    Vector3 vecMin;
    Vector3 vecMax;
    float rotationX;
    float rotationY;
    float rotationZ;
    bool isMoving;
    bool isMovingRight;
    bool isMovingLeft;
    bool isMovingUp;
    bool isMovingDown;
    bool isMovingCloser;
    bool isMovingFurther;
    bool isRotatingX;
    bool isRotatingY;
    bool isRotatingZ;
        rotationX = 0.0f;
        rotationY = 0.0f;
        rotationZ = 0.0f;
        isMovingRight = false;
        isMovingLeft = false;
        isMovingUp = false;
        isMovingDown = false;
        isMovingCloser = false;
        isMovingFurther = false;
        isRotatingX = false;
        isRotatingY = false;
        isRotatingZ = false;

    void UpdatePos()
        //-1 and +1 from the origin of the cube (centre)
        vecMin.x = xPos - 1.0f;
        vecMax.x = xPos + 1.0f;
        vecMin.y = yPos - 1.0f;
        vecMax.y = yPos + 1.0f;
        vecMin.z = zPos - 1.0f;
        vecMax.z = zPos + 1.0f;

AABB Collision Function

bool AABBtoAABB(const UserInput& box1, const UserInput& box2)
    return!(box1.vecMin.x > box2.vecMax.x ||
        box1.vecMax.x < box2.vecMin.x ||
        box1.vecMax.y < box2.vecMin.y ||
        box1.vecMin.y > box2.vecMax.y ||
        box1.vecMin.z > box2.vecMax.z ||
        box1.vecMax.z < box2.vecMin.z);

Update Function

//Update each Frame
void Update()
    AABBtoAABB(input[0], input[1]);
  • 1
    \$\begingroup\$ So, you checked through existing Q&A in the collision-resolution tag for ideas about how to respond to and resolve collisions once you've detected them, right? What have you learned so far and how have you tried applying it to your case? \$\endgroup\$ – DMGregory Jan 24 '19 at 13:13

Give the objects a velocity. Set the velocity to zero on each axis if there would be a collision in that place.

// Object
velocity.x = 4
velocity.y = 2

if is_collision(velocity.x, 0, other) velocity.x = 0
x += velocity.x

if is_collision(0, velocity.y, other) velocity.y = 0
y += velocity.y

// Collision function
is_collision(offset.x, offset.y, other)
| improve this answer | |
  • 1
    \$\begingroup\$ Thank you so much for this! I've implemented the velocity, and created a function to check collisions and it works perfectly! \$\endgroup\$ – George_H Jan 24 '19 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.