Box2D body rotation problem

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

Box2D body rotation problem

Postby fabiitch » Mon Oct 23, 2017 10:57 pm

Hello sir,
For start , sry for bad english!
and you take time to give me a 'easy answer to applicate' .

Im not sure to be in right index for this topic but lets go !

I try to made the best game ever but : im block on a basic fucking body rotation.
SO :

My problem is :

My game is a 2D shooter arena(maybe), and i use box2D
My player shot a rocket (a rectangle body), in the direction of the pointer where player click.
The point where rocket start is ok but, i block for body rotation.

All my pointPosition are in box2D metrics.

i try some

Code: Select all
   float deltaY = getDelaYValue(posPointer, player.body.getPosition());
      float deltaX = getDelaXValue(posPointer, player.body.getPosition());
      double costTeta = Math.atan(deltaY / deltaX);

      float teta = (float) Math.toDegrees(costTeta);   System.out.println("degrees :" + teta);
      System.out.println("spriteRotation =" + player.sprite.getRotation());
      // newBullet.body.setTransform(bodyBulletPositionToStartShot, (float)

      newBullet.body.setTransform(bodyBulletPositionToStartShot, angleQueJeMet);

My problem : give the angle at this rocket.

Important : i put a schema of the problem.
My girlfirend got 1 physics doctorat and write scientific research dont understand the problem, and its just impossible to explicate .

i can calcul "teta" but its not the good angle for the body.

Plz help me im lost and i hate math.
the schema
tetaSaMere.png (25.03 KiB) Viewed 512 times
Posts: 11
Joined: Thu May 14, 2015 11:59 pm

Re: Box2D body rotation problem

Postby RafaSKB » Tue Oct 24, 2017 1:14 am

I don't know exactly what your "angleQueJeMet" variable is, but here's the general idea to do what you want:

Code: Select all
// Your player's coordinates (in world units)
float aX;
float aY;

// Click coordinates (in world units)
float bX;
float bY;

// Calculate angle, in radians (NOT degrees)
float angleInRadians = Math.atan2(bY - aY, bX - aX);

// Rotate body
rocketBody.setTransform(rocketBody.getPosition(), angleInRadians);

In case this snipped doesn't help, then we will need to see more of your code.
Typing Label - Label that appears as if it was being typed in real time.
Junk E.T. - A trash-themed roguelike in space. -
Posts: 102
Joined: Sat Aug 01, 2015 5:14 am
Location: São Paulo, Brazil

Re: Box2D body rotation problem

Postby fabiitch » Thu Oct 26, 2017 1:27 pm

Thanks you it work perfect.

The probleme was my rectangle was init as (10,20) and he dont have the good orientation at start.

I init him as (20,10) and the angle is good !

Posts: 11
Joined: Thu May 14, 2015 11:59 pm

Return to General Development

Who is online

Users browsing this forum: No registered users and 5 guests