Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Huge Dungeon Collision Detection

Author
Message
badkneecap
16
Years of Service
User Offline
Joined: 21st Jun 2010
Location:
Posted: 5th Jul 2010 23:59
I went through the tutorial for the Huge Dungeon. There was a comment about modifying the code so the user "slides" down a wall and not just stop dead in their tracks. I was able to get that to work with one exception, when I slide into a corner. I listed my code below. So, when I "slide into a corner, it continues to slide. It doesn't seem to work when both the x and z positions are "bad", so I try and return the camera back to where it was before the move. Am I missing something? Has anyone come up with a solution that works?

` Collisiondetection
if map$(int(curposx),int(curposz))="#"
if tx# >= ((curposx * cubesize) - (cubesize/2)) - collisionstep and tx# <= ((curposx*cubesize) + (cubesize/2)) + collisionstep and tz# >= ((curposz * cubesize) - (cubesize/2)) - collisionstep and tz# <= ((curposz * cubesize) + (cubesize/2)) + collisionstep
xbad = 0
zbad = 0

` 1
if tx# >= ((curposx * cubesize) - (cubesize/2)) - collisionstep and tx# <= ((curposx*cubesize) + (cubesize/2)) + collisionstep and oldpositionz# >= ((curposz * cubesize) - (cubesize/2)) - collisionstep and oldpositionz# <= ((curposz * cubesize) + (cubesize/2)) + collisionstep
xbad = 1
endif

` 2
if oldpositionx# >= ((curposx * cubesize) - (cubesize/2)) - collisionstep and oldpositionx# <= ((curposx*cubesize) + (cubesize/2)) + collisionstep and tz# >= ((curposz * cubesize) - (cubesize/2)) - collisionstep and tz# <= ((curposz * cubesize) + (cubesize/2)) + collisionstep
zbad = 1
endif

`if xbad = 1 and zbad = 1 then end
if xbad = 0 and zbad = 1 then position camera tx#, camera position y(), oldpositionz#
if xbad = 1 and zbad = 0 then position camera oldpositionx#, camera position y(), tz#
if xbad = 1 and zbad = 1 then position camera oldpositionx#, camera position y(), oldpositionz#
endif
endif
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 6th Jul 2010 01:37
Well, the basic rule of sliding collision (easy method I mean) is this:

Store last X and Z position. (LX and LZ)
If there is a collision on X,Z then
If there is a collision on LX,Z then Z=LZ
If there is a collision on X,LZ then X=LX
Endif

So find the collision, then on each axis, check it with the previous position on the opposite axis. If there is a collision on the X axis only, then LX,Z should be 0, but X,LZ will be 1. By affecting only 1 axis at a time, it should slide nicely as long as the actual position is never inside a collidable area.

Hope this helps.


Health, Ammo, and bacon and eggs!
badkneecap
16
Years of Service
User Offline
Joined: 21st Jun 2010
Location:
Posted: 6th Jul 2010 04:51
I tried that, but no matter what, I keep getting the same problem. That is, when I slide into a corner, it keeps going. If I slide on x, or I slide on z, it works fine. the only time I have a problem is when it collides on both.

So, the logic above seems kind of elementary, but that's because I couldn't get it to work the way you listed. However, look at the logic above. I believe it follows the same logic that you are mentioning.

Here's another observation. If x is bad and z is bad, then it should be a corner (no slide) and it should go back to the old x and z position. But it's not, and that's really my problem. I put a line of code in there that says to end if the x and z are both bad. the program will stop, so I know the logic is correct. I'm using oldpositionx# and oldpositionz#, so it should return to the position it was in before the move.

Seems simple, but it just doesn't want to work. The collision code above is the only code I changed in the code for that tutorial.

Do you see anything wrong with the code or logic?

Login to post a reply

Server time is: 2026-07-25 06:56:39
Your offset time is: 2026-07-25 06:56:39