Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (601)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [LibGDX][Box2D] 2 Objects, rotating incorrectly  (Read 535 times)
0 Members and 1 Guest are viewing this topic.
Offline Gibbo3771
« Posted 2014-05-13 12:40:23 »

I am in a bit of a mess here and not sure how I can do this, I am pretty damn sure using cos/sin was the answer but it does not seem to be the solution.

Basically I have 2 objects, a Spaceship and a Cannon.

The Spaceship is a Box2D body, it accelerates using W and decelerates using S, turns with A and D.

I then have the Cannon that sits on a specific spot on the Spaceship, this position being 0.5625 units to the right of the Spaceship body origin which is at 0, 0 (bottom left). My game is designed using pixel art, so 16 pixels = 1 unit, my ship is 16x8 pixels.
The cannon is 8x8 pixels.

So the cannon's rotation is completely independent of the ship angle, the idea behind it is that you can free fly the ship where ever you want and have the cannon shooting in a totally different direction.

I have the cannon rotating on the spot, constantly facing the mouse. However as soon as you turn the ship, the cannon rotates around the origin of the body, when really I need to stay exactly where it is.

This has something to do with the way I position the cannon, I call the place it goes a "weapon mount", which is essentially for now just a Vector2:

   protected Vector2 weaponMount = new Vector2();

   public void update(float delta) {
      weaponMount.set(getBody().getPosition().x + 0.5625f, getBody()

This is where the problem lies I think, this above would only work if the bodies never rotated. Because if I take X and add that onto it when the angle is at 90°, it is going to be way off.

I am not sure what I should be using here? I thought if I used cos/sin but that does not seem to be any good. Here are some other snippets that might be of use, although I think this code is ok...

   public void draw(SpriteBatch batch) {
      Vector3 mouse = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);;
      angle = MathUtils.atan2(-(owner.getWeaponMount().y - mouse.y),
            -(owner.getWeaponMount().x - mouse.x));
      sprite.setOrigin(width / 2, height / 2);
      sprite.setPosition(owner.getWeaponMount().x, owner.getWeaponMount().y);
      sprite.setRotation(MathUtils.radiansToDegrees * angle);

Here is a screen shot of a ship at 0 degrees and one at 90, you can see the problem:

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline BurntPizza

« JGO Bitwise Duke »

Medals: 419
Exp: 6 years

« Reply #1 - Posted 2014-05-13 15:13:45 »

The weapon mount has to rotate with the parent sprite:

final float wepMountDistFromParentOrigin = 0.5625f; // excessive name I know, but descriptive

public void update(float delta) {
      weaponMount.set(getBody().getPosition().x + wepMountDistFromParentOrigin * Math.cos(/* parent rotation */), getBody().getPosition().y + wepMountDistFromParentOrigin * Math.sin(/* parent rotation */));
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Jesse (14 views)
2015-07-29 04:35:27

Riven (36 views)
2015-07-27 16:38:00

Riven (18 views)
2015-07-27 15:35:20

Riven (21 views)
2015-07-27 12:26:13

Riven (12 views)
2015-07-27 12:23:39

BurntPizza (30 views)
2015-07-25 00:14:37

BurntPizza (41 views)
2015-07-24 22:06:39

BurntPizza (24 views)
2015-07-24 06:06:53

NoxInc (27 views)
2015-07-22 22:16:53

NoxInc (18 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!