1
votes

I am developing java game using box2d for my physics, I have got helicopter, ex:

enter image description here

I reduced gravity by setting:

body.setGravityScale(0.03f);

So it acts bit realistic (is affected by gravity only little bit, floating in the air)

To move it, down/up left/right I have controller, thats how I control my helicopter:

body.applyLinearImpulse(new Vector2(pValueX * 3, pValueY * 3), mainBody.getWorldCenter());

Where pValueX and pValueY are 1 or -1 (directions up/down left or right)

It works good, but now I am trying to achieve more realistic effect, when moving helicopter left/right I wanted to tilt it little bit so it works like real helicopter, but could not find proper way how to do it, I have tried applying force in different part of the body, but it makes my helicopter rotating 360 degrees if keep pressing left or right.

3

3 Answers

1
votes

This question is old, but in case it's still relevant, I created a helicopter using JBox2D (which pretty much maps directly to Box2D). For tilting left/right (i.e. forwards/backwards relative to the pilot):-

heli.applyTorque(TURN_TORQUE);

or

heli.applyTorque(-TURN_TORQUE);

This rotates the heli, and then if the player wants lift:

Vec2 force = new Vec2();
force.y = (float)Math.cos(chopper.getAngle()) * -1;
force.x = (float)Math.sin(chopper.getAngle());
force.mulLocal(ROTOR_FORCE);
heli.applyForceToCenter(force);
1
votes

What you can do is, just define two constants as maxForceLeft and maxForceRight. When you press left apply some force on the cockpit part of the helicopter and keep comparing it with the maxForceLeft,once it reaches that value stop applying the force.Do the same for the right button by applying the force on the tail rotor part of the helicopter.In this way you can avoid rotating it 360 degrees.Depending upon the kind of effect you want for your helicopter you can apply the forces in either upward or downward direction.

1
votes
  http://www.iforce2d.net/b2dtut/rotate-to-angle

What you need is rotating the body to a desired angle.. This is a great tutorial to achieve this.

I hope this would help.