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.
!!! Please Help !!!
code removed for now, well update as soon as possible
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:
srand(GetTickCount());
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:
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.
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.
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.
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.