Code Monkey home page Code Monkey logo

pingpong's Introduction

========================================== PingPong homework for Interactive Graphics

Asra Nizami and Guillermo Vera

This is an implementation of an interactive Ping Pong game using G3D. The player is provided with a paddle, whose movement is controlled by the mouse. The game starts off with a ball being served from the opponent's end, which the player has to hit back with the paddle.

Implementations

The code first draws the ping pong table, the paddle and the ball according to actual physical dimensions. The ping pong ball has a radius of 2cm, the paddle has a radius of 8cm and the table has length, width and height being 274cm, 152.5cm and 76cm respectively. It also draws a net in the middle of the table of width 1cm and height 15.25cm.

The code uses the equations of projectile motion, taking gravity into account, to determine the path the ball will follow as it moves. It collides and interacts with the ping pong table, the net, and the paddle in a physically meaningful way. When it bounces off the table, its velocity in the y-direction reverses and decreases by a factor, defined by constant TABLE_RESTITUTION. It only bounces off the table when it is above the table, and falls down infinitely when it is off the table. When the ball collides with the net, its velocity in the y-direction is not affected, but its z-direction velocity is reduced by a NET_RESTITUTION factor. This is in line with the physical situation of objects colliding at an angle to the normal of a surface.

When the ball collides with the paddle, it reverses its velocity in the z-direction. It also takes into account the velocity of the paddle at the point of impact and imparts some of the paddle's velocity to the ball. Since the paddle is always at a fixed height of 20cm, its y-velocity is always 0, so the ball's y-velocity is not affected. The code takes into account the x and z velocities of the paddle and adds them to the ball's post-collision velocities respectively. It uses the law of conservation of momentum equation m1v1 - m2m2 = m2v3 (assuming that the paddle's velocity is 0 after the collision) to impart velocity, and scales it by different factors PADDLEX_RESTITUTION and PADDLEZ_RESTITUTION, to make it easier to hit angled shots.

Commands

Pressing the spacebar key resets the ball to its initial position and velocity. The camera also pans using regular G3D panning command keys.

pingpong's People

Contributors

gverac avatar

Watchers

James Cloos avatar Asra Nizami avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.