Jump to content
  • Advertisement
Sign in to follow this  
zqf

Expanding on slab based line vs AABB

This topic is 2122 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can't remember where I found the source for this, but I converted this algorithm from C to actionscript and it works perfectly, giving me the position of the hit if one has occurred.
However I'd like to extend it so it can give me the specific side that the ray hit, so I can use it for bouncing objects off. Is this possible with this algorithm (and if so how) or would it be better to use a different technique?


public static function LineSegmentVsTile(v1:LineSeg, AABB:ATile, impactPoint:Point):Boolean
{
var dirFracX:Number = 1 / v1.vx;
var dirFracY:Number = 1 / v1.vy;

var t1:Number = (AABB.position.x - v1.a.x) * dirFracX;
var t2:Number = ((AABB.position.x + (AABB.halfWidth * 2)) - v1.a.x) * dirFracX;
var t3:Number = (AABB.position.y - v1.a.y) * dirFracY;
var t4:Number = ((AABB.position.y + (AABB.halfHeight * 2)) - v1.a.y) * dirFracY;

var tmin:Number = Math.Max(Math.Min(t1, t2), Math.Min(t3, t4));
var tmax:Number = Math.Min(Math.Max(t1, t2), Math.Max(t3, t4));

if (tmax < 0)
{
return false;
}
else if (tmin > 1)
{
return false;
}
else if (tmin > tmax)
{
return false;
}
else
{
if (impactPoint)
{
var colX:Number = v1.vx * tmin;
var colY:Number = v1.vy * tmin;

impactPoint.x = v1.a.x + colX;
impactPoint.y = v1.a.y + colY;
}
return true;
}
}


Thanks

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!