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.

2D All the way! / Trying to create ball bouncing off the wall...what am I doing wrong?

Author
Message
Pog Mo Hone
13
Years of Service
User Offline
Joined: 21st May 2010
Location:
Posted: 8th Jun 2010 22:16
The ball starts off going downward-right and then bounces back the way it came. When it goes up and hits the left wall it again goes back the way it came, back and forth....not sure what is wrong here...

//Ball moves, if reaches wall, bounces off
iBallX += iBallSpeed;
iBallY += iBallSpeed;

if ( iBallX < 0)
{
iBallX += iBallWidth;
iBallSpeed *= -1;
}

else if( iBallX > 1002 - iBallWidth)
{
iBallX -= iBallWidth;
iBallSpeed *= -1;
}

else if( iBallY < 0)
{
iBallY += iBallHeight;
iBallSpeed *= -1;
}

else if( iBallY > 750 - iBallHeight)
{
iBallY -= iBallHeight;
iBallSpeed *= -1;
}

"Wisemen say: Forgiveness is divine, but never pay full price for late pizza." - Michaelangelo
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 9th Jun 2010 11:29
Hi, are you using the DarkGDK as you may get more help from that board. Your problem could be you are using the one variable for both x and y increase and decrease. Try using two variables one for the x increase and decrease and one for the y increase and decrease.

Here is a code snippit that works in DBPro and you should be able to make sense of it.


Hope this helps

A clever person solves a problem, a wise person avoids it - Albert Einstein
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 9th Jun 2010 12:12
I think thats exactly it Hodgey - iBallSpeed, you need to have seperate X and Y, like fBallSpeedX and fBallSpeedY. The speed variables should probably be floats too.

That basic principle looks right enough though, decrement Y speed, if theres a collision then invert the speed. I'm not sure that decrementing the width and height from the position when theres a collision is the best method. I would suggest that you work out the zone left, right, top and bottom, and include the ball size in that - then set the coordinates to limit, rather than detract...

if ( iBallX > 1000 )
{
iBallX = 1000;
if ( iBallSpeedX > 0 ) iBallSpeedX *= -1;
}

if ( iBallX < -1000 )
{
iBallX = -1000;
if ( iBallSpeedX < 0 ) iBallSpeedX *= -1;
}

Something like that maybe.


Health, Ammo, and bacon and eggs!
Pog Mo Hone
13
Years of Service
User Offline
Joined: 21st May 2010
Location:
Posted: 9th Jun 2010 20:33
Nice, that was exactly it. Just added a seperate variable for the speed and vuala! Thank you both for your responses, and I will follow up on other posts under Dark GDK.

"Wisemen say: Forgiveness is divine, but never pay full price for late pizza." - Michaelangelo

Login to post a reply

Server time is: 2024-04-24 00:47:12
Your offset time is: 2024-04-24 00:47:12