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.

Dark GDK / How Does 2D Collision Work?

Author
Message
Azeem
16
Years of Service
User Offline
Joined: 6th Jan 2008
Location: Metropolis
Posted: 19th Jul 2008 15:45
Hi ive been trying to try and get my 2d Sprite to hit another image and stop, unfortunatly this hasnt worked too well.
Can anyone supply me with some code that would help me with object collision in 2d GDK?

Thanks
Azeem

Azeem - Game Programmer In Training...
Manfred
16
Years of Service
User Offline
Joined: 23rd Jun 2008
Location: Austria
Posted: 20th Jul 2008 01:30
Hi Azeem,

what exactly do you want to do?
The functions dbSpriteCollision() and dbSpriteHit() just check if the specified sprites overlap (I didn't find out any difference between the two by now). As far as I know these two just work with sprites, so if you want to check collision between an image and a sprite, you can make a sprite out of your image, or write a own collision detection for images and sprites, with which I could help you if necessary.
What do you mean with stop: that the sprite cannot overlap the image?

Casting, the art of manipulation; further information every day in TV.
Manfred
16
Years of Service
User Offline
Joined: 23rd Jun 2008
Location: Austria
Posted: 20th Jul 2008 02:30
The problem with all collision detection methods is that they just react if the objects are already overlapping, I'm searching for a solution to this problem too.

Casting, the art of manipulation; further information every day in TV.
Azeem
16
Years of Service
User Offline
Joined: 6th Jan 2008
Location: Metropolis
Posted: 20th Jul 2008 16:47
Quote: "What do you mean with stop: that the sprite cannot overlap the image?"


yh thats what i mean, im having a problem figuring this out. I have tried to get all four corners of the object and tried to see that if the other sprite hit these coordinates he would move 2 x pixels back while hes trying to move 2 pixels foward each time which means he wouldnt move at all when he hit the object but that didnt work well.

Quote: "The functions dbSpriteCollision() and dbSpriteHit() just check if the specified sprites overlap"


I honestly despise those functions at first i thought they would make the object solid too but unfortunatly... nope. They just return a value.

If i come up with a solution i'l be sure to post it here.
Thanks for the help.

Azeem - Game Programmer In Training...
Merak Spielman
16
Years of Service
User Offline
Joined: 14th Jul 2008
Location:
Posted: 23rd Jul 2008 18:35
I figured this one out.
First, create a couple of global variables. I called them spriteposx and spriteposy.

At the beginning of each GDK loop, set them to equal the current location of your sprite (this code assumes your sprite is designated number 1):


Now you've stored your sprites' current position before you've tried to move it.

Now you move your sprite. I figure you've gotten this working so I won't post code for it.

Then you check for a collision. This code checks if the sprite has collided with ANY other sprite:



That might not be exactly what you want, since you might not want all sprites to be "solid."

Now, if that returns a value other than 0, we know that my sprite has hit something. So all we need to do is move it back to its previous position before the screen redraws:

Manfred
16
Years of Service
User Offline
Joined: 23rd Jun 2008
Location: Austria
Posted: 23rd Jul 2008 23:45
The difference between dbSpriteCollision() and dbSpriteHit() is that dbSpriteCollision() checks if the rectngular borders of the sprites are overlapping, dbSpriteHit() checks if the visible parts (the none transparent) are overlapping, so the second is more exact, but slower.

I've tried something similar as you did, but I didn't call dbSprite() to reset it to a stored position, I changed the actual position and then called dbSprite().
The main problem in my case is that collision is detected in one turn but the move happened in another. Do you have the same problem, or is it working correctly?

Casting, the art of manipulation; further information every day in TV.
Merak Spielman
16
Years of Service
User Offline
Joined: 14th Jul 2008
Location:
Posted: 24th Jul 2008 00:31
For me it seems to be working fine. The move into "overlap" and the re-move out of "overlap" both happen before the screen refreshes, so it's invisible to the player. Since I'm still mainly fooling around, I don't know if doing it this way would cause a slowdown if you have lots of interacting sprites.

The only problem with my code is all obsticles are "sticky." That is, when you run into them your sprite stops, no matter which direction it was moving. So if you're going diagonally, you don't slide along the edge of the obstructing sprite, you just come to a complete stop. Most games do the "slide along the edge" thing if you're moving diagonally. I'm trying to figure that out now.
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 24th Jul 2008 00:59 Edited at: 24th Jul 2008 00:59
Quote: "I honestly despise those functions at first i thought they would make the object solid too but unfortunatly... nope. They just return a value."


It would have been folly for TGC to make the commands stop sprites from overlapping. That would have left the programmer little control. Those commands do exactly what they should (Of course, an option for pixel-perfect collision would be nice.).

Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo

Login to post a reply

Server time is: 2024-09-30 03:15:59
Your offset time is: 2024-09-30 03:15:59