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 Discussion / A quick question about collision detection

Author
Message
Yodaman Jer
User Banned
Posted: 21st Oct 2008 05:20
Well, this is a subject that has always slipped by me in some way. I finally found a way to use static collision a while back, but I've discovered it makes my frame rate drop depressingly so.
And then, whilst doing the Binary Moon tutorials, I stumbled upon Box Collision, which the dude used on a single-loaded object.
Here's my question:

If I designed a hollow 3D object, much like a hallway (see image below),



would I be able to use Box Collision since it applies it to 'every object limb'?

Thanks in advance, and sorry for starting another thread...

Yodaman Jer

Guitarist, 3D modeler, Video editor, Renaissance Faire performer, Christian, and all-around GEEK. With a capital 'G'.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 21st Oct 2008 15:47 Edited at: 21st Oct 2008 16:20
OK, try sparky`s DLL. It has all of the commands you know for collision, and it is blazingly fast. I can`t find where to download it, but I will come back with a link.

Just use "load dll" to load it(it must be in the main directory), and then use the commands in the help file...

And if your darkbasic is not enhanced, work on this:



This uses sliding box collision. It is very simple to use for simple games (yeah, I even used it in "Jack in the Box" in peachy...), but for more complex levels this wont work...

If you put a rotating collision box around everything (make object collision box <object>,<-x>,<-y>,<-z>,<x>,<y>,<z>,1), the collision box will rotate with the object, you can detect collision, but you can`t use "get object collision x(),y() or z()"...

I am not sure if this is faster, but you can check if the objects are overlapping with variables and set the collision off, and if the other objects variables is bigger or smaller than your variables, then collision=1.



This will only work with boxes though, not high complex levels like in SoulHunter.

Suicide is away of telling God, You can’t fire me I quit !!!!!
Yodaman Jer
User Banned
Posted: 21st Oct 2008 17:18 Edited at: 21st Oct 2008 17:27
That's a bit much to chew on...::om nom nom nom::
I believe that my DarkBASIC is enhanced. I just quickly knocked together a small castle hallway test engine (mostly just to test out some lighting ideas I had) yesterday, but I haven't gotten around to adding collision. On the bright side, it runs at the pace I set it to..but that's because there's only four objects.

I was just asking if it's possible to use box collision on a single object, such as an entire level built in Anim8or. Would it then only apply the collision to the object's 'limbs'?

Thanks for those examples, they do help. I'm really interested in rotating box collision..that gives me some ideas...

Sorry, forgot to give you the code to the engine...



You'll have to download the media from here. Be sure to save the code in the folder "castletest"!

Yodaman Jer

Guitarist, 3D modeler, Video editor, Renaissance Faire performer, Christian, and all-around GEEK. With a capital 'G'.

Attachments

Login to view attachments
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 21st Oct 2008 18:01 Edited at: 21st Oct 2008 18:14
@Yodaman
Just a few notes to add to what TheComet had posted.

1. Whenever using DBC collision, always use Set Object Collision On to turn collision detection on for those objects that need it, and always use Set Object Collision Off to turn off those objects that don't. Even though by default the collision is on, DBC has some real quirky problems with collision that can come up.

2. If the size of your object changes (scale), turn collision off, reset the collision (Set Object Collision to sphere's, polys, whatever) turn the collision back on.

3. Be aware that there are two object box type collisions:
a. Set Object Collision to Boxes - this seems to be what you were talking about. This will set all of the limbs to box collision detection (drawing boxes around each limb).
b. Make Object Collision Box - this puts a sngle box around whatever area you define in relation to your object. It is much faster than Set Object Collision to Boxes and will only detect collision between other objects that have used MAKE OBJECT COLLISION BOX or areas that use MAKE STATIC COLLISION BOX.

MAKE OBJECT COLLISION BOX has a few advantages in that as TheComet stated, you can use it for sliding collision. That is, it will return a 3d point in space of collision from which you can adjust the position of your object.

In all cases with BOX collision, if your object is inside of the box, it will be considered to be colliding. So putting box collision on a building that has multiple hallways and intricacies may not be the best idea. You can build static boxes around each and every wall, floor, and ceiling, but depending on the complexity of the building, it may be more work than you want. For the interior of a building, I suggest setting it's collision to polygons detection for either Sparky's or DBC

With DBC polygon collision, however, you not be able to use detection for your object that is set by MAKE OBJECT COLLISION BOX but only from SET OBJECT COLLIION TO boxes,spheres, or polygons.

Here's a quick example that has a cube inside of a box. Using SET OBJECT COLLISION TO BOXES on each - collision is always happening even though the cubes aren't touching:



Now if I run the same code but change the "room" box to polygon collision, there should only be collision when the green cube hits a wall:



You'll notice the cube is off of the floor. For this example, I'm avoiding constant collision. If you look at my post about Getting Ground Height from X or 3DS objects, you could use similar methods to find the floor height at any given time and position your main character just above it.

Enjoy your day.
Yodaman Jer
User Banned
Posted: 21st Oct 2008 19:00 Edited at: 21st Oct 2008 19:02
Thanks Latch!

So, in your last example, I ran it and it told me when the cube ran into the wireframe mesh. But it didn't keep it outside of the mesh. To do that, would I just have to use variables like so?:

`Store positions
objangX#=Object Angle X(obj#)
objangY#=Object Angle Y(obj#)
objangZ#=Object Angle Z(obj#)


And then use those variables to re-position the player object from each individual polygon somewhere in the main loop?

Also, where can I get Sparky's DLL?

Thanks for all of your help guys!

Yodaman Jer

