[LibGDX] Gun 3D model to be in front of the player

Hey guys,

I been having this problem that I want to give a 3d model the same position and rotation as the player, kinda like having the origin of the gun on the player’s position and “look” to where the player is looking.
So for this I have the player translation and the camera direction.

gun.instance.transform.set(playertranslation.x, playertranslation.y, playertranslation.z, camera.direction.x, camera.direction.y, camera.direction.z, 0);

Note that “instance” is the ModelInstance and “playertranslation” is just a Vector3 with the player’s position.

Anyone of you guys figured this out? There seems to be something with Quaternions and Matrix transformations, but it’s much harder than I though and can’t seem to find a way of doing it, specially since I don’t understand them that much.

Cheers.

You just need to rotate the gun then translate it to the player’s position plus the camera’s forward vector(scaled of course).

i transform my model like below, note that I’m using z-up coordinate system!


instance.nodes.first().translation.set(cpm.get(e).getInterpolatedValue());//set position
instance.nodes.first().rotation.set(new Vector3(0f, 0f, 1f), crm.get(e).getInterpolatedValue());//set rotation by providing up-vector and rotation around it
instance.calculateTransforms();//apply the transforms to the instance

You can use different parameters to set rotation, just read the java-docs.

Hey, I’m trying to get what is “cpm.get(e)”, I just can’t find this method “getInterpolatedValue()”.

Can you please explain further?

Plus I have the camera rotation (it seems) as a “direction” Vector3, while the rotation on the ModelInstance asks me for a Quaternion

Oh well ;D What about reading the documentation so that you can write code without guessing?

ModelInstance
Node

Normal Code:


instance.nodes.first().translation.set(...);//set position
instance.nodes.first().rotation.set(...);//set rotation by providing up-vector and rotation around it
instance.calculateTransforms();//apply the transforms to the instance

Code showing the class instead of the variable-name:


ModelInstance.Array<Node>.Node.Vector3.set(Vector3);//set position
ModelInstance.Array<Node>.Node.Quaternion.set(Vector3, float);//set rotation by providing up-vector and rotation around it
ModelInstance.calculateTransforms();//apply the transforms to the instance

You set position and rotation, then recalculate transforms.
nodes.first gives you the first Node in your models Node-list, you’ll most likely need to transform this Node.

You Just need to put in your values, the values (cpm.get(e).getInterpolatedValue()) are not important for you.
cpm.get(e) returns a Component that contains position data, which is not related to LibGDX, I just left it there.