Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411528 Posts in 69377 Topics- by 58433 Members - Latest Member: Bohdan_Zoshchenko

April 29, 2024, 01:06:46 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Simple Box2D/Impulse problem
Pages: [1]
Print
Author Topic: Simple Box2D/Impulse problem  (Read 1864 times)
go
Level 0
**


View Profile
« on: January 25, 2010, 10:46:39 PM »

I ran into what is possibly a really easy problem, but I can't seem to find the solution.

The Box2D forums are down, so I'll try my luck here.

I want move a Box2D body in a circular pattern. It's a pretty trivial thing to do.

This works perfectly with SetXForm :

Code:
//orbitAngle loops from 0 to 360
float dX = cos( orbitAngle * PI / 180.0 ) ;
float dY = sin( orbitAngle * PI / 180.0 ) ;

b2Vec2 moveVec = b2Vec2( body->GetPosition().x + dX / scale, body->GetPosition().y + dY / scale);
body->SetXForm( moveVec , body->GetAngle() );

The problem is SetXForm doesn't work very well with the outside forces applied to the body.

I know I have to use impulses, however this doesn't produce what I want :

Code:
float dX = cos( orbitAngle * PI / 180.0 ) ;
float dY = sin( orbitAngle * PI / 180.0 ) ;

b2Vec2 impulseVec = b2Vec2( 0.2 * dX , 0.2 * dY );
body->ApplyImpulse( impulseVec , body->GetWorldCenter() );

The body makes half circles and never returns to origin.

What is the obvious thing I'm missing?
Logged
bateleur
Level 10
*****



View Profile
« Reply #1 on: January 25, 2010, 11:49:38 PM »

What is the obvious thing I'm missing?

Initial velocity?

For a body to follow a circular path the forces on it (or equivalently, impulses) vary as you have them. However, that will only result in a circular path if at any given point the force is centripetal. If the body in your example is initially stationary that won't be the case.
Logged

encoder
Level 0
**


View Profile WWW
« Reply #2 on: January 27, 2010, 11:33:56 AM »

either case, you need an initial speed.

register a force pointing towards a center and set a tangent initial velocity.
if your body moves in a circular fashion you are lucky, most likely will have an elliptical path. you must do the math to get it exactly right. trial-and-error works to for some degree.
 Blink  Blink
Logged

god is an instance of a class. who is the coder?
go
Level 0
**


View Profile
« Reply #3 on: January 27, 2010, 12:33:06 PM »

Yeah, it works fine with an initial velocity.

Thanks.

 Beer!
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic