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.

Newcomers DBPro Corner / Massive Collision Resource - Compilation of All Snippets

Author
Message
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 17th Dec 2006 03:35 Edited at: 6th Jan 2007 13:41
Collision Code Snippet Resource





- Intro

Pretty much every week, theres at least 1 (usually more) question regarding getting collision working in games. Its usually one of the first questions newcommers have, but usually the last to get answered. So, in hopes of lightening the load of these questions and providing a quick way for people to show newcommers all of the collision snippets on the TGC forums, Ive made this thread.

Below is a compilation of all of the usefull code snippets, dlls, links to other sites/resources, and anything else that I could find to do with collision, hope it helps.




---o Sparky's Collision Dll (FREE)

---o Nuclear Glory Collision ($)



---o Kensupen's BSP Style Sliding Collision Snippet

---o G-Man/Lost In Thought's Sliding Collision Snippet

---o TDK's DB Collision Made Simple Tutorial



---o RUCCUS' Collision Function (easy to implement)




---o DBP Native Collision Commands Example:



---o Matrix Collision Using the Get Ground Height() command
(easily adapted to Advanced Terrain by using the Get Terrain Height() command instead.



---o Raycasting using DBP's Internal Intersect Object() command.



---o Camera Collision in Third Person View:





I threw together this code to give a better idea of the kind of FPS rates each method of collision gives you. It starts out using Sparky's dll. Pressing 2 will switch to DB Intersect Object, pressing 3 to DB Polygon Collision, 1 back to Sparky's, and 0 if you'd like to turn off collision all together.

My results (the FPS at the time of a collision occuring)

Sparkys Dll: 345 FPS
DB Intersect Object: 335 FPS
DB Polygon Collision: 270 FPS

Here's the code;



Just a note, the difference between Sparky's dll and the DB Intersect Object command's FPSs might not seem too large now, but if you use multiple objects to check collision against, or use an object with a larger polygon count, you'll see where Sparky's dll truly shines. Feel free to put in a map in place of the sphere there.



Make your collision faster/smarter: This is a method used in tonnes of games out there, even the console games. To make your collision responses faster and less probable of a buggy collision occuring, make collision maps. Collision maps are basically a second, extremely low poly, untextured version of your main map, that's hidden in your game world. You then detect collision with this collision map instead of your main map. Not only does this method tend to increase FPS drastically, but as I said above, it allows you to make your collision responses more predictable. By this I mean, if you had an extremely irregular shaped object, or a small object floating in the air, or a bunch of cracks in the cement of a city model, your player *might* get positioned weirdly depending on how you're handling collision. If you're trying to squeeze out more fps from your game, make a collision map, in the past it's increased my FPS more than 300 percent.

Breaking up the Workload: Another great tip is to cut your map up into smaller grid sections. Then, only detect collision with the closest grid section to the user. If you have a 100000 polygon map (not uncommon), and divided it up into just 10 grid pieces, you would reduce the amount of polygons being checked for intersection in your world down to just 10000. Thats a 90000 polygon difference! Believe me, it makes all the difference.

Dont check if you dont have to: If your player is skydiving out of a plane attempting to land into the main mission area for their next objective, are they going to be running into anything in the air? If your player's not moving, and nothing else is moving that could move them, are they going to ever hit anything? Chances are they wont, so why bother detecting for collision at those times? Another huge FPS increase.

Bullet Calculations:: When most users start out making their first FPS, they automatically assume they need to make tonnes of bullet objects when the user fires their gun. But in reality, as long as the gun being fired is fairly modern, chances are it'll be shooting bullets so fast that all you'll see is the bullet spray. Why make the bullet objects then? Instead, send an intersection check going from the player's gun to a point infront of the player's gun far away, if you detect an intersection with an enemy, decrease their health. This is what all proffessional games do. How do they get the bullet spray effect? Plains or spheres, depending on the effect. Usually a plain is positioned between the player's gun and where the bullet hits, and is stretched to fit between the 2 points. The plain is then textured with a transparent gunspray image, and hidden/shown repeatedly. The same effect can be achieved with using a sphere with an image of gunspray on it, examples of using spheres can be found in the free game "America's Army".

Bullet Decals: For this one you'll want to use Sparky's dll. If you look in sparky's helpful example programs, you'll see one where he constantly positions a plain on the object being clicked on with the mouse, and he orients this plain to face the same direction as the polygon hit. Thats a perfect decal system right there; just change the code to leave the decals when the user fires a bullet, and have them fade after a time period. Instant bullet holes.

- Other Useful Links

Overview of 3D Collision

- Outro
Thats all for now, feel free to post with links to informative sites, other code snippets, plugins, or theories on collision detection that I may have missed, and Ill add them to the list if I think they fit the shoe.

- RUC'

Attachments

Login to view attachments
Gil Galvanti
19
Years of Service
User Offline
Joined: 22nd Dec 2004
Location: Texas, United States
Posted: 17th Dec 2006 07:16
Awesome, thanks a ton, I've always had problems with collision, and that first Collision Code Snippets without plugins one is amazing, I'll definetly be using that. Thanks again .

Pirates of Port Royale
Live the life of a pirate.
Dracula
18
Years of Service
User Offline
Joined: 7th Aug 2006
Location: DBP Recreation of Castle Csejthe
Posted: 17th Dec 2006 13:56
very nice. wish this had been here last year... lol
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 17th Dec 2006 14:13 Edited at: 27th Dec 2006 19:00
Drac, it had, you just didnt search like people told you to.

Attachments

Login to view attachments
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 17th Dec 2006 22:21
sticky goo

RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 17th Dec 2006 23:26 Edited at: 28th Dec 2006 03:00
Cool, thanks.

Attachments

Login to view attachments
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 17th Dec 2006 23:28
thank you for trying to make this place a little more structured and making it easier to solve the most repetitive questions.

Gil Galvanti
19
Years of Service
User Offline
Joined: 22nd Dec 2004
Location: Texas, United States
Posted: 19th Dec 2006 05:25
I have a question, on Kensupen's BSP Style Sliding Collision Snippet, I can't figure out how to adjust the hieght with which it will detect an object as a ramp object instead of a wall object, because, honestly, I don't understand much of the code. Anyone know?

Pirates of Port Royale
Live the life of a pirate.
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 21st Dec 2006 14:37 Edited at: 28th Dec 2006 03:06
Gil sorry, I cant help you much there. I didnt really get to understand Ken's code snippet.

Just added a link to TDK's Collision Tutorial he just made.

Attachments

Login to view attachments
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 27th Dec 2006 14:44
Ruc', you rule. Wish there was this kind of stuff around when I started, because it was a big problem for me, but luckily, it wont be anymore

To everyone:
I really recommend Ruccus's collision function. It's really easy to use, and because all it really does is return positions, it can be used for practically anything. I've used it in my physics library, and in an FPS game I was making for the NVIDIA competition

Again, great work, and thanks to all the people who made this post possible.

RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 27th Dec 2006 18:09 Edited at: 28th Dec 2006 03:08
I know how you feel Zoto, when I firs tstarted (around the same time as you) collision was the deciding factor for all of my projects being done, I could never figure it out. Sparky's dll is a godsend for me.

Attachments

Login to view attachments
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 28th Dec 2006 02:26 Edited at: 28th Dec 2006 05:12
removed duplicate link

RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 28th Dec 2006 02:54 Edited at: 28th Dec 2006 03:12
Thanks indi, but that was already up there in the original post .

Attachments

Login to view attachments
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 28th Dec 2006 03:22
Making this post to get another image file up, but while Im at it, I guess Ill post whats been added;

- Added images to make it look a bit neater
- Added cam collision snippet

Attachments

Login to view attachments
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 28th Dec 2006 05:11
oops sorry mate.

Code Dragon
18
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 29th Dec 2006 14:53 Edited at: 29th Dec 2006 19:24
I'm sure this will help me a lot, I don't code in 3D often yet, I'm still a 2D programmer. I'm pretty much clueless on how to do collision in 3D, so yeah this should be a big help.

I've pretty much mastered 2D, so if you're going to make a section on 2D collision here's a code snippet I wrote to do perfect sliding collision on sprites.

"Did I ever tell you how I got the nickname the Dragon of the West?"
"I'm not interested in a lengthy anecdote, Uncle."
"It's more of a demonstration really."
Don Malone
21
Years of Service
User Offline
Joined: 27th Apr 2003
Location: Birmingham, Alabama
Posted: 10th Jan 2007 03:11
I love the structure of the listing and the information provided.

Thanks Ruccus.

Making nothing for the third straight year.

dononeton
20
Years of Service
User Offline
Joined: 12th Jun 2004
Location: Tusaloosa, AL : USA
Posted: 14th Jan 2007 02:52
Breaking up the Workload. How would you break up the workload?
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 14th Jan 2007 03:02
Are you talking about the "splitting up the map" tip?

dononeton
20
Years of Service
User Offline
Joined: 12th Jun 2004
Location: Tusaloosa, AL : USA
Posted: 14th Jan 2007 03:04
Yeah that is what I mean
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 14th Jan 2007 14:03 Edited at: 14th Jan 2007 14:05
the name says it all... You open your map in a modeller, and cut it up into smaller sections, and export each smaller section seperately. Then load up all of the sections in your game, and only check for collision with the section close to your player.

Maybe this'll help explain:



The red dot is the player, as the player moves on the map, the grid section that the player is in lights up. You'd only check collision with this grid section, and ignore all of the others. If you assume that each grid section has 5000 polygons, instead of checking the entire map for collision (checking 45000 polygons), you're only checking 5000 at a time. That huge decrease in checks can in most cases crank up your framerate a lot.

Attachments

Login to view attachments
dononeton
20
Years of Service
User Offline
Joined: 12th Jun 2004
Location: Tusaloosa, AL : USA
Posted: 14th Jan 2007 17:36
I was think that was the way to do it.So the first thing you should determine is how many polygons you want to render at anyone time, I guess the same can so for physics and the ai. So if you have a 50000 polygon level and you can render 5000 polygon you then do 50000/5000 and get 10. Thats 10 different grids or 10 seperate models you have to position in your game.


So design the level with room, hallways for interior levels and for outside levels try do make the terrain in pieces like when you get so far you come to a small canyon you can go through have a blured screen and fades out and fades back in, and your are on the otherside. It makes it seem to be large and you can check for collision in the area you are in only. And on interior that is on the terrain well samething. You go in and the fading thing happens and the game stops checking collision on the outside and starts checking for collision on the interior.


I guess in each area will have its own lightmap

thanks Ruccus
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 15th Jan 2007 01:18
Just keep in mind, it doesnt have to be a square grid. You could do landmarks, like a mansion would be 1 section, an old tree would be another.

And ofcourse, if your player isnt moving, dont check for collision, if they are moving but you know for sure that they wont hit anything (like if theyre sky diving), only check for collision when you know they can hit something (like if they skydive and reach a low altitude, check for collision).

- RUC'

hallo
17
Years of Service
User Offline
Joined: 26th Jan 2007
Location: Behind You!
Posted: 27th Jan 2007 13:44
cool

Hallo
Cian Rice
20
Years of Service
User Offline
Joined: 8th Jun 2004
Location:
Posted: 18th Feb 2007 17:43
Sorry to bump this up but I'm having a problem with Kensupen's collision when I try to make it 3rd person and implement it into my own code. What happens is that whenever I move down a slope it makes it act as if the character is moving down stairs. How could I fix this ? I'll post up code if needed.

Thanks,
Cian

[url=www.waza-online.com/waza][/url]
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 18th Feb 2007 18:00
You should email ken about that, I cant figure out how his system works.

aluseus GOD
17
Years of Service
User Offline
Joined: 19th Mar 2007
Location: I\'m here. Now I\'m there. I keep moving
Posted: 20th Mar 2007 00:55
Well, I'm new, and it doesen't say anything about 2d collision...
I was wondering if you could tell me how to amke collision from the top and left diferent...
vibe runner
18
Years of Service
User Offline
Joined: 7th Aug 2006
Location: The Future
Posted: 8th Apr 2007 21:02
aluseus: because this is 3D, for 2D look at the SPRITE HIT and SPRITE COLLISION commands.

Login to post a reply

Server time is: 2024-11-25 20:46:17
Your offset time is: 2024-11-25 20:46:17