Posts tagged game
Pong Applied Box2DFlex Physics Engine
0I decided to learn by example and assigned myself the goal of building pong in box2d.
box2d is an overkill for pong , but what do you know, it’s damn easy.
the gist of it is :
- layout
- world setup
- event handling
Full source code hosted in github Pong in Box2DFlex.
The layout
a SkinnablePhysicsContainer holding two border containers (named p1 and p2) which are the player bars and corelating p1Goal and p2Goal which will act as hit area for the ball graphics element (Ellipse).
Setting Up the World
when the application loads it maps all elements in the physics container to box2d bodies , in woldSetup method i specified different mapping types to components
The key thing you need to know is that you want p1, p2 and the goals controls to be kinematic bodies so
- they are not affected by collations and
- they do not collide with each other
- they affect dynamic bodies colliding into them
and an event handler onBodiesContact that triggers when two bodies collide .
Event Hadnling
onBodiesContact is triggered each time two bodies collide indie it i do a simple test to see which to bodies collided and triggers the respective indicator.
world_mouseMoveHandler is handling the translation of mouse position and movement of p2