something like this?
b->SetAngle(-45);A fast, easy to use, free, and community supported 2D game engine
something like this?
b->SetAngle(-45);a bodydef can just do:
b.angle = CC_DEGREES_TO_RADIANS(-45);
if you're an actual body, I think you need to use:
b.SetTransform(b.GetPosition(), CC_DEGREES_TO_RADIANS(-45));Or if using rectangles :
b2PolygonShape staticBox;
staticBox.SetAsBox(width.x/32, width.y/32, b2Vec2(width.x /32,-width.y/32), angle);thanks, this worked
b->SetTransform(b->GetPosition(), CC_DEGREES_TO_RADIANS(25));Try not to use SetTransform, it can 'mess' the physics up.
Regards,
Steve
The "proper" way to change a body's rotation is to apply just enough torque to rotate the body. However, this requires some messy math and is cumbersome to do. Most people on the Box2D community recommend you set the transform of the body, like mentioned in post #2. Though, use a -> as you have a pointer to a b2Body.
Hi , when we use the settransform the body keeps on rotating without stopping... how can i rotate a body to a particular angle and stop?
Thanks
If you want to forbid rotation for a body just use this:
body->SetFixedRotation(true);
when you need it or set it directly to the body definition when you create your box2d body like this:
b2BodyDef bodyDef;
....
bodyDef.fixedRotation = true;
Hope this helped :)
You want to make the body point to the direction of motion (If I understand correctly)
I'm doing this
CGPoint current = CGPointMake(b->GetPosition().x*PTM_RATIO, b->GetPosition().y*PTM_RATIO);
CGPoint destionation = CGPointMake(shield.position.x+arc4random()%20, shield.position.y+arc4random()%20);
CGPoint vector = ccpSub(destionation, current);
vector = ccpNormalize(vector);
b2Vec2 direction = b2Vec2(vector.x*(arc4random()%5+2), vector.y*(arc4random()%5+2));
float rads = atan2f(direction.y, direction.x);
b->ApplyLinearImpulse(direction, b->GetPosition());
b->SetTransform(b->GetPosition(), rads);
b is the body you move, shield is the end point. This will make the b body to move toward the shield body (location) and also update it angle to match the direction vector.
Here I calculate the new direction and also its angle in radians and then just apply a transform to the b body.
You can use SetTransform as pointed out above, and then SetAngularVelocity(0) to stop it from rotating after that.
@ EviluS what is shield exactly how do i get it ? since i dont know my final position because the object does not move according to some touch location .. Thanks
shield is a box2d body (specific for my code) positioned in the center of the screen.
In your case if you do not know the final position you can create a method which on every 0.05 (or some other interval) takes your box2d body position and then run second method with a delay. In that second method you should use my code from above but replace the shield position with the one you took. I think that should do the trick.
You must log in to post.