EpicPong24 – [7h30] We (finally) have a pong!

Finally, the basic pong system is working!

This took way more time that I thought (which is normal, I guess?). Two things gave me a lot of trouble :

– Everything is procedurally generated and I want to make sure I’ll be ready when I’ll add many levels, walls and balls.

– I’m using rigidbody for the physics. However, Pong has a different physic for the paddle. The farther you are from the middle, the steeper the angle of the ball will be after the contact. I initially tried to use OnCollisionEnter() or OnCollisionExit() and change the velocity of the ball myself. However, it often led to the ball going in and out of the pad and some other strange behavior. This is probably related to the way I handled the ContactPoints. I’ll have to look into it in more detail.

I finally decided to set my paddle to “is trigger” which prevented it to interact with the Ball’s rigidbody. I then used the following code with OnTriggerEnter() :

(Sorry for the unformatted code, I didn’t find how to copy-paster it formatted in WordPress. If anyone know, I would be really happy to hear from you)

// Change ball direction when it collide with pads.
 void OnTriggerEnter(Collider _Collided)
 {
 GameObject _PadCollided = _Collided.gameObject;
 float _contactDeltaY;
 float _contactRatio;
 float _contactRatioPI;
 float _velMagnitude;
 float _newVelX;
 float _newVelY;

 // Calculate the ratio between the size of the paddle and the point being hit on the paddle
 _contactDeltaY = transform.position.y - _PadCollided.transform.position.y; // Distance on the Y axis between the pad center and the ball center
 _contactRatio = _contactDeltaY/_PadCollided.transform.localScale.y*2; // (-1,1) Ratio between the contactDeltaY and the half-length of the paddle.
 _contactRatioPI = limitAngle*_contactRatio*3.141592654f/2; // Ratio of PI to use for new velocity calculation

_velMagnitude = rigidbody.velocity.magnitude; // Magnitude of the velocity to keep it constant

 _newVelX = _velMagnitude*Mathf.Cos (_contactRatioPI); // New velocity in X axis
 _newVelY = _velMagnitude*Mathf.Sin (_contactRatioPI); // New velocity in Y axis

 // Update the ball velocity
 if(transform.rigidbody.velocity.x < 0.0f)
 {
 rigidbody.velocity = new Vector3(_newVelX, _newVelY, 0.0f);
 }
 else if(transform.rigidbody.velocity.x > 0.0f)
 {
 rigidbody.velocity = new Vector3(-_newVelX, _newVelY, 0.0f);
 }
 }

And it works really smoothly!
I added a small point counter and a way to reinitialize the ball to the middle. The game is now playable!

For the next update :

o I’ll add different level and a way to generate them easily

o I’ll add a stat class for the player.

This will allow to modify all of the paddle attribute easily. The goal afterward, is to have the player gain Exp while doing the level. Player will have the possibility to tune its paddle with the level he gained. Further level will be harder and require more skill. Still a lot of work to do but it goes fairly well! Now, time to go eat something!! Updated repository and webplayer :

EpicPong24 Repository on GitHub

EpicPong24 WebPlayer

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s