| View previous topic :: View next topic |
| Author |
Message |
BluescreenODeff

Joined: 11 Aug 2007 Posts: 17 Location: Kansas
|
Posted: Mon Jun 08, 2009 7:54 pm Post subject: Collision Detection in FB Game |
|
|
Hello everyone. I'm working on an RPG and I've nearly got the colission detection right. My problem is when you hit the object that is specified as a wall, you are blocked, but you also can't move in any other direction until you let go of the key. I use MULTIKEY to get the character moving. This is the collision detection code:
| Code: |
for a = 0 to columns-1
for b = 0 to rows-1
if spritemode(map(a,b)) = 1 then
cmin = (a*16)-15: cmax = (a*16)+15
rmin = (b*16)-15: rmax = (b*16)+15
if r >= rmin and r <rmax>= cmin and c <= cmax then
if MULTIKEY(&h48) then r = r + 1
if MULTIKEY(&h4D) then c = c - 1
if MULTIKEY(&h50) then r = r - 1
if MULTIKEY(&h4B) then c = c + 1
end if
end if
next b
next a
|
columns and rows are the number of columns and rows in the current map.
spritemode is the status of each tile (whether or not you can go through them)
map(a,b) is the map, of course.
the cmin, cmax, rmin, and rmax es specify the boundaries the character has to cross before any collision action is taken.
If there is a collision, the game will move the character in the opposite direction 1 pixel, according to what button was pushed.
Any suggestions? Thanks and God Bless! _________________ --Bluescreen O'Deff
John 1:1, John 3:16, Acts 10:38 |
|
| Back to top |
|
 |
Mentat

Joined: 07 Aug 2007 Posts: 406 Location: NC, US
|
Posted: Mon Jun 08, 2009 8:39 pm Post subject: |
|
|
I'm having a little trouble following the code (specifically, what does "if r >= rmin and r <rmax>= cmin and c <= cmax then " do?).
What about moving a copy of coordinates, then check collision detection? If no collision, then update the original coordinates with the updated copy, otherwise ignore the copy. I found that to help me out sometimes when weird things happen. _________________ For any grievances posted above, I blame whoever is in charge . . . |
|
| Back to top |
|
 |
BluescreenODeff

Joined: 11 Aug 2007 Posts: 17 Location: Kansas
|
Posted: Mon Jun 08, 2009 8:54 pm Post subject: |
|
|
Sorry, the clipboard did something silly.
I'll explain it in pseudo-code:
if r is greater than or equal to rmin and r is less than or equal to rmax and c is greater than or equal to cmin and c is less than or equal to cmax then
r and c is my way of saying y and x. It's easier for me to think "row, column".
Anyway, thanks for the idea; I'll try it. _________________ --Bluescreen O'Deff
John 1:1, John 3:16, Acts 10:38 |
|
| Back to top |
|
 |
BluescreenODeff

Joined: 11 Aug 2007 Posts: 17 Location: Kansas
|
Posted: Mon Jun 08, 2009 9:24 pm Post subject: Aha! |
|
|
I found an answer; I didn't need to make a copy (Actually, I tried it the wrong way). Sorry about that; but it is a good idea!
Here is my updated code:
| Code: |
for a = 0 to columns-1
for b = 0 to rows-1
if spritemode(map(a,b)) = 1 then
cmin = (a*16)-15: cmax = (a*16)+15
rmin = (b*16)-15: rmax = (b*16)+15
if r >= rmin and r <rmax>= cmin and c <= cmax then
if r = rmin then r = r -1
if r = rmax then r = r +1
if c = cmin then c = c -1
if c = cmax then c = c +1
end if
end if
next b
next a
|
This deals with each row and column collision as it occurs.
Sorry that I didn't try out your idea fully.
BTW, remember the pseudo-code post (the <rmax> wasn't part of the original code) _________________ --Bluescreen O'Deff
John 1:1, John 3:16, Acts 10:38 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2002 phpBB Group
|