Guitarist, 3D modeler, Video editor, Renaissance Faire performer, Christian, and all-around GEEK. With a capital 'G'.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 21st Oct 2008 22:00 Edited at: 21st Oct 2008 22:06
Sparky's

The collision positioning can be tricky because the collision detection is constant (you can slide a little deeper into the collision before you get out so the distance to get out can change). Basically, you want to keep track of your position before any collision, and then move back to this position when collision occurs.


With DBC if you do your checks carefully and keep track of the distance in the z and x direction (positions before and after collision detection), you can even do sliding collision.

With Sparky's you can find the point of collision by casting a ray in various directions and create sliding collision using this information.

Enjoy your day.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 22nd Oct 2008 09:22 Edited at: 22nd Oct 2008 12:38
Quote: "Whenever using DBC collision, always use Set Object Collision On"


An excellent point and very good help, Latch, but isn`t the collision on automatic when you make a collision box? And if you make an object, the collision is on already, isn`t it?

Because I only ever use "set object collision off" if I don`t need collision, otherwise it`s on...

Quote: "I was just asking if it's possible to use box collision on a single object, such as an entire level built in Anim8or. Would it then only apply the collision to the object's 'limbs'?"


No, if you use box collision for and object with limbs, it will put a hole box around the hole object... You need polygon collision, which runs really slow in DBC. That`s why you need sparky`s.

So, I don`t think I really explained well, I thought you were a little more advanced. Anyway, this is how you use box collision:

First, you make and object:



Now, you set up the collision box. So, you use the command "make object collision box" to put a collision box around the object:

With a collision box, you need to define the top left behind corner and the bottom right front corner of the cube. My cube is 10 wide, 10 high and 10 long. So, the top left behind corner is at x=-5, y=-5, and z=-5. And the bottom right front corner is at x=5,y=5 and z=5. If you set the flag to 0, you have a non-rotating collision box, if 1 then it rotates with the object, but you can`t use it for sliding collision. So:



OK, now do the same with your player:



Because my player is 4 wide, 4 high and 4 long, the top left behind corner is at x=-2, y=-2, z=-2 and the bottom right front corner is at x=2, y=2 and z=2. The flag has to be 0, or sliding collision won`t work.

So, we set up the objects. Now to the main loop. Before you start collision detection, you have to get the positions of your player object:



After that should be the controls. Here I will just use space for jumping and arrows to move left and right:



OK, now to the gravity: All of the variable changing MUST be before the collision. So here, I will decrease the variable "grav#" and add it to the y value of the player. this will decrease the players y position:



And to make sure the player doesn`t fall forever, we position it at the start again if it falls:



Now to the collision. The command get object collision x() will return a value of how deep an object is stuck in another collision box. As well as the commands get object collision y() and get object collision z(). You can use these values to pull the player out of the collision box it`s colliding with, so it doesn`t sink into it.NOTE: THESE COMMANDS WILL NOT WORK WITH ROTATING COLLISION BOXES!

The command object collision(obj1,obj2) will return a value of one if the two objects are colliding, else 0 will be returned. if you set obj2 to 0, the command will return the number of the object that obj1 is colliding with:



Now update the player with it`s new positions:



And the hole thing will look like this:



Hope this helps you a little with your sliding collision! Once you get this, you can move on to advanced polygon collision.

TheComet

Suicide is away of telling God, You can’t fire me I quit !!!!!
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 22nd Oct 2008 12:40
Sorry for the double post, just to let you know that I edited my above post with a small sliding collision tutorial!

Hope it helps!

TheComet

Suicide is away of telling God, You can’t fire me I quit !!!!!
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 22nd Oct 2008 18:07
@TheComet
Great example! (actually kinda fun to try and get the cylinder back on the box!)

Quote: "An excellent point and very good help, Latch, but isn`t the collision on automatic when you make a collision box? And if you make an object, the collision is on already, isn`t it?"


Yes, supposedly, and it defaults to SET OBJECT COLLISION TO BOXES but I've dealt with it enough to have it fail when relying on the defaults. It's been most successful when I've always managed the state of the collision myself (on, off).

Quote: "No, if you use box collision for and object with limbs, it will put a hole box around the hole object... You need polygon collision, which runs really slow in DBC. That`s why you need sparky`s."


Just in case you weren't aware, there are two types of box collision for objects:

1. SET OBJECT COLLISION TO BOXES - this will put a collision box around every limb

2. MAKE OBJECT COLLISION BOX - this allows you to draw a single box around the object - useful for N S W E Up Dn sliding collision detection.

@Yodaman
Also be sure to check out TDK's tutorials:

Collision Made Simple

Enjoy your day.
Yodaman Jer
User Banned
Posted: 22nd Oct 2008 20:03
Thanks guys!

Yodaman Jer

Guitarist, 3D modeler, Video editor, Renaissance Faire performer, Christian, and all-around GEEK. With a capital 'G'.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 23rd Oct 2008 09:23
Quote: "@TheComet
Great example! (actually kinda fun to try and get the cylinder back on the box!) "


Thanks Latch!

Quote: "Just in case you weren't aware, there are two types of box collision for objects:

1. SET OBJECT COLLISION TO BOXES - this will put a collision box around every limb

2. MAKE OBJECT COLLISION BOX - this allows you to draw a single box around the object - useful for N S W E Up Dn sliding collision detection."


Sorry, I think I didn`t explain well again. You can use SET OBJECT COLLISION TO BOXES for objects with box-shaped limbs, but for complex high definition levels you need polygon collision(like for a landscape) or a level from soulhunter.

TheComet

Suicide is away of telling God, You can’t fire me I quit !!!!!

Login to post a reply

Server time is: 2025-06-07 08:13:28
Your offset time is: 2025-06-07 08:13:28