Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (531)
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 143 times)
0 Members and 1 Guest are viewing this topic.
Offline Gibbo3771
« Posted 2014-05-13 14: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:

1  
2  
3  
4  
5  
6  
7  
   protected Vector2 weaponMount = new Vector2();


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


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...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   @Override
   public void draw(SpriteBatch batch) {
      Vector3 mouse = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
      GameScreen.game.b2dCam.unproject(mouse);
      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);
      sprite.draw(batch);
   }


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
« Reply #1 - Posted 2014-05-13 17:13:45 »

The weapon mount has to rotate with the parent sprite:

1  
2  
3  
4  
5  
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  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (13 views)
2014-07-24 01:59:36

Riven (13 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (14 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (39 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (35 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org 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‑gaming.org
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!