Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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 weird sprite rotation error  (Read 2418 times)
0 Members and 1 Guest are viewing this topic.
Offline cheatsguy

Junior Member


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Posted 2013-01-01 15:15:29 »

I am currently working on a project where the character's arm is rotated around based on the mouse's location, using Math.atan2(). My problem is, I want the player to be holding weapons in their hand, and it gets a little weird. I'm using the sprite class for rotated images, so I have to position and then rotate the sprite in question. Currently, this is accomplished by this:
1  
2  
3  
4  
5  
6  
7  
8  
primaryWeaponl.setRotation(aimAngle + 90);
      primaryWeaponl.setPosition(
            playerScreenX + armXDis
                  + (float) Math.sin(Math.toRadians(aimAngle))
                  * (characterArm.getHeight()),
            playerScreenY + armYDis
                  - (float) Math.cos(Math.toRadians(aimAngle))
                  * characterArm.getHeight());

my issue is that when I position the image, it looks just fine:


but when I add in the rotation (the first line):



I'm not sure what is causing this, but any help would be great ( sorry for the image shapes, I simply cropped them by hand  Undecided ) could it be a problem with the sprite class? I've tried setting the origin for rotation, but it doesn't seem to help.

Busy between school, work, life, games, programming and general screwing around.
If you'd like some pixel art for your game, send me a PM, i'll see what I can do.
Current project: http://elementalwarblog.wordpress.com/
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #1 - Posted 2013-01-01 19:41:23 »

You'll need to set the sprite origin to the point you want to rotate around. The origin is relative to the sprite's position (the bottom left corner).

Offline cheatsguy

Junior Member


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Reply #2 - Posted 2013-01-01 20:15:23 »

For some reason, setting the sprite origin is doing nothing for me. I set it to (Sprite.getWidth() - 8, 6) ( the location of the handle) and I'm still having the same issue, in the exact same location.

Busy between school, work, life, games, programming and general screwing around.
If you'd like some pixel art for your game, send me a PM, i'll see what I can do.
Current project: http://elementalwarblog.wordpress.com/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #3 - Posted 2013-01-01 21:10:16 »

Be sure you are calling sprite.draw(batch) and not batch.draw(sprite). This is an unfortunate API gotcha (Sprite is-a TextureRegion and should have used composition instead, can't change it now).

Offline cheatsguy

Junior Member


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Reply #4 - Posted 2013-01-01 22:33:05 »

I have been using sprite.draw, I'm really not sure what I'm doing wrong. Would it be helpful if I posted the entire render() loop? in the create() loop it sets the player's arm's origin just fine, so I'm not sure what the error here is :/

Busy between school, work, life, games, programming and general screwing around.
If you'd like some pixel art for your game, send me a PM, i'll see what I can do.
Current project: http://elementalwarblog.wordpress.com/
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #5 - Posted 2013-01-01 22:35:23 »

Yes of course.

Offline cheatsguy

Junior Member


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Reply #6 - Posted 2013-01-02 00:23:36 »

Here it is:
http://pastebin.java-gaming.org/43eff31693d

Busy between school, work, life, games, programming and general screwing around.
If you'd like some pixel art for your game, send me a PM, i'll see what I can do.
Current project: http://elementalwarblog.wordpress.com/
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #7 - Posted 2013-01-02 09:40:57 »

Don't call new in your render method.

You are changing the primaryWeaponl position based on the rotation angle. Just set the position, origin, and rotation, then draw.

I recommend creating a simpler test case where you can experiment more easily. You can use this as a base:
https://code.google.com/p/libgdx/wiki/GettingHelp#Barebones_SpriteBatch

Offline cheatsguy

Junior Member


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Reply #8 - Posted 2013-01-02 16:06:15 »

Thanks nate,  i'll just use the bare-bones and then see wht my issue is. Thanks Smiley

EDIT: decided to go the easy route since this has been bugging me for the past few days, and it looks nicer than it ever could the other way.

Busy between school, work, life, games, programming and general screwing around.
If you'd like some pixel art for your game, send me a PM, i'll see what I can do.
Current project: http://elementalwarblog.wordpress.com/
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (13 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (31 views)
2014-08-16 06:45:27

Tekkerue (31 views)
2014-08-16 06:22:17

Tekkerue (19 views)
2014-08-16 06:20:21

Tekkerue (29 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (42 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!