Game Development Community

Collision direction detection

by Steven Hine · in Torque Game Builder · 10/09/2008 (5:38 pm) · 2 replies

Is there a method of detecting where a collision occurred on an object? Like did one object hit the top, bottom, right, or left of the source object.

#1
10/10/2008 (12:42 am)
Inside the onCollision callback, there is a variable passed called the collision normal. This variable tells you a lot about the collision. If you're just wanting to detect what side of the source object the collision was detected, you can do something like this:

function myClass::onCollision(%srcObject, %dstObject, %srcRef, %dstRef, %time, %normal, %contacts, %points)
{
    %normalX = getWord(%normal, 0);
    %normalY = getWord(%normal, 1);
    
    if (%normalY != 0)
    {
        if (%normalY < 0)
            echo ("top");
        else
            echo ("bottom");
    }
    
    if (%normalX != 0)
    {
        if (%normalX < 0)
            echo ("left");
        else
            echo ("bottom");
    }
}
#2
10/10/2008 (2:37 pm)
Thanks! Exactly what I was looking for...