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.

DarkBASIC Professional Discussion / Not calculating angle correctly?

Author
Message
Zeus
20
Years of Service
User Offline
Joined: 8th Jul 2006
Location: Atop Mount Olympus
Posted: 13th Apr 2011 15:56
I am attempting to convert my AS2 brick breaker game to DBP and have had much success up until the point where the ball hits the paddle. I have now switched the game over to 2d which I have found to be what I wanted in the first place. Well when the ball hits the paddle the ball simply starts bouncing straight up and down instead of bouncing at an angle when it hits the left or right of the paddle. The function that calculates the angle is called CalcBallAngle() in the code. Did I do something wrong there? Please correct me where I am wrong because the AS2 code worked perfectly. I posted my entire code below and the single function by itself underneath the source code. I commented the code and indented to make it easier to read.

Source code:


CalcBallAngle() Function (by itself):


Thanks!
Zeus

Quel
17
Years of Service
User Offline
Joined: 13th Mar 2009
Location:
Posted: 13th Apr 2011 16:10
Use atanfull(X distance, Y distance) to calculate the angle from the last X and Y movements of the ball right before hitting the paddle.
Zeus
20
Years of Service
User Offline
Joined: 8th Jul 2006
Location: Atop Mount Olympus
Posted: 13th Apr 2011 16:13
Thank you but where would I place this in the code?

Jimmy
22
Years of Service
User Offline
Joined: 20th Aug 2003
Location: Back in the USA
Posted: 13th Apr 2011 16:50
I don't know if this will fix it, but you could try using floats when calculating your hit percentage. As it stands, your 0.5 modifier is just getting rounded off due to integer conversion.



NaGaFailMo.
Zeus
20
Years of Service
User Offline
Joined: 8th Jul 2006
Location: Atop Mount Olympus
Posted: 13th Apr 2011 16:57
Perfect! Thank you soo much.

American Warfare is still in development - it is currently postponed until I build up the skill to complete it.
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 13th Apr 2011 17:03
Why do you need the ball angle?

Can't you just use those velocities, as if this is for pong, or breakout even, you won't need angles, just invert those velocities and affect them. Like if you hit the ball with the paddle, work out the difference between the paddle centre and ball centre, and affect the X velocity depending on that.

Like, if your paddle is 20 units wide:

If bally>paddley
xdif=ballx-paddlex
if xdif>-10 and xdif<10
ballys = abs(ballys)
ballxs=xdif/10.0
endif

And to do some wall collision:

If ballx<10.0
ballxs=abs(ballxs)
ballx=10.0
Endif

If ballx>630.0
ballxs=-abs(ballxs)
ballx=630.0
Endif

If bally<10.0
ballys=-abs(ballxs)
bally=10.0
Endif


But, say you wanted to work out the angle that ball is moving at...

Ang=atanfull(ballxs,ballys)

Health, Ammo, and bacon and eggs!
Zeus
20
Years of Service
User Offline
Joined: 8th Jul 2006
Location: Atop Mount Olympus
Posted: 13th Apr 2011 23:42
What I meant to say was which direction the ball will go once it hits the paddle. Sorry for the mistake.

American Warfare is still in development - it is currently postponed until I build up the skill to complete it.

Login to post a reply

Server time is: 2026-07-17 13:59:25
Your offset time is: 2026-07-17 13:59:25