0

I am using Box2D to simulate a small world where the user can drag objects and 'stick' them in some predefined locations (basically 'locking' them in a certain position).

I have this code to lock an object in a position. Everything works excepts I cannot make SetPosition (nor SetTransform) to work. They just move the object to (0,0).

FYI the world is drawn using EaselJS.

// checks if mouse is dragging the object nearby one of the containers
if( isWithin(mouseX, mouseY, containers) ) {

    // make object 'straight'
    body.SetAngularVelocity(0);
    body.SetAngle(0);

    // makes the object a kinetic body
    body.SetType(b2Body.b2_kineticBody);

    // doesn't work. it always moves the object to (0,0)
    body.SetPosition(5,5); // I am using (5,5) for simplicity
                           // it should have the coordinates of the center of the container
    // alternative: (also doesn't work)
    //body.SetTransform(b2Vec2(5,5), body.GetAngle());
}

What am I doing wrong?

ios-lizard
  • 834
  • 1
  • 12
  • 19

2 Answers2

0

Your code looks Ok for me. Here is my code for dynamic body from my app. Do not know if it make sense in your case.

        b2Vec2 delta = that->settings->initPos - that->ballBody->GetPosition();
        delta *= that->ballBody->GetMass();
        that->ballBody->ApplyLinearImpulse(delta, that->ballBody->GetPosition());
Max
  • 6,286
  • 5
  • 44
  • 86
0

Try using

body->SetTransform(b2Vec2(5,5), body->GetAngle());
Jim Rota
  • 1,781
  • 2
  • 16
  • 21