Hey everyone, I’m just wondering if there is an elegant way of drawing items on my players (such as they’re equipped weapon). I’m currently trying to just place the item sprite in such a way that it lines up with the characters hands, and then updating every time the character moves or rotates. However, with this technique it is very difficult to get the item to match up properly. Does anyone know of any better ways of doing this?
Starbound uses something similar, but some bodies with no arms.
Instead of equip a weapon, I think you can equip an “arm”. So your new sprite is a arm holding a weapon and you can rotate properly setting the origin to the top of the arm and setting the position to the shoulder in the body.
If your game is a 2D platform game, you can mirror or flip the sprite to match with the left/right side.
Sorry, I should have been more explicit. My game isn’t a platformer, the view is from overhead. Whereas in your example the weapon is always in essentially the same position, here it would have to rotate around the character as they rotate. I’ve included a screenshot of the game to be more clear. Thank you anyway for the advice, it was quite detailed! And thanks to anyone in advance for the help.
Draw the sprite as you’d want it to show instead of trying to make some special stuff with code.
See this: http://gaurav.munjal.us/Universal-LPC-Spritesheet-Character-Generator/
Use a parent child setup.
So the position of the character is say, 5x5. And the item should be placed in their right hand. So lets say the sprite is facing directly right (zero degrees).
The item should be positioned 1 unit offset on the y axis. So if you were to write both positions separate, it would be 5x5 for the player and 5x6 for the item.
Now if you were to parent them (or their transforms), then you would have it be 5x5 for player and 0x1 for the item.
Here is some pseudo:
// New character at position 5, 5
Character c = new Character(5, 5);
// Give the character an item, at position 0, 1
Item i = new Item(0, 1);
// Now parent them
i.setParent(c);
// Now the get position method would look something like this
public Vector2 getPosition(){
return parent == null ? position : position.add(parent.getPosition());
}
// Draw each of them
// This will draw the item at the position of the parent, then at it's own position
batch.draw(i.sprite, i.getPosition());
// This will just draw the character, if it had a parent it would draw it at its parents position + its own position
batch.draw(c.sprite, i.getPosition());
This would be best split up into transforms of course, as you don’t want to tie objects together like this.
That’s what I was looking for! Thank you. Do you have any advice for what to do then when I rotate the sprites?
Pretty much the exact same, just replace position with angle/rotation ;).
But when the parent sprite rotates, the position of where the child sprite (the item) needs to be also changes (because the characters hand is in a different position). This is the biggest issue I’m having with the whole situation.
Yeah this is the exact same, as long as you have your sprite origin set to the middle, it should be fine.
If it is offset in anyway, it will pivot weird.
Position before rotation as well (if you are doing some sort of get order).
The easy/cheating way to do this would be to draw the character in gimp, and on a separate layer draw the weapon in his hand. Then save the character as an image and the weapon layer as a separate image as well.
Each image should be the same size so the centers of both images would be the exact same.
Then all you gotta do in your code is draw the weapon, and then the character onto that.
Then just rotate both sprites the same, as if they were one image in the code.
And bam, Bob’s your uncle.
This to me is the easiest and best way to do this. Although you don’t have to use GIMP, any program that has layers should allow you to draw the items you need easily. Still trying to figure out how I would do this in a non top down game where you have to animate each piece of armor.
Did you decide on a method to use?
I’m working of a 2D dungeon crawler that will hopefully have an inventory system so I’m looking for something similar (though the player can have his back to the screen). Gibbo’s solution does seem neat
Cheers ;).
Jacobs solution is also viable as long as the art is not to be used by things off varying sizes. If you want to allow say a large orc to use the same dagger as the player, and the player happens to be half the size…well, you just doubled your asset count. Say you wanted to use the same sprite for the UI and pickups , it’s now triple the amount of assets.
So take these into account before doing it the cheat way lol.
So is your way the standard way of doing this type of item placement? I mainly plan on only using the assets for the player but it would be cool is something like a skeleton used the same weapons.
Would you not have to redesign the assets for bigger mobs anyway ???
Parenting transforms is a pretty standard way of positioning things relative to other things, regardless of what it is. If say a skeleton was to pick up a bow on the ground, the skeleton would know where about it should hold that particular item.
You would not have to redesign the assets for a bigger mob. Why should a dagger be bigger for a 10ft orc type mob as opposed to a 6ft hero? A dagger is a dagger, if the orcs have their own weapons then of course, you would have to make more assets, as opposed to redesigning existing ones.
If you are making a game, where anybody can pick up anything, then yes; this is a pretty standard way.
![](https://www.jvm-gaming.org/letter_avatar_proxy/v4/letter/g/58f4c7/40.png)
Why should a dagger be bigger for a 10ft orc type mob as opposed to a 6ft hero?
I was thinking that an orc with a tiny dagger would look silly haha ::).
I’m going to look into using the parent/child method as it seems much easier in the long run (especially if I want lots of cool weapons). Would you happen to know where I can read more about transforms and this style of placement?
I have done this previously (open in new tab for full size):
This uses a rather complicated system of origins and roots. Basically, i have a humanoidLayout.json file, where i have all the roots of each piece of equipment for every frame for every direction for a humanoid entity. Each EquipableItem has a path to its equippedLayoutImageAtlas and equippedLayout. equippedLayoutImageAtlas is just a atlas containing all frames of the equipment and equippedLayout is a file that tells the program how to slice up the atlas, which TextureRegion belongs to what direction and frame, and where the origins are for each TextureRegion. Then the EquipmentRendererSystem renders the player and equipment, in order that depends on the direction the player is facing in. The renderer looks if you have a sword equipped, if you do, it retrieves the adequate frame and its origin, then it retrieves the root from the humanoidLayout, and finally it renders the frame so that its origin is aligned with the root.
Tl;Dr: its not really hard but takes a lot of time to make. Origins + atlases.
![](https://www.jvm-gaming.org/user_avatar/www.jvm-gaming.org/olo/40/355_2.png)
This uses a rather complicated system of origins and roots. Basically, i have a humanoidLayout.json file, where i have all the roots of each piece of equipment for every frame for every direction for a humanoid entity. Each EquipableItem has a path to its equippedLayoutImageAtlas and equippedLayout. equippedLayoutImageAtlas is just a atlas containing all frames of the equipment and equippedLayout is a file that tells the program how to slice up the atlas, which TextureRegion belongs to what direction and frame, and where the origins are for each TextureRegion. Then the EquipmentRendererSystem renders the player and equipment, in order that depends on the direction the player is facing in. The renderer looks if you have a sword equipped, if you do, it retrieves the adequate frame and its origin, then it retrieves the root from the humanoidLayout, and finally it renders the frame so that its origin is aligned with the root.
Damn. That does seem like a lot of work I’ve never actually looked into JSON’s either, only XML, HTML, CSS etc.
Now i’m not sure what to do D:
![](https://www.jvm-gaming.org/user_avatar/www.jvm-gaming.org/sauronwatchesyou/40/168_2.png)
I’ve never actually looked into JSON’s either, only XML, HTML, CSS etc.
Now i’m not sure what to do D:
The use of JSON is just an implementation detail, more or less. You can use whatever representation you’re most comfortable with.
As for what you (and presumably the OP) are wanting to do, transform hierarchies are really the way to go, as explained previously by Gibbo3771. I don’t know what framework you’re using, but many if not most graphics frameworks will include support for creating and combining transforms. Generally, what you’ll want to do here is combine the world transform for the player with the local transform (relative to the player) of whatever the player is holding (or is attached to the player or whatever). In terms of matrix multiplication, this will be either player_world_transform * object_local_transform or object_local_transform * player_world_transform, depending on the notational conventions used. The resulting transform will then be the world transform for the object that you want to render.
![](https://www.jvm-gaming.org/letter_avatar_proxy/v4/letter/j/c77e96/40.png)
As for what you (and presumably the OP) are wanting to do, transform hierarchies are really the way to go, as explained previously by Gibbo3771. I don’t know what framework you’re using, but many if not most graphics frameworks will include support for creating and combining transforms. Generally, what you’ll want to do here is combine the world transform for the player with the local transform (relative to the player).
Matrices are my nightmare but I really want this to work. I found these cool videos where the guy talks about matrices, parenting and how to set things up (Though he is using a complex 3D version and uses matrix4’s).
I’m using LibGDX which likely has matrix support but I actually do know how to add/multiple matrices anyway. For the transforms, would I simply just use vector2’s from the players/weapons positions? As my game is 2D or is it vector3’s ???. I don’t plan on having the player rotate, however the player can face away from the camera so the weaponing positioning could get more complex this way
The videos I was talking about