Game Development Reference
In-Depth Information
Experiencing tunneling
Box2D by default does its best to prevent tunneling, computing the discrete simulation
using a continuous collision detection. Unfortunately, for a performance-related
reason, this kind of collision detection is applied only on dynamic bodies versus
static bodies. This means we can produce tunneling between two dynamic bodies.
1.
Look at the following script:
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class Main extends Sprite {
private var world:b2World;
private var worldScale:Number=30;
public function Main() {
world=new b2World(new b2Vec2(0,5),true);
debugDraw();
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(320/worldScale,
470/worldScale);
var polygonShape:b2PolygonShape=
new b2PolygonShape();
polygonShape.SetAsBox(320/worldScale,
10/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.density=1;
fixtureDef.restitution=0.5;
fixtureDef.friction=0.5;
var body:b2Body=world.CreateBody(bodyDef);
body.CreateFixture(fixtureDef);
bodyDef.position.Set(600/worldScale,
240/worldScale);
bodyDef.type=b2Body.b2_dynamicBody;
polygonShape.SetAsBox(10/worldScale,
220/worldScale);
var body2:b2Body=world.CreateBody(bodyDef);
body2.CreateFixture(fixtureDef);
 
Search WWH ::




Custom Search