Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (799)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (865)
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] Gun 3D model to be in front of the player  (Read 5784 times)
0 Members and 1 Guest are viewing this topic.
Offline scanevaro
« Posted 2015-11-21 21:45:02 »

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.

1  
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.
Offline Longarmx
« Reply #1 - Posted 2015-11-22 04:12:50 »

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

Offline VaTTeRGeR
« Reply #2 - Posted 2015-11-22 10:30:59 »

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

1  
2  
3  
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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline scanevaro
« Reply #3 - Posted 2015-11-26 15:50:07 »

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

1  
2  
3  
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
Offline VaTTeRGeR
« Reply #4 - Posted 2015-11-26 16:46:22 »

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

ModelInstance
Node

Normal Code:
1  
2  
3  
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:
1  
2  
3  
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.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (192 views)
2019-09-04 15:33:17

hadezbladez (4880 views)
2018-11-16 13:46:03

hadezbladez (1791 views)
2018-11-16 13:41:33

hadezbladez (5152 views)
2018-11-16 13:35:35

hadezbladez (1019 views)
2018-11-16 13:32:03

EgonOlsen (4381 views)
2018-06-10 19:43:48

EgonOlsen (5224 views)
2018-06-10 19:43:44

EgonOlsen (2950 views)
2018-06-10 19:43:20

DesertCoockie (3869 views)
2018-05-13 18:23:11

nelsongames (4295 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!