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.

Newcomers DBPro Corner / Need some help with pong.....

Author
Message
EviLz
17
Years of Service
User Offline
Joined: 1st May 2007
Location:
Posted: 1st May 2007 18:21
Okay so I've recently just started out with Dark Basic and have worked on making a pong game on my own, well its totally done except for one thing, i need some help on how to change the angle that the ball would travel after it hits a paddle (based on where it hits the paddle if i can).
Ive tried everything i know and failed..can anyone help me out on how i might be able to do this?

The pact is sealed.
Code Dragon
18
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 1st May 2007 22:38
You simply invert the ball's velocity. If x velocity is vx, then simply use this code when it hits the paddle:

vx = -vx

By reading this sentence you have given me brief control of your mind.
EviLz
17
Years of Service
User Offline
Joined: 1st May 2007
Location:
Posted: 2nd May 2007 00:53
thanks! i figured it would be something simple like that

The pact is sealed.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 2nd May 2007 02:03
You could track how fast your paddle is moving and the direction it is coming from at the time of impact in order to determine what angle the ball should bounce off of it.

Just simply save the location of the paddle during each loop and when it updates you can calculate how far it has moved and the direction. Use this info to calculate the bounce direction.

LB
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 2nd May 2007 03:36
Quote: "thanks! i figured it would be something simple like that"


I don't think it is actually.

For a start, unless DBP has changed from DBC, (I haven't had chance to test it yet), I'm pretty sure that you can't negate variables like that. If it doesn't work, try vx = 0-vx.

But, the main thing is that code only reverses the direction on the X direction. It doesn't alter the angle based on the point of impact - or the speed of the paddle which is an aspect which could also be factored in.

If the paddle is on the right and moves up/down, then the ball will be moving from left to right on impact.

Normally you would negate the X value as shown by Code Dragon and leave the Y value alone - and the bounce angle remains unchanged.

Altering the Y value would be needed to alter the angle.

If it hits the paddle dead in the middle then you might decide to have no Y velocity when the ball is returned. You might also want it to be that the further from the centre of the paddle is hit, the sharper the bounce angle.

To do this, divide the paddle into say 5 zones. This is easier if you make each zone say 10 pixels and have the paddle 50 pixels in size.

At the moment of impact, you know the Y position of the ball. You also know the Y position of the paddle and can calculate it's Y centre.

If the ball's Y position is greater than the centre of the paddle minus 5 and less than the centre of the paddle plus 5 then the ball just hit the centre zone. If so, negate the X and set the Y to zero. Something along the lines of:



All you need to do is calculate which zone of the paddle was hit using the same method and alter the Y value accordingly.

TDK_Man

EviLz
17
Years of Service
User Offline
Joined: 1st May 2007
Location:
Posted: 2nd May 2007 15:16
Thanks, thats gonna help me out alot! and yea it is a bit harder than what it first looked like it would be

The pact is sealed.

Login to post a reply

Server time is: 2024-09-25 21:28:08
Your offset time is: 2024-09-25 21:28:08