Advertisement

Ball movment

Started by December 08, 2002 04:10 PM
5 comments, last by Dalik 22 years, 2 months ago
Alright I need some help with moving the ball around the screen. I want the ball to move in a random direction at the start of each level/game. If you can please show me how to do this. I am thinking of a way by using a seed, but I am sure I am making this way to hard and not really sure how I would implement this. Also what kind of structure would I have for moving the ball. I got 2 variables, these hold the posistion where to ball is at. g_ball_position_x //currently set at 400 g_ball_position_y //currently set at 350 I am using C and I am unsure how to get this going. thank you for your time.
Interested in being apart of a team of people that are developing a toolkit that can help anyone product an online game? Then click here http://tangle.thomson.id.au/
Use a random number generator to get your initial positions in the range that is desired (screen height and screen width I assume), but you also need something to store the random velocity of the object. If you want the ball to start at some constant speed, generate a random angle [0, 2pi] or [0, 360], cast it into radians if its not already, and then set vx equal to vi*cost and vy equal to vi*sint where vi is that initial speed you want. If you want random speed, you could repeat the same process where the vi is in some range [low, high], because otherwise you might get a ball that move really slowly.

Brendan
Brendan"Mathematics is the Queen of the Sciences, and Arithmetic the Queen of Mathematics" -Gauss
Advertisement
I havent had any trig or higher math classes but I am reading up on some formulas etc, but in to get a better understanding of what your saying could you give me code so I can learn from it please. It gives me a better understanding how to put the math into code.

[edited by - Dalik on December 9, 2002 3:54:58 AM]
Interested in being apart of a team of people that are developing a toolkit that can help anyone product an online game? Then click here http://tangle.thomson.id.au/
just look up vectors in the articles section of gamedev. or on google.
You really need to advance your math when you get into graphics... but I will give you a few hints.

it''d probably be easiest to make a ball object, using a struct...
i.e.
_________________________
struct ball
{
int x; //stores x position
int y; //stores y position
int vx; //stores horizontal movement per fram
int vy; //stores vertical movement per frame
}
_______________________

for the random point of it, just set up a random structure... using c++, you could use the time from windows to see srand, then use the rand function and set that up for your x and y.
With this setup, to adjust when the ball hits a side, u just say vx=-vx or vy=-vy...
anyway... this is all elementary... and if you didn''t have this much figured out, i honestly suggest you sit down with a few good books and read them before getting into high level programming

If barbie is so popular, why do you have to buy her friends?
If barbie is so popular, why do you have to buy her friends?
This is what I have so far, and the ball moves straight up. Thats the good news. Bad news I dont think its all correct here is the movment code, also the ball should move in different directions not just up.


  ball.vel = 10;ball.direction_x = cos(ball.angle);ball.direction_y = sin(ball.angle);g_ball_position_y = g_ball_position_y + ball.direction_y; g_ball_position_x = g_ball_position_x + ball.direction_x;  


g_ball_position_y is the position of the ball, its global

ball.direction_x/y are values in the ball struct
same as vx and vy

ball.angle is in the ball struct also and is given a random value from rand()

If you have any ideas let me know, I will be working on it still
Interested in being apart of a team of people that are developing a toolkit that can help anyone product an online game? Then click here http://tangle.thomson.id.au/
Advertisement
this is the code I have to bouce the ball off the wall

  		if((g_ball_position_x <= 0) || (g_ball_position_x >= SCREEN_W))		{			g_ball_position_x = (-g_ball_position_x);			g_ball_position_y -= (-g_ball_position_y);		}				if((g_ball_position_y <= 0) || (g_ball_position_y >= SCREEN_H))		{			g_ball_position_y -= g_ball_position_y;			g_ball_position_x -= g_ball_position_x;					}  


This isnt working either one of the 2 are not working
also


  g_ball_position_y *= -1;g_ball_position_x *= -1;  

doesnt work either
any ideas would be great thnx.
Interested in being apart of a team of people that are developing a toolkit that can help anyone product an online game? Then click here http://tangle.thomson.id.au/

This topic is closed to new replies.

Advertisement