Need help doing collision detection for moving objects
Author |
Message |
TuringStudent
|
Posted: Sun Jun 12, 2011 12:38 pm Post subject: Need help doing collision detection for moving objects |
|
|
What is it you are trying to achieve?
I am designing a game involving a moving blue square and moving red circles as enemies. The objective is to move my blue square across a rectangular area and dodge the enemy red circles who move in alternating up/down patterns (i.e. the first enemy moves down-up then the next guy up-down etc.). I am having great difficulty doing collision detection to stop my blue square from moving (and ultimately later on making him restart at the beginning of the level) when he collides with a red circle.
What is the problem you are having?
I cannot seem to come up with any proper way of using collisions detection whether by whatdotcolour commands or executing a command to die or something once the coordinates of my blue square are equal to some coordinate aspect of the red circle.
Describe what you have tried to solve this problem
I have tried multiple solutions on my own without help, those including drawing my enemies as drawfilloval and the colour red (12) and then using a whatdotcolour command whose parameters are all the corners of my blue square so that as soon as any corner comes in contact with a red background essentially, my blue square will "die" and have to start at the beginning again. I have also tried collision detection using the changing Y coordinates of the red circles (they move up to down/ down to up so the position of their Y centre changes in a loop) to check if any corners of my blue square are equal to these Y coordinates. This doesnt work because I only want my blue square to die when he encounters the red circle and not just the coordinate at which the red circle is drawn. This causes me to die without getting close to the circle.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
Turing: |
<Add your code here>
|
Please specify what version of Turing you are using
Turing 4.1.1
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
That_Blue_BlockV3.t |
Filesize: |
16.24 KB |
Downloaded: |
216 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Wed Jun 15, 2011 8:43 am Post subject: RE:Need help doing collision detection for moving objects |
|
|
So im assuming you're trying to make a game that's basically the same as "The World's Hardest Game"?
|
|
|
|
|
![](images/spacer.gif) |
TWizard
![](http://compsci.ca/v3/uploads/user_avatars/14193012864c2cdeb3237c3.jpg)
|
Posted: Fri Aug 12, 2011 6:07 pm Post subject: RE:Need help doing collision detection for moving objects |
|
|
You may have already tried this but, if it were me i would have the box either equal out side or what ever speed your moving at give a negative speed, only when you box coordinated meet your enemy coordinated. Since the Draw.Box, uses 2 positions to draw it, and the Draw.Oval uses only 1 you would have to use the ovals position minus or plus in the direction you want it to detect with using its radius as the amount you want to detect.
if Box_X1 >= Ball_X - "ball radius" and Box_X2 <= Ball_X + "ball radius" and Box_Y1 >= Ball_Y - "ball radius" and Box_Y2 <= Ball_Y + "ball radius" then
collision := true
end if
Unfortunately this makes the ball, a box, i'v lost my method of making that ball use and check for its diameter, instead of making it a box. If i find a better way or if someone posts a different solution, then i hope what i'v said is some what useful.
|
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Sat Aug 13, 2011 2:34 pm Post subject: RE:Need help doing collision detection for moving objects |
|
|
Collision Detection with Rectangles and Circles is painfully annoying. You either need some funky math, or have to use bitmasks (look for the per-pixel collision detection tutorial) . My suggestion is to either just rectangles or just circles. Unless you're using Non-regular shapes (like sprites), you shouldn't use bitmasks as it'll be vastly slower than the math.
Edit: I sorta realized the math is way easier when dealing with non-rotated rectangles.
Collision between Rectangles and Circles occurs when:
* Then center point of the circle is within the rectangle.
* One of the edge lines of the rectangle intersect with the circle.
The first point is relatively easy. The second is much more complicated math. However, as we're not dealing with rotated rectangles, we can simplify this.
The outer perimeter of the rounded box is the farthest the center point of the circle can be for a collision with the inner box.
You can break this up into 2 functions.
2 calls to isPointInRectangle() for if the center of the circle is in the 2 overlaping rectangles.
4 calls to isPointInCircle() for if the center of the circle is in the rounded edges.
|
|
|
|
|
![](images/spacer.gif) |
mirhagk
|
Posted: Mon Aug 15, 2011 8:15 am Post subject: RE:Need help doing collision detection for moving objects |
|
|
There was a tutorial on this, I'll try to find it, but basically either the circle's centeris within the rectangle, the rectangle's corners are in the circle. However there is 1 case that Zren missed that could potentially happen (although only with big enough rectangles and small enough circles).
The 4 cardinal directions out from the center of the circle along the perimeter of the circle, could be inside the rectangle. Like center.x-radius.x,center.y etc could be within the rectangle. This happens when the circle intersects with a side of the rectangle.
|
|
|
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
|
|
|
![](images/spacer.gif) |
|
|