Skip to main content
gamedev.net
🔒 Locked

collision detection

Started by charlieb410 2 replies 900+ views
Original Post
charlieb410
charlieb410
Hi everyone I''ve been studying some code which implements collision detection using boundary rectangles. All it consist of is a ball that bounces around the screen to illustrate some basic sprite properties. Heres some of the code: // Check the bounds // Wrap? if (boundsAction == Sprite.BA_WRAP) { if ((xPos + width) < xBounds) xPos = xBounds + wBounds; else if (xPos > (xBounds + wBounds)) xPos = xBounds - width; if ((yPos + height) < yBounds) yPos = yBounds + hBounds; else if (yPos > (yBounds + hBounds)) yPos = yBounds - height; } xPos = x coord of the sprite yPos = y coord of the sprite width = the image width heigth = the image heigth xBounds, yBounds, hBounds, and wBounds are all boundary variable for the sprite. This block of code causes the ball to wrap to the other side of the screen if the certain conditions are met. How does this work? For example what exactly does if((xPos + width)< xBounds) check for?
Indeterminatus
Indeterminatus
quote:

This block of code causes the ball to wrap to the other side of the screen if the certain conditions are met. How does this work? For example what exactly does if((xPos + width)< xBounds)
check for?



This code checks whether the sprite has moved completely out of the left bounding/wrapping area.
xPos + width gives you the very right point of the sprite, xBounds is the left bounding area.
if xPos + width is smaller than xBounds, this means that the sprite has completely moved out of that area (only talking about x-axis here!).
The rest of the code does pretty much the same, except that it checks for the right, the top and the bottom.


Indeterminatus

--si tacuisses, philosophus mansisses--
Indeterminatus--si tacuisses, philosophus mansisses--
charlieb410
charlieb410
Hey
Thanks for taking the time to answer my question.

[edited by - charlieb410 on May 6, 2004 12:39:52 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.