LibGDX Draw item in players hand (2D top down view)

Is this side-view, or top-down? Also, what do you mean by the player facing away from the camera? (Maybe this was mentioned earlier…)

Top down that allows the player to show his back to the camera. See image below of my game view :slight_smile:

http://s7.postimg.org/oft7c4sbf/javaw_2015_05_28_14_35_20_22.png

I don’t know if this will give you what you need, but when only positioning is involved (no scale, rotation, etc.), combining transforms reduces to simple vector addition, which is trivial. (In the OP’s case, it looks like rotation would be involved, in which case there’s a little more to do.)

Gibbo3771 gave an example of how to do this with positions only, here:

Basically, you’d want something like the following:

  • If the framework has support for anchoring, set the anchor point for the item sprite to wherever you want it to be held. (For example, if the item is a staff, the anchor point would be about midway along the staff.)
  • Determine the offset (a 2-d vector) from the anchor point of the player avatar to the player avatar’s hand.
  • The world position of the item will then be the player position plus this offset.

Hi Jesse,

I did the anchor point thing and the positioning and it works but of course at the moment it only works when the player is facing forward as the players hand location changes when he faces a direction/animations.

Do you know how I would able to setup the positioning so the item can always stay in the hand? Do I have to manually figure out 4 different vectors from the player to the players hand and then just add them the same as before?

Thanks again ;D

Yeah, pretty much. Maybe someone else will have a better suggestion, but the relative position of the hand for each frame of animation is basically a content issue, so it’s just something you have to specify. For a more sophisticated system you might have an editor that allows you to specify the hand position for each frame of animation and then exports that information as JSON, XML, or whatever, which is then read by the game code. But for something fairly straightforward like this, I’d just do the simplest thing that works. (I’d at least store the offsets in a text file so that they’re easily modifiable, but to begin with you could just hard-code them, just to get things working.)

Damn. That’s going to take me forever D:

I take it that i’ll have to also specify the rendering process for when the player is facing away from the camera as well so that the item isn’t rendered in front of the player

How many separate frames of animation (that would affect hand position) do you have total?

[quote]I take it that i’ll have to also specify the rendering process for when the player is facing away from the camera as well so that the item isn’t rendered in front of the player
[/quote]
Yes, that sounds right.

Umm. North/south/east/west have 8 frames each that affect the hand position and then the animations for stabbing/casting is around 8 each as well.

Likely… 40? xD

Well, that’s not too many :slight_smile: Maybe someone else can suggest something, but if it’s just straight-up pixel art, I’m not sure how you’d automate finding the hand offsets. It seems like it would just need to be done manually.

Didn’t want to just hand out a solution until you had multiple input, but here is pretty much what I use.

A member of JumpButton Studios helped me understand this, I now use it for everything, I even wrote it in C# and C++ it was that damn useful.

You can use it if you wish, this is the basic version that just parents everything, written in Java :

import com.badlogic.gdx.math.Affine2;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Quaternion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

/**
 * Represents the data for the position, scale, angle and origin
 * 
 * @author Stephen Gibson
 */
public class Transform {

	public static Quaternion ZERO_ROTATION = new Quaternion();
	public static Matrix4 ZERO_TRANSFORM = new Matrix4();
	public static Vector3 VECTOR_ZERO = new Vector3();
	public static Vector3 VECTOR_ONE = new Vector3(1, 1, 1);

	// The transform that is parented to this transform, for relative position,
	// scale and rotation
	protected Transform parent;
	public Vector3 position = new Vector3();
	public Vector3 scale = new Vector3(1, 1, 1);
	public Quaternion rotation = new Quaternion();
	
	// Scale and rotation matrix
	public Matrix4 model = new Matrix4();
	public Matrix4 transform = new Matrix4();

	/**
	 * Sets the parent of this transform, this transform won't be updated until the next step
	 * @param parent
	 */
	public void setParent(Transform parent){
		this.parent = parent;
	}
	
	public Matrix4 getParentTransform(){
		if(parent != null)
			return parent.getTransform();
		return ZERO_TRANSFORM;
	}
	
	public Quaternion getParentRotation(){
			if(parent != null)
				return parent.getRotation();
			return ZERO_ROTATION;
	}
	
	public Vector3 getParentScale(){
		if(parent != null)
			return parent.getScale();
		return VECTOR_ONE;
	}
	
	public Matrix4 getModelMatrix(){
		model.idt();
		model.scl(scale);
		model.rotate(rotation);
		return model;
	}
	
	public Matrix4 getTransform(){
		transform.idt();
		transform.mul(getParentTransform());
		transform.translate(position);
		transform.mul(getModelMatrix());
		return transform;
	}
	
	/**
	 * Gets the position relative to the parent
	 * @return
	 */
	public Vector3 getPosition(){
		return position.cpy().mul(getParentTransform());
	}
	
	/**
	 * Gets the absolute position
	 * @return
	 */
	public Vector3 getLocalPosition(){
		return position;
	}
	
	/**
	 * Get the scale relative to the parent
	 * @return
	 */
	public Vector3 getScale(){
		Vector3 scl = parent == null ? VECTOR_ONE : parent.getScale();
		return scale.cpy().scl(scl.x, scl.y, scl.z);
	}
	
	/**
	 * Get the rotation relative to the parent
	 * @return
	 */
	public Quaternion getRotation(){
		return rotation.cpy().mul(getParentRotation());
	}
	
	public void setPosition(Vector2 position){
		this.position.set(position, 1 );
	}
	
	public void setScale(Vector2 scale){
		this.scale.set(scale, 1);
	}
	
	public void setRotation(Quaternion rotation){
		this.rotation = rotation;
	}
	
	public void setAngleAxis(float x, float y, float z, float degrees){
		rotation.setFromAxis(x, y, z, degrees);
	}
} 

This version here, although not written in Java, allows you to choose what part of the transform to parent. Should be easy to duplicate LINK

Thank you, Gibbo ;D

I don’t think I know enough about matrices/this type of math at the moment so i’m going to go the cheating route for this game (The built in assets method). I know how to add/multiple matrices but i’ve never dealt with them in LibGDX/games so it’s a bit overwhelming.

I have to decide on the collision detection for my weapons as well. I’m likely going to use a separate rectangle around the player, with varying size for the weapon OR give each weapon a hitbox that changes direction depending on how the player faces :point:

You are free to implement that class into your project, don’t worry about how it works, hell I’m still learning matrices lol.

Oh, okay ;D

For this to work, I need to create some sort of ‘joint’ for the hand, correct? As I need to tell the weapon where to connect to. This would still mean having to move the joint manually for each frame of the animation and then this math would move the weapon for me

Depends how you are doing it, how I would be to have the character know where is hand is (via joint, Mount or something). Then have the weapon know where it should be held using a similar system. If your animation moves these points visually then yes, you will need to have some sort of system that moves the joints each frame and also reassigns the transform position.

Alternatively, you could also give the joint a transform, parent that to the character, then parent the item to the joint. Structured correctly, the joint can easily be updated to match the characters state and animation, and the weapon will move accordingly.