tags:

views:

31

answers:

1

I am having a problem, and i have tried all the possible combination. I am trying to get my ball to just basically fly off the screen as fast as possible. The only problem is no matter what values i set it just moves at the same rate. I tried doing ballBody.SetLinearVelocity(new Vector2(1000000f, 0));

ballBody.SetLinearVelocity(new Vector2(10f, 0));

And every number in between, and the ball still moves at the same speed. What Exactly am i doing wrong. Also the code for the body is below . All i want is for the ball to go really fast, almost like a Pinball machine.

        var bodyDef = new BodyDef();
        bodyDef.position = new Vector2(400, 200);

        bodyDef.type = BodyType.Dynamic;
        bodyDef.fixedRotation = true;


        ballBody = world.CreateBody(bodyDef);

        var circleShape = new CircleShape();
        circleShape._radius = 12.5f;

        var fixtureDef = new FixtureDef();
        fixtureDef.restitution = 1.4f;
        fixtureDef.shape = circleShape;
        fixtureDef.friction = 0;


        ballBody.CreateFixture(fixtureDef);
+1  A: 

There is a maximum linear velocity in Box2D due to numerical stability. However, it is set high enough that you should never hit it with normal use.

You should note, and this is mentioned in the documentation, that Box2D works best with lengths between 0.1 and 10.0. This is imagined in meters according to the author and makes it suitable to simulate objects from baseballs to barrels and up to buses. Beyond that it becomes more sensitive to numerical inaccuracy.

Try scaling down your coordinate system so that your average simulated object (a ball perhaps) is 1.0 units in size. This will help Box2D to simulate your system optimally.

Given this range of operation, speeds beyond 10 or 100 units/second could cause problems, especially if you apply them to dynamic bodies who receive massive momentums. If these high-momentum bodies should collide with some other body the momentum would spread and your whole system might end up flying away/becoming unstable.

For this reason it is probably best to turn the ball into a kinematic body once it is supposed to fly away. A kinematic body moves according to velocity but does not collide and is not acted on by forces. I am not sure but it is possible that kinematic bodies are allowed higher speeds as they carry no momentum data.

Edit: Also, you should normally not use a restitution coefficient higher than 1.0f. Doing so will cause the body to gain kinetic energy during the collision. This is not impossible in the real world but would require some energy source. Imagine a chemical compound attached to the body that explodes on impact causing more energy to be pumped into the system.

Staffan E