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.

Code Snippets / [DBP] 2D sliding collision system (pixel accuracy, more than 230 FPS on my computer)

Author
Message
Atreides
20
Years of Service
User Offline
Joined: 11th Oct 2003
Location: Switzerland (but NOT on a mountain !)
Posted: 7th Apr 2004 22:59 Edited at: 8th Apr 2004 12:49
Here's my last collision system, I hope you'll like it
So, how does it works ? I'll try to explain it, but since I don't speak English well, it may be a bit hard to understand me

First, I think it would be good that you test it ; you can draw a map (bitmap please, not jpeg ) or download this one : http://sensai.free.fr/attreid/map.bmp
Okay, my map is ugly and blue, but if you dislike blue, just open it with Paint and replace the blue with red (or pink if you're a Lady ^_~).
If you want to draw a map yourself, be sure that the area around the coordinates {350,350} doesn't contain anything, and that it's a 800x600 pixels image !

Okay, now, you may launch it and test it


If you're still reading, then I guess you're interrested by my collision system (or do you like my map so much ?).. So I'll do my best to explain how it works

Line 5, I load the image and put it in a memblock .. I don't remimber why I didn't load an image but loaded a bitmap .. There was maybe something in the water

Look ad the line fourty, I give informations to the coll type (position X, position Y and Radius), then there's a gosub and I take the informations from the coll type.. Interresting, isn't it ? So everything's in the gosub .. Let's see it

On the way, one can see a few functions :
mem_point() gives the color of a pixel that's stocked in a memblock
arrondit() gives the integer of a float, but add one when the first decimal number is superior or equal to five
colliz() looks around the sphere if there's a collision (gives back 1) or not (gives back 0)

Then, my dear gosub. I like gosubs. They're lovely, with their two points and ... hmm.. I should shut up ^__^
So, as you can see, at the beggining, I put several variables to 0 ; I need them empty for the collision system

As you can see, there's almost the same code twice. It's for each side (left & right) of the circle. I'll explain only the left-handed one - the first one.
I look only at the pixels which are around the circle ; not the ones which are in it (it would be far too slow)

First step, I look if the pixel is in the bitmap - if it isn't, there'll be a problem with the memblock
Second step, I look the color of the pixel => if it's superior to 0, the pixel isn't black => there's a collision on that point => I add the position to the coll.center[x,y], I increase the number of collision points (coll.nbr), and I tell there's a collision (coll.hit=1)
Okay, now, I've all the informations, what should I do ? I should use them or drink a beer. Because I'm a kind guy, I'll drink my beer later, for now, I explain you how I use the informations ^^

first step : I've to use the informations only if there's a collision
Second step : I find the center of gravity of all the collision points (I divide the sum of their positions by their number)
Third step : I ask my dear DBP the angle between the center of the sphere and the center of gravity of the collision points
Fourth step : I move the sphere along the angle until there's no collision again

And ... that's all ^^

I hope you understood what I said ^^ .. .. .. Sorry for making you sleep ^_^
I've never been good at explaining things, especially not in a foreign language

The sleeper must awaken !
sami
20
Years of Service
User Offline
Joined: 31st May 2003
Location: France
Posted: 8th Apr 2004 23:58
I don't speak English well too but I understand what you should say.
Atreides
20
Years of Service
User Offline
Joined: 11th Oct 2003
Location: Switzerland (but NOT on a mountain !)
Posted: 11th Apr 2004 15:42
en gros, je fais la liste de tous les points de collision entre le bord du cercle et l'image, je cherche leur centre de gravité, je cherche l'angle entre le centre du cercle et ce centre de gravité et finalement, je recule le cercle selon cet angle jusqu'à ce qu'il n'y aie plus de collision

The sleeper must awaken !
flibX0r
21
Years of Service
User Offline
Joined: 14th Feb 2003
Location: Western Australia
Posted: 11th Apr 2004 16:38
Code doesn't work. i think it might have cut part of it off.

it doesn't recognise this:


Current Project: Interactive DBPro Showcase Example
Project Status: Game Engine 90%
http://www.w3th.tk <-- Soon to have some content
Atreides
20
Years of Service
User Offline
Joined: 11th Oct 2003
Location: Switzerland (but NOT on a mountain !)
Posted: 12th Apr 2004 16:01 Edited at: 12th Apr 2004 16:02
Sometimes, I wonder why does the forum hates me ^^
Usually, it cut only the end of my snippets ; I looked at the bottom and it was all right .. But this time, it cut the middle of my snippet

Edit>
Grrr, I posted again, and there's the same problem ... Here's an update :



The sleeper must awaken !
sami
20
Years of Service
User Offline
Joined: 31st May 2003
Location: France
Posted: 13th Apr 2004 02:13
Why do you speak French Astreides? I make a mistake in my message!
Atreides
20
Years of Service
User Offline
Joined: 11th Oct 2003
Location: Switzerland (but NOT on a mountain !)
Posted: 13th Apr 2004 15:58
no, but under your name, it's written that you live in France, and usually, French speak French

The sleeper must awaken !
sami
20
Years of Service
User Offline
Joined: 31st May 2003
Location: France
Posted: 14th Apr 2004 00:13

Login to post a reply

Server time is: 2024-05-12 22:51:53
Your offset time is: 2024-05-12 22:51:53