Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4088356
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:58:20+00:00 2026-05-20T18:58:20+00:00

I have the following code: function collisionDetect(left1,right1,top1,bottom1,left2,right2,top2,bottom2,dir) local left,right,top,bottom=false,false,false,false if left1<right2 then left=true end

  • 0

I have the following code:

function collisionDetect(left1,right1,top1,bottom1,left2,right2,top2,bottom2,dir)
    local left,right,top,bottom=false,false,false,false
    if left1<right2 then left=true end
    if right1>left2 then right=true end
    if top1>bottom2 then top=true end
    if bottom1<top2 then bottom=true end
    if dir.x>0 and (top or bottom) then
        if right then return 1 end
    elseif dir.x<0 and (top or bottom) then
        if left then return 2 end
    elseif dir.y>0 and (left or right) then
        if bottom then return 3 end
    elseif dir.y<0 and (left or right) then
        if top then return 4 end
    end
    return 0
end

left1, right1, etc. are arguments containing the position of the respective bounding box (box 1 or 2).

dir is a “Vector2” (contains an x and y property).

For some reason, my code is returning collisions for objects no where near by. Any ideas?

EDIT:

I have solved my problem, here is the code for anybody googling the subject.

(It’s in Lua, a pretty straight forward and simple to interpret language)

function collisionDetect(left1,right1,top1,bottom1,left2,right2,top2,bottom2,dir)
    local insideHorizontal=false
    local insideVertical=false
    if left1<right2 then insideHorizontal=true end
    if right1>left2 then insideHorizontal=true end
    if top1>bottom2 then insideVertical=true end
    if bottom1<top2 then insideVertical=true end
    if insideHorizontal and insideVertical then
        return true
    end
    return false
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:58:21+00:00Added an answer on May 20, 2026 at 6:58 pm

    Have a look at this for a nice collision detection tutorial/algorithm.

    Essentially:

    bool check_collision( SDL_Rect A, SDL_Rect B )
    {
       //...
       //Work out sides
       //...
    
       //If any of the sides from A are outside of B
        if( bottomA <= topB )
        {
            return false;
        }
    
        if( topA >= bottomB )
        {
            return false;
        }
    
        if( rightA <= leftB )
        {
            return false;
        }
    
        if( leftA >= rightB )
        {
            return false;
        }
    
        //If none of the sides from A are outside B
        return true;
    }
    

    Or, in terms of your code:

    function collisionDetect(left1,right1,top1,bottom1,left2,right2,top2,bottom2,dir)
        if bottom1<=top2 then return true end
        if top1>=bottom2 then return true end
        if right1<=left2 then return true end
        if left1<=right2 then return true end
        return false
    end
    

    (this isn’t a language I know, so I’m guessing a bit)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the following code: function One() {} One.prototype.x = undefined; function Two()
I have the following code making a GET request on a URL: $('#searchButton').click(function() {
Suppose I have the following code: class siteMS { ... function __CONSTRUCT() { require
I have the following code: <script type=text/javascript> function SubmitForm() { form1.submit(); } function ShowResponse()
I have the following code: <script type=text/javascript> $(document).ready(function() { $(#Save).click(function() { $.post(url, { data:
I have the following JavaScript code: Link In which the function makewindows does not
I have using the following code for developing tabs. $(document).ready(function() { $('#container-1').tabs(); } ....
I have Designed the Javascript function My.js it contains following code My.js function display(){
I have the following javascript code, which loads without error, however the update function
I have the following code: function showAccessRequests_click() { var buttonValue = $(#showAccessRequests).val(); if (buttonValue

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.