• Advertisement
Sign in to follow this  

need help with trajectory mapping and classes for a pong game

This topic is 4175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya all, I have got further with SDL and C++ in my game than I ever have with any game project , thanks to the tips on this forum. Anyway I am enjoying the learning and I had in my pong game 2 paddles one was the player paddle and correctly moved up and down via sdl key events ......I also managed to draw the ball object sticking to the cpu paddle initially. Unfortunately I have had to go back to the drawing board because of the way my game loop and classes were laid out. The problem I was having is because I needed the ball to check if it had collided with the cpu or player paddle .....if it hits the top 3rd of the cpu paddle on the right side I would want to do y=y - 1 the x=x-1 to send it up diagonally up left , then I would need to redraw to show the movement to the same decrement until the ball hit the top of the screen ........at which point I would need to send it diagonally down to the left until it hits the player bat or goes of the screen. Im struggling to think how im going to do this in a modular OO simplistic and neat way. What classes to you folks think I should have? currently all drawing is done by calling objecttype.show(); from main and then doing SDL_Flip screen. My code seemed to be getting very messy so whats the best way to handle angles of collisions and trajectory? In the program my classes were :
  Frame
  #define cpupaddle 1
  #define playerpaddle 2
  int x,y
   ^ 
   | inherits from abstract class
   |
ball   paddle    bat

All my shapes bat,ball,paddle[cpu and player] were sdl surfaces ......at one point I tried to get the paddle surfaces into an sdl rect but this didnt seem to work also with sdl rects I could not get there x values and I needed these values at all times as they were constantly changing. Apologies for the long winded post but I am starting again and any ideas/help/class names would be appreciated and how do I deal with trajectorys? should a specific class handle trajectorys? where would I need arrays or vectors? when would they be created? Thanks David

Share this post


Link to post
Share on other sites
Advertisement
I would suggest making one-direction trajectories their own class (that is, a single instance describes all movement from one side to another, including bounces on the top and bottom). Compute the new trajectory whenever the ball bounces on a paddle (or the AI does some strategic planning) based on paddle velocity, friction and incoming angle.

Having a class for trajectories allows you to fetch the new ball position at any given time through interpolation without having to euler-integrate its velocity and handle collisions correctly (since you can solve the time of collision fairly easily), as well as aid AI thinking (where will the ball be when it reached my side?).

Share this post


Link to post
Share on other sites
Ok a trajectory class sounds good! Im hoping Im getting this right but say Im dealing with the trajectroy after the ball hits the bat on the top third would I store all those trajectory points in an array.....maybe like:


array_Top_third_bat[]{x = 1; y = 7;, //say the left bat started at yposition7
x = 2; y = 6;,
x = 3; y = 5;,
x = 4; y = 4;,
x = 5; y = 3;,
x = 6; y = 2;,
x = 7; y = 1,
x = 8; y = 0; //ball hits top centre of screen
x = 7, y = 1;
x = 6, y = 2;
x = 5, y = 3;
x = 4, y = 4;
x = 3, y = 5;
x = 2, y = 6;
x = 1, y = 7;
x = 0, y = 8; //ball has hit the bat or has hit edge of screen so either score for cpu/player or bounce back
}




Thanks for your suggestion would it be possible you could show me an example of a set of trajectorys stored? Looking at that just now ^^ Im thinking that only caters for if the bat is at position y7 or it hits the cpubat ay y 8 then I could read the trajectory from MAX to MIN

Maybe im thinking of it all wrong :-) such is the life of a newbie )

David

Share this post


Link to post
Share on other sites
I suggest storing merely the collision points and the horizontal speed. For instance:


/ \
/ \ /
/ \ /
\/


For the trajectory above, you would store the starting point, the top point, the bottom point, and the end point. More bounces means more intermediary points.

Given the horizontal speed, you can compute the current horizontal position. Given any horizontal position, you can compute the vertical position (because you know between which points you are) and thus the full position of the ball.

Share this post


Link to post
Share on other sites
Could you give me an example of the formulae for this?

I am using x,y xvel,yvel(xvel and yvel are rate of movement) and I use fdelta

Share this post


Link to post
Share on other sites
if you want go to my site: qwarm.phpnet.us -> downloads ,and get my oooold pong clone :) its packed with source too(its not very good programmed, but collisions work :) it using sdl and c++

Share this post


Link to post
Share on other sites
ahh thanks thats exactly what I need ! .......It doesnt worry me if its not coded the 100% reccomended way but Im at the stage where I need to upgrade my programming box of tricks :-) so seeing the way collision is done for the ball would be great :-)

Thanks
redwing

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement