Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 13 May 2012
Offline Last Active Dec 08 2012 11:12 AM

Topics I've Started

How do I reset my game?

03 December 2012 - 06:20 PM


I am making a 2d ping pong game in c#.
If the ball hits the net, the balls position and velocity should be reset to the initial values at the start of the game.
I know that the global timer therefore needs to be reset, but I don't know how to do it correctly. All my attempts yet to reset the game
have failed.

Here is the call of the ball's update method by testing class update's method:
[source lang="C#"] public void Update(double elapsedTime) { ball.Update(elapsedTime,yPosition,xPosition,tablesurface,netsurface,ref c, ref r1, ref r2); }[/source]
Here is the code of the ball update method:

[source lang="C#"] enum ballStatus{Reset,Play}; ballStatus status = ballStatus.Play; public void Update(double time, double height, double newXPos, List<Point> collisionsrfc,List<Point> netcollisionsrfc, ref Circle c,ref Rectangle rect1,ref Rectangle rect2) { if(start_timer) { timer+=time; } if((timer > 10.0) && (start_timer)) { status = ballStatus.Reset; } else { status = ballStatus.Play; } if(status == ballStatus.Play) { Xstart = newXPos; Ystart = height; Tilted_Throw(time,Ystart,Xstart,collisionsrfc,netcollisionsrfc,ref c, ref rect1, ref rect2); TotalTimePassed+=12*time; SetPosition(GetX,GetY); } if(status == ballStatus.Reset) { SetPosition(-340,70); Xstart=-340; Ystart=70; TotalTimePassed=0; resetvelocity=true; timer=0; start_timer=false; } }[/source]

The variable timer is not the global game timer, but the timer for reseting the game after 10 seconds, if the ball collided with the net.
The global timer is the variable TotalTimePassed.
The values -340 for x and 70 for y are the initial position values of the ball at the start of the game.
If resetvelocity is true, than the velocity is changed to the desired initial velocity of the ball in the ball's Tilted_Throw method:

[source lang="C#"]if(resetvelocity==false){ _velocity.Y = (velocity_start.Y - g.gravitation * TotalTimePassed); _velocity.X = velocity_start.X;} else{ SetStartVelocity(90+rand.Next(0,15),10+rand.Next(5,15)); _velocity.X = velocity_start.X; _velocity.Y = velocity_start.Y; resetvelocity=false; }[/source]
With SetStartVelocity I set the new starting velocity(velocity_start)(a bit different than the initial velocity, because ive added randomness).
The problem is, whenever the ball hits the net, timer will first go to 0 and then go up to 10, then again to 0 and then again up to 10 etc.
After every 10 seconds the ball will repeat its motion near the net and at this point i notice, that the ball is only displayed for 1 frame at the desired position(-340,70).

Please help me or give me solutions for this problem.
Thanks in advance!

Pixel-based collision detection with sphere

28 September 2012 - 07:07 AM


I am currently making a pixel based 2D table tennis game where I need to to find the exact point the ball hits the table.
For simplicity reasons I have't programmed the spin of the ball yet.
Can anyone help me with formulas or something else helpful?