Advertisement

Changing angle of ball without changing speed

Started by May 02, 2000 07:28 PM
1 comment, last by t2sherm 24 years, 7 months ago
I am making a game in which you bounce around a ball and hit bricks. When the ball hits the paddle, i want the ball to bounce off at different angles. If it hits the far edge of the paddles, it will have a small angle, more angle as it gets toward the middle, and a 90 degree angle in the middle. I am using 4 variables for the motion of the ball. BallDirx, BallDiry, BallSpeedx, BallSpeedy. Everyframe, the balldirx (either 1 or -1) is multiplied by the ballspeedx and same for y coords. A problem arises when i want a different angle, as i must have different slopes for the ball like if x and y both = 4, it will be a 45 degree angle. If i want less angle, i can use like x=5 and y=2 or something. This works, but everyframe the ball now moves 5 pixels left and 2 up, and it causes the speed to change whenever the angle does and it just doesn''t look right when you are playing the game. The problem is that the lowest amount of pixels you can move is 1. if i could move in like .25 pixel increments, it wouldn''t be that bad. Also if i have a ball going with x=2 y=4 for example, and i want to increase the speed of the ball, in order for the angle to remain the same, i have to keep the same slope of 1/2. This means i would have to increase the speeds to x=4 y=8, which is a major speed up. I have seen games like brickout where there are many angles, and the speed increases only a little, so i know it can be done, but i can''t seem to figure out how. Does anyone have any ideas? Thanks for all who answered my last question on the brick collision! t2sherm ô¿ô
t2sherm ô¿ô
All you do is make sure that the total distance the ball is moving remains the same.

You can do this by getting the length of the ball's movement vector using pythagoras (a squared+c squared = c squared)

Length= sqrt(pow(ballspeedx,2)+pow(ballspeedy,2));

Then, divide by the length to normalise the vector (make it of length 1).

ballspeedx/=length;
ballspeedy/=length;

You can then multiply the vector by the ball's speed and the speed should always be the same, whatever direction the ball is going in.

ballspeedx*=ball.speed;
ballspeedy*=ball.speed;

I hope this helps.

btw, I'm currently writing a game in the same sort of style to yours. If you're interested, you can find it here:


http://www.geocities.com/ben32768


Edited by - benjamin bunny on 5/2/00 7:50:11 PM

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

Advertisement
Otherwise instead of having vectors for speed in both planes,
use 1.) angle variable and
2.) a speed variable,
and you can thus calculate the speed in both planes by

xdir = cos(angle) *speed
ydir = sin(angle) * speed

every frame, you would add xdir onto x, and ydir onto y.
so if xdir is negative, it would in effect subtract that quantity from x, and likewise with ydir.

You can thus just change the angle that your ball is bouncing at, and the speed will remain constant.
Although this approach will need more checking, it allows quite a bit of flexibility.
Otherwise benjamin bunnies suggestion is also good.

Need more help?
hope I''ve helped
FReY
do unto others... and then run like hell.

This topic is closed to new replies.

Advertisement