Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 09 Jun 2011
Offline Last Active Oct 20 2016 05:48 AM

#4915441 Ping Pong (Ball Movement)

Posted by on 22 February 2012 - 02:59 AM

yea you can do it without classes, but if your using c++ then you may as use them. If you are not conformal with classes I suggest reading up on them now because they are one of the core foundation principles of nearly every program :).

With your linear equations are you trying to achieve a random direction? it might be slightly over complicating it but if it works then should be fine

Another suggestion for you would be to define a static const for your speed makes it neater and clearer :)

static const int BALL_SPEED = 10;
Velocity += BALL_SPEED;

#4915296 Ping Pong (Ball Movement)

Posted by on 21 February 2012 - 04:25 PM

ok so you have you game loop, I will just lay out how I would do the basic movement. If I was to do the whole thing you wouldnt be learning anythign Posted Image

im not sure how you code your controls work but I will use arrow keys for player 1 and WS for player 2

finally not knowing your framwework means that i will made logical substitutions on how events may be handled

ok well I scratched this together using notepad on my laptop as my computer has died on me so sorry for any syntax errors

Player player1;
player player2;
   string button = event.getButton(); //this is totally made up for this example many frameworks handle this differantly I know SDL has its own event lableing system ( yes string is very bad way of doing this but makes it clearer as an example)
   bool upordown = event.upordown();  // again I dont know your framework so making this up to make scence   if( button == up || button == down )
	player1.HandleEvent( button );// each object should know how to handle its own even in my oppinion
   else if( button == W || button == S )
	player2.HandleEvent( button, upordown ):
	//anything elts like program close/exit for example

return 0;

//player class
class Player

	  //contructor and deconstructor
	  void Update();
	  void Draw();
	  void HandleEvent( string button, bool upordown );


	 int direction;
	 int speed;
	 int _VelocityX;
	 int _VelocityY;
	 int X;
	 int Y;
void Player::HandleEvent( string button, bool upordown )
	 //w and up handed in the same way for this example, you can create some sort of id system where w and up return the same id
	 if( button == w || button == up )
		 if( upordown == true )
			  speed = BALL_SPEED; //probably a const or passed in the constructor
			 speed = 0;

		 direction = 180;//assumes mathamatic cos and sign where 0 is east direction
   //elts do the same for down with direction as 90//update velocities if speed is 0 ball will stop

  _VelocityX = int ( cos( ( _RotationAngle + 0.0 ) * M_PI / 180.0 ) * ( _CurrentMovementSpeed * _FrameDeltaTime ) ) );
  _VelocityY = int ( sin( ( _RotationAngle + 0.0 ) * M_PI / 180.0 ) * (_CurrentMovementSpeed  * _FrameDeltaTime ) ) );

void Player::Update()
	x += _VelocityX;
   y +=  _VelocityY;

void Player::Draw()
   draw however your frameworks draws

This is my take on doing something like this, might be some people that disagree but hope it helps. Note the ball movement can be done in exactly the same way but using collision detection to change direction rather than some event from the keyboard Posted Image.

#4915149 Ping Pong (Ball Movement)

Posted by on 21 February 2012 - 06:04 AM

I have never used allegro 5 but I will create some sudo code that you will be able to convert to your needs, il post this tonight for you :). And if you are using a game loop rather than event driven you will probably need a queue.

#4913838 Ping Pong (Ball Movement)

Posted by on 17 February 2012 - 02:52 AM

Well I guess before-it-was-popular took the words right out of my mouth nice explanation m8

but may I add that if you simply add speed to X and Y when you want to move at an angle ( NE, SE, SW, NW ) then you will probably be adding or subtracting speed to both X and Y this gives the unwanted effect of doubling the speed of the ball ( you are the full amount to both X and Y ). You could of course compensate for this by keeping track of the latest direction you want to travel in and halving the appropriate velocity ( hard coding all directions ) but then we may as use an angle in the first place Posted Image

If you need help implementing cos and sin give us a howler il plot out some example code for ya

#4913477 Ping Pong (Ball Movement)

Posted by on 15 February 2012 - 03:50 PM

Simply adding velocity to X and Y is fine but what about when you need to calculate the bounce? surly we need to be a little more accurate using cos sin and bounce angle/ facing direction is better no?

_VelocityX = int ( cos( ( _RotationAngle + 0.0 ) * M_PI / 180.0 ) * ( _CurrentMovementSpeed * _FrameDeltaTime ) ) );
_VelocityY = int ( sin( ( _RotationAngle + 0.0 ) * M_PI / 180.0 ) * (_CurrentMovementSpeed  * _FrameDeltaTime ) ) );
p.s _FrameDeltaTime = time it took to complete one cycle or the gameloop. This means the ball travels the same distance no matter the frame rate

then we can use

_Ball->x += _VelocityX;
_Ball->y += _VelocityY;

#4873010 Help with jumping

Posted by on 15 October 2011 - 07:17 PM

this thread was quite funny to read, its the common case of someone doing a few online tutorial believing they instantly know how to program and that they know best ignoring other peoples advice