Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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 776 times)
0 Members and 1 Guest are viewing this topic.
Online cheatsguy

Junior Member


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Posted 2013-01-01 16: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 Wizard


Medals: 81
Projects: 3


Esoteric Software


« Reply #1 - Posted 2013-01-01 20: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).

Online cheatsguy

Junior Member


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Reply #2 - Posted 2013-01-01 21: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 Wizard


Medals: 81
Projects: 3


Esoteric Software


« Reply #3 - Posted 2013-01-01 22: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).

Online cheatsguy

Junior Member


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Reply #4 - Posted 2013-01-01 23: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 Wizard


Medals: 81
Projects: 3


Esoteric Software


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

Yes of course.

Online cheatsguy

Junior Member


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Reply #6 - Posted 2013-01-02 01: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 Wizard


Medals: 81
Projects: 3


Esoteric Software


« Reply #7 - Posted 2013-01-02 10: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

Online cheatsguy

Junior Member


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Reply #8 - Posted 2013-01-02 17: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  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (84 views)
2013-05-17 21:29:12

alaslipknot (92 views)
2013-05-16 21:24:48

gouessej (123 views)
2013-05-16 00:53:38

gouessej (115 views)
2013-05-16 00:17:58

theagentd (127 views)
2013-05-15 15:01:13

theagentd (114 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (187 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.09 seconds with 21 queries.