This update method completely bypasses the one the base ig.Entity that normally moves the entity. Its update() method takes the position of the body from the physics simulation and converts it back to Impact's unit system. The ig.Box2DEntity automatically creates a physics body with the size of the entity. Ig.Box2DGame has a special property debugCollisionRects, that when set to true will draw all the collision boxes as green outlines. The performance of Box2D seems to be mostly bound by the number of collisions and not the number of objects in th world. In my tests, this worked quite nice even for larger game worlds. However, instead of just creating one physics object for each tile, it tries to find larger rectangle regions of tiles to combine into one object. loadLevel() method of the ig.Box2DGame creates the physics world from a collision map. See the source code of the physics example game. For this, I wrote the ig.Box2DGame and ig.Box2DEntity classes, that do all the dirty work for you. Now, to integrate Box2D with Impact, it would be nice to have a base entity class that uses Box2D and a base Game class that creates the physics world. By default it is set to 0.1 - that means that 1 pixel equals 0.1 meters. Therefore, we use a global scaling factor to convert from our pixels to meters: Box2D.SCALE. Your objects should be roughly between 0.1 and 10 meters in size - for most games this means that simply taking the pixel sizes as foundation for the physics could introduce some artifacts. The units in Box2D are tuned to work well as meters. Setting up the Collision Map and Entities ig.system.tick provides the time that has passed since the last frame - which is exactly the amount of time our physics simulation should advance: The b2.World.Step() method takes three parameters: the timestep in seconds and the number of internal iterations for the velocity and positions computations. Thus box we created is 10 by 10 units in size and because its center is at (100,100), it will stretch out from (95,95) to (105,105).Īfter everything is set up, we can advance the physics simulation in our game's update method. Also, the SetAsBox method takes half-width and half-height parameters. This is different from Impact, where the position of an entity is set for the upper left corner of the entity. ![]() Please note that the position we set (100, 100) specifies the origin of the object - its center point. Var fixture = new 2FixtureDef įixture.shape = new 2PolygonShape() Var bodyDef = new 2BodyDef() īodyDef.type = 2_dynamicBody With the world ready, we can now add some objects to it. Create the world and make it globally available as part of ig. For this, we just need to define a gravity vector and create an instance of b2World: The first thing we need to do is setting up the Box2D World. The source code for this demo with the Box2D Plugin can be found on your download page. ![]() I also wrapped the source into a module, to be easily loadable by Impact. So for instance to refer to the b2BodyDef class you can use 2BodyDef. All Box2D Classes are now members of the Box2D. The JavaScript version of Box2D lives in its own namespace. Recently, the ActionScript version of Box2D has been ported to JavaScript. Box2D is a physics engine, originally written in C++, ported to ActionScript and used in hundreds of games, including the incredibly popular Angry Birds. This is often sufficient for Jump'n'Run or RPG games, but sometimes you need some more accurate and realistic physics. Impact provides some basic game collision detection and response for entities.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |