Monday, May 15, 2017

animation - C# - separate sprite for 2d character and arms



I'm making a 2D action platformer in C# (not using XNA).


So far I have the character drawn on the screen, animating and moving. The sprite sheets provided by the artist have the character and arms in two different sheets. And this is my problem.


How would I go about adding the arms to the character? I have a general idea that I would draw the 1st hand (one further away from screen) then character and then the other hand (the one closer to the screen). What I have an issue with is, how could I combine them into one object in order to make coding the movement and actions easier? Also I guess I should use anchor points to make positioning of individual parts easier, and also to make my life easier when I start adding weapons.


The actual case becomes a bit more complex as the arms sprites have to be changed depending on the type of the weapon the character is using. Also the hand on the arm closer to the screen is missing a thumb which is so that it could be animated during a reload with certain weapons.


I guess I'm looking for a way to construct a player object from the separate sheets in order to keep that full character easily drawable, since in some cases, the whole object should be switched, like a climbing animation or acrobatic move animation have arms on the character already.


Thank you in advance.



Answer



You need to build an object graph for your character. Have the basic player object contain a master sprite, such as the torso. In the master sprite, have Joints(which have an offset, translation, from the parent sprite), which then contain child objects(such as an arm, or two) with rotations. The children are the same as type the master, having joints of their own, and then child objects(such as a hand), with a rotation. Continue until you've built a graph containing all the parts of your character.


When you render it, call a render function on your master, then translate to a joint, render all its children, pop your translation and continue to the next joint. If you're not sure on how to do this, take a look at Recursion.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...