View Full Version : Ball and Random movement ** FIXED ** : )
11-10-2004, 03:30 AM
Hi, i'm back. thanks for the help before, it really saved my life.
=== On the same assignment (bat and ball - Pong) ===
I have another problem. Basically when I press the left mouse button the program starts, so the ball starts moving normally and the bat starts moving normally, but the problem is that I need the ball to start at at a random angle (between -45 and +45)each time I start a new game.
I have included the code, it has all the neccesary functions including the getInitialAngle();.
It compiles but when I run it, the ball starts bouncing of every direction very very fast and that's it. You'll understand what I mean if you run it.
!!! :confused: :confused: Please Help :confused: :confused: !!!
code removed for now, well update as soon as possible
11-10-2004, 05:59 AM
you're recalculating your initial angle in your idle function. This value should only be calculated once in some initalization code.
also, your call to glLoadIdentity() is commented out in the display function - I don't know if this is intentional, just wanted to let you know.
11-10-2004, 06:04 AM
yeah that is meant to be like that cause i was try out some stuff.
what do you suggest to do exactly then. how could i implement the code?
11-10-2004, 06:13 AM
well, that's wrong. of course the ball is going to fly all over the place...each frame you're changing the angle of direction it's moving in.
move the line where you set angle from the idle function into your main function (anywhere before glutMainLoop() - make it the first statement).
also, in your getInitialAngle function put this line before you use the rand() function:
in addition to that: the way you calculate the random angle looks weird. It might work i don't know, but if it doesn't return the correct values, use this:
rand()%90 - 45;
11-10-2004, 06:37 AM
I did exactly what you said, but instead of it bouncing of in ever direction in just moves in one direction at random and stops there and it's not like it "moves", it disappears and then reappears.
proper Bogus style.
also the srand comment just caused an error, i already have a srand comment before the rand() part.
any other ideas?
thanks for the help so far.
11-10-2004, 07:43 AM
that's because the ball movement has to be cumulative. each idle function you set ball.x to -sin(angle), which is the same value each time.
it should be more like ball.x += -sin(angle).
(by the way, the y component is obtained using sine of angle and x component using cos - if the ball is moving at an angle of 10 degrees it should move farther in x than y each frame:
cos(10) ~ .9
sin(10) ~ .1)
when the ball hits off a 'bat' or a wall you'll need to toggle its travel direction. I see you doing this using dx and dy. what you should do is set dx to cos(angle) in main and dy to sin(angle) also in main.
then use those values to update ball.x and ball.y:
ball.x += dx;
ball.y += dy;
it seems like this is an assignment of yours and you should try and understand what's going on. so you're going to have to think about it a bit.
11-10-2004, 11:35 PM
Thanks Aelund, Im gonna try out this today when I get back to school. You are correct, this is an assignment, (I think i already said that somewhere), yeah don't get me wrong, I am understanding everything you are saying, I enjoy openGL and wanna develop my knowledge on it as much as possible.
Thanks for the help, you don't know how much I appreciate it.
11-11-2004, 07:09 AM
Fixed it, thanks to you Aeluned.
Also i realised that I had to move the angle=getInitialAngle(); function to a place where it wouldn't be continuosly called upon, so I put it in the mouse function (left click).
And now it is doing it randomly. Which i am very happy about. Everything works.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.