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 / Creating a boundary for a bouncing sprite

Author
Message
MindTrixz
14
Years of Service
User Offline
Joined: 22nd Sep 2010
Location:
Posted: 26th Oct 2010 01:32
Ok so I have been having two issues with my code. I cant seem to get the random balls to move a steady pace about the screen and two I need to create a boundary around the screen for the sprites to bounce off of until the two sprites collide. Any help would be great...

Attachments

Login to view attachments
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 26th Oct 2010 19:44 Edited at: 26th Oct 2010 19:50
Regarding that you always add the same amount to the X and Y coordinates, theoretically the balls should move the same amount every loop. However, it's not quite good to leave the control of movement speed entirely to the screen refresh rate (and 10 is rather low for frame rate, it won't give you a smooth movement). I think you should consider implementing time-based movement.

Maybe you can also consider using dbMoveSprite, then you can give the sprite a direction and move it diagonally across the screen without calculating X and Y separately.

For bouncing balls back from the screen edge, I have two ideas:

1. Watch the coordinates of the ball and if it goes outside the screen area (for example: if x < 0 || x >= screenwidth), then bounce it back.

2. Make four rectangular sprites, one for each screen edge, and position them just outside the visible screen area. Then you can use dbSpriteCollision or dbSpriteHit, to detect when the ball collides with an "edge sprite". I haven't tested but I'm pretty sure collision detection should work even if one of the tested sprites is outside the visible area. (If not, you can position the edge sprites to make one pixel of them visible.)

Regarding the code: you don't need to call dbRandomize again in the collision function, that's enough once in the setup function. Also, this code:



would be much shorter like this:



EDIT: Hmm, I just noticed you provided the image as well and tested the program. The sprites are jumping all over the place, so there is some other problem here than time-based movement. I'll see if I can provide more advice a little later.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 26th Oct 2010 20:02
OK I found why those sprites are jumping around wildly. You start off both sprites at the same coordinates, so they will immediately collide with each other. When you detect a collision, you also set both of them to the same (random but same) coordinates, so they will collide again and receive new coordinates again... Assign different coordinates to the sprites:



Of course this still does not guarantee that the sprites will not be placed into collision again, if similar random numbers are generated, but the chances for that are much lower. If you implement "bouncing back" properly, then this randomness will be removed anyway - I suppose - since it should be replaced with moving the ball with the same speed in a different direction.
MindTrixz
14
Years of Service
User Offline
Joined: 22nd Sep 2010
Location:
Posted: 28th Oct 2010 03:28
thanks for all the help in clearing some stuff up. But i got one more thing I am using dbMoveSprite yet the two bowling balls are bouncing all over the screen. how do I get them to move at a steady pace?
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 28th Oct 2010 20:38
Have you changed the coordinates as shown above, so that the two balls are placed on different positions? Once I corrected that, the balls moved steadily.

Login to post a reply

Server time is: 2024-11-19 16:39:49
Your offset time is: 2024-11-19 16:39:49