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 / Sprite Collision

Author
Message
Marsh0
15
Years of Service
User Offline
Joined: 18th Mar 2009
Location:
Posted: 28th Jan 2010 18:14
Hello i am making a simple Pong game. The problem i have run into is that i cannot get dbSpriteCollision to work. Once in awhile it will work but the majority of the time the ball will go right through the paddle.

here is my code. The return is there so that it will exit when it hits so i know something actually happened.



It works fine for the computer though i have it lining both x coordinates up exactly which is probably why it works. Any ideas on how i would detect the second the ball hits anywhere on the paddle?

Thanks for any help

www.touchofdeathproductions.com
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 30th Jan 2010 11:34
In the first sentence you mention dbSpriteCollision, then show a code which contains dbSpriteHit. These are, AFAIK, not interchangeable. dbSpriteHit detects when the sprites are lined up alongside each other, while dbSpriteCollision detects overlap. For a fast moving ball, only overlap detection is suitable because it will almost never be lined up with the paddle.

If you use dbSpriteCollision and still cannot get it working, post some more code because from this is not really possible to spot the problem.
Marsh0
15
Years of Service
User Offline
Joined: 18th Mar 2009
Location:
Posted: 30th Jan 2010 17:44 Edited at: 30th Jan 2010 17:58
I used both and could not get either to work. I am not sure what other code i should post. Its in a loop it should work fine. Here is my entire code. its not to long at the moment.



www.touchofdeathproductions.com
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 30th Jan 2010 19:19 Edited at: 30th Jan 2010 19:22
Do I understand correctly that you want to use the same sprite for both the player paddle and the computer paddle? That will never work. You can use the same image but not the same sprite. The collision is detected by sprite coordinates, so you need two separate sprites, each with their own coordinates.

A few other tips:

You mix the dbSprite and dbPasteSprite calls, only one of these display methods should be necessary. dbSprite displays and positions the sprite, you don't need to paste it again. dbPasteSprite is when you want to control the sprite drawing yourself and that is usually used after hiding the original sprite first.

In dbSpriteCollision, you don't need to repeat the sprite ID numbers twice in reverse order, once is enough.

If you want to use dbSpriteCollision, then be careful with dbOffsetSprite. It displays the image at a different place than the sprite coordinates and I don't know which coordinate set is then used for collision detection... it may be correct but it needs some testing, I never tried.

Never use dbStr like this:



All Dark GDK functions which return a char pointer create a new string in memory every time they are called and if you do not delete the returned string, you create a memory leak. Use sprintf with a char array if you want to display a number in string format.

You use animated sprites like sprite sheets. Although they can be used like that, but this is not their purpose. They are mainly for creating animation and to be played with the dbPlaySprite command. Either create separate images for the sprites (advisable since the paddle sprites have to be separated anyway from each other) or if you want a sprite sheet, you may use dbSetSpriteTextureCoord to set the area of the image which that sprite should display.

General C++ things: Instead of the four "if" statements checking the value of ballmove, you could use a switch statement. Also, variables can (and should) be initialized with a value when they are declared, you don't need to first declare and then assign a value:

Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 30th Jan 2010 19:28 Edited at: 30th Jan 2010 19:28
P.S. If you use two sprites for player paddle and computer paddle, you do not need to check coordinates to see which paddle the ball collided with. You just check collision with both sprites.

And one more coding tip. Instead of this:



(which is wrong anyway because in the last line it should check 1 probably, not a second zero), you can write this:

Marsh0
15
Years of Service
User Offline
Joined: 18th Mar 2009
Location:
Posted: 30th Jan 2010 19:43
Thanks for all the advice Mireben, i figured it would save resources or something to make only one sprite for both paddles instead of two. Which is why im pasting the sprite and not just placing it because two paddles work off the same one . Ill make it into two sprites this will hopefully solve my problem . Thanks for the random code to i was thinking something like that but never really wrapped my head around it.

Also the paddles will be animated so no problems there. Also could you give me a example of a switch statement if you have some time? i kind of just jumped into c++ and didn't really learn how to do things properly. I think i have seen switch statements but have never written one.

www.touchofdeathproductions.com
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 31st Jan 2010 10:24
Sure, here is an example. This was the original version:



The switch:



There are good C++ references at several sites, it's worth to have a look. (A good book may be even better, but the on-line references are very useful and comprehensive too.)

http://www.cplusplus.com/doc/tutorial/
http://msdn.microsoft.com/en-us/beginner/cc305129.aspx
http://www.learncpp.com/
Marsh0
15
Years of Service
User Offline
Joined: 18th Mar 2009
Location:
Posted: 31st Jan 2010 17:29
Ah sweet thats handy thanks!

www.touchofdeathproductions.com

Login to post a reply

Server time is: 2024-11-20 05:26:31
Your offset time is: 2024-11-20 05:26:31