Apply rotation force?

Nickert
Posts: 25
Joined: Sat Dec 29, 2012 7:20 pm

Apply rotation force?

Post by Nickert »

Can I apply a rotation only force, or do I have to use applyForce and calculate where and how I have to push it to get it to rotate?
Nickert
Posts: 25
Joined: Sat Dec 29, 2012 7:20 pm

Re: Apply rotation force?

Post by Nickert »

If so, how can I apply a force to rotate every object? I should apply it relative to the center of mass I guess?
Nickert
Posts: 25
Joined: Sat Dec 29, 2012 7:20 pm

Re: Apply rotation force?

Post by Nickert »

So I ended up doing this, which works really well. Comments are welcome!

Code: Select all

float const player_rotation_speed = configuration.get("player_rotation_speed", M_PI*2.0f) * dt;
auto const player_rotation = self.get_rotation();
float const player_acceleration = configuration.get("player_acceleration", 100.0f) * dt;

Speed const forward = player_rotation * moggle::vector3<float>{0, 0, -1};
Speed const backward = player_rotation * moggle::vector3<float>{0, 0, 1};
Speed const left = player_rotation * moggle::vector3<float>{-1, 0, 0};
Speed const right = player_rotation * moggle::vector3<float>{1, 0, 0};
Speed const up = player_rotation * moggle::vector3<float>{0, 1, 0};
Speed const down = player_rotation * moggle::vector3<float>{0, -1, 0};

if(parameters.input.keys_pressed[SDLK_LEFT]){
	self.physics->apply_force(player_rotation_speed * down, left);
}

if(parameters.input.keys_pressed[SDLK_RIGHT]){
	self.physics->apply_force(player_rotation_speed * down, right);
}

if(parameters.input.keys_pressed[SDLK_q]){
	self.physics->apply_force(player_rotation_speed * left, forward);
}

if(parameters.input.keys_pressed[SDLK_e]){
	self.physics->apply_force(player_rotation_speed * right, forward);
}

if(parameters.input.keys_pressed[SDLK_DOWN]){
	self.physics->apply_force(player_rotation_speed * up, forward);
}

if(parameters.input.keys_pressed[SDLK_UP]){
	self.physics->apply_force(player_rotation_speed * down, forward);
}
So first I define some relative vectors, and then use that to let the player do the rotation. You can fly really well with it. I know this is not code you can directly use, but you'll get what I did to get rotation to work nicely. Here's a movie of a ufo flying, using this rotation code and similar acceleration code: http://astrant.net/beamer/beamer_ufo_freeflight.mov