Rectangular Collison Detection
Author |
Message |
Mustiko
|
Posted: Sat May 31, 2008 11:39 am Post subject: Rectangular Collison Detection |
|
|
Hey guys I have a question concerning rectangular collision detection. My question is that if I want to define borders so that when a player in a game goes to the point thye cannot go past that border, now I looked a tutorial all it told me was how to make the object change direction when it touches the border...but I just need the player/object to not be able to go past and continue with whatever. Secondly, I want to make a platform and that platform is part of the background so I want make it so that if he touches the bottom fo the platform or lands on the top he cannot go through it.
Thanks for all your help at Compsci |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Insectoid

|
Posted: Sat May 31, 2008 2:06 pm Post subject: RE:Rectangular Collison Detection |
|
|
Are you loading an image for the background?
To make it not go past a certain point, make an if statement
code: |
if y + 10 < hightst point then
y += 2
end if
|
|
|
|
|
|
 |
Mustiko
|
Posted: Sat May 31, 2008 10:47 pm Post subject: Re: Rectangular Collison Detection |
|
|
Well the image is the background. I am trying to make a platform in the background into a real platform that the player can walk on |
|
|
|
|
 |
TheWanderer
|
Posted: Sun Jun 01, 2008 12:37 am Post subject: Re: Rectangular Collison Detection |
|
|
I would go with insectoid's solution
here's my 2 cents:
1. draw your image using turing
2. create x, y, and button int variables for mouse
3. loop a mousewhere program and display the coordinates of the platform you want
4. write these down. these will be the coordinates of your platform
5. use insectoid's limit detection when you code the movement for your player
Turing: | var x, y, b : int % for Mouse module
loop
cls
Pic.Draw ("imagename.jpg", 0, 0, picCopy)
mousewhere (x, y, b )
locate (1, 1)
put "(", x, ", ", y, ")"
end loop
|
the pic will be drawn, then when you move the mouse cursor over the area of the platform, you'll know what coordinates to set the limits to
good luck! |
|
|
|
|
 |
Reality Check
|
Posted: Sun Jun 01, 2008 8:35 am Post subject: Re: Rectangular Collison Detection |
|
|
Well, I assume you are adding to an x and y variable when certain keys are pressed. So, only add to your location variables IF the current x or y location is not past the collision point. If it is, you do not add. |
|
|
|
|
 |
Mustiko
|
Posted: Sun Jun 01, 2008 3:47 pm Post subject: Re: Rectangular Collison Detection |
|
|
So basic if statements like the one insectoid used would work? Because I've tried to look at other peoples codes and figure something out and I have gravity and according to others some put like gravity = 0 or make big if statements stating all the sides of the platform and saying if x1 is > x2 then ..... gravity = 0 or ground = true or stuff like that. Would I need stuff like that?
Thanks again for the help |
|
|
|
|
 |
Insectoid

|
Posted: Sun Jun 01, 2008 5:37 pm Post subject: RE:Rectangular Collison Detection |
|
|
It would take a lot of if statements to hardcode every platform/boundary. This is unavoidable if you're drawing a picture. I haven't done too much with gravity, but I assume that while the character is falling, you also check if his X coordinate is less than the X of the right side and greater than the X of the left side of the platform and Y is less than or equal to the top of the platform and Y is greater than the bottom of the platform then Y velocity is equal to 0. Or something along those lines. Unless it's a very simple picture (2-3 colors) this is the only way (That I know of). |
|
|
|
|
 |
Mustiko
|
Posted: Sun Jun 01, 2008 9:58 pm Post subject: Re: Rectangular Collison Detection |
|
|
Ok and what if I had a picture and to save space instead of using a bunch of if statements I made a game like mario where every platform is made out of blocks or bricks and they are separated into equal squares. So if you pass through different parts there are other platforms or blocks. And what I am saying is just make a couple of if statements to cover all of the blocks and just plug in whatever the blocks are for that level. Now I have allt he measurements for all the block if I were to do that because I was planning that but would I do the same thing as you suggested? also if you could maybe give a couple of general if statements for all the sides so I exactly how to do this. For example you could use s1 for shape 1 and s2 for shape 2 and x1, x2, y1, y2 etc.. so something like this....i guess...s1x1 > s2x2 blah blah I am making this up because the tutorials don't really give such a general statement so I don't no where to start. Thanks. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Mustiko
|
Posted: Sun Jun 01, 2008 9:59 pm Post subject: Re: Rectangular Collison Detection |
|
|
By the way thats my last question because once I get that I think I will be able to do the rest unless you tell me something really complicated haha.. |
|
|
|
|
 |
TheWanderer
|
Posted: Sun Jun 01, 2008 10:27 pm Post subject: Re: Rectangular Collison Detection |
|
|
Say one rectangle runs from
100,150---------------200,150
|.......................................|
|.......................................|
|.......................................|
100,100---------------200,100
your if statements would be, (assuming your character location is designated x, y)
and that you can pass through the rectangle from the bottom and the sides (like in super smash brothers platforms)
Turing: | if x > 100 and x < 200 then
if y - 1 >= 150 then
y -= 1
end if
else
y -= 1
end if |
just off the top of my head, could be improved.. but should work
this isn't tested as I don't have a setup to test with...
it'd be much more complicated if you couldn't pass the platform at all (like in super mario 1 platforms) |
|
|
|
|
 |
Insectoid

|
Posted: Mon Jun 02, 2008 7:53 am Post subject: RE:Rectangular Collison Detection |
|
|
I suppose for a large level like that youm would have to use either whatdotcolor or think of your own genious algorithm. I'm sure it's been done... |
|
|
|
|
 |
Mustiko
|
Posted: Mon Jun 02, 2008 9:13 am Post subject: Re: Rectangular Collison Detection |
|
|
Yea thats basically my problem TheWanderer because I am trying to make the platforms like super mario 1, so you can't pass through them. Any suggestions because that is the 1 important thing I need? O by the way I am testing some stuff out so if I figure it out I'll post if for other poeple with this problem. |
|
|
|
|
 |
Insectoid

|
Posted: Mon Jun 02, 2008 4:33 pm Post subject: RE:Rectangular Collison Detection |
|
|
I suppose it would be easier if you draw the map in turing (say, you have a brick pic and a tunnel pic) and as you draw them to the screen, their coordinates are saved to an array and then when the character moves, you check through the array with a for loop to see if it collides with something. |
|
|
|
|
 |
TheWanderer
|
Posted: Mon Jun 02, 2008 10:30 pm Post subject: Re: RE:Rectangular Collison Detection |
|
|
insectoid @ Mon Jun 02, 2008 4:33 pm wrote: I suppose it would be easier if you draw the map in turing (say, you have a brick pic and a tunnel pic) and as you draw them to the screen, their coordinates are saved to an array and then when the character moves, you check through the array with a for loop to see if it collides with something.
That would work. a little twist on that idea:
Assuming you know how View.Update works,
create another image the same size as the photo you're using.
it'll be blank, then wherever the platforms are in your original photo, create black squares or whatever colour you can remember.
like if a platform is a circle shape at (100,100)
in your blank image create a black circle at (100,100), same size, same shape.
when you run your program, here's the detection method to stop player movement:
Edit : a little fix:
same method for detecting moving left, up, and down.
if your player tries to move over a black area, it won't be able to
just draw the platform map image first, then have turing check for collision, then draw your photo over that so the player can't see it.
Good call, insectoid |
|
|
|
|
 |
Mustiko
|
Posted: Tue Jun 03, 2008 4:33 pm Post subject: Re: Rectangular Collison Detection |
|
|
Ok thanks I'll try that but heres question for you...I don't understand why you ahve to put playerX + 1 and playerY + 1 what does adding one do? I hope this wasn't a noob question |
|
|
|
|
 |
|
|