Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to Game programming

Problem with attached weapon  [SOLVED]

All about the StoneScript

Problem with attached weapon

Postby DarkSoul » 16 Apr 2012, 20:09

Hello.
I'm working on a top down view game and I have a big problem. When I try attach my weapon to the hand, the weapon is moving a bit extrange and does not follow the hand bone, you can see the problem in the following video:
http://youtu.be/BuBt4pVS6qY

The code for make it is:
Code: Select all
--Guarda la posición de los huesos de la mano
local bx, by, bz = shape.getSkeletonJointTranslation (this.Character ( ), "Bip01_R_Hand_id", object.kGlobalSpace)
--Guarda la rotación de los huesos de la mano
local rx, ry, rz = shape.getSkeletonJointRotation (this.Character ( ), "Bip01_R_Hand_id", object.kGlobalSpace)
if (this.CurrentWeapon ( ) == "M9") then       
   --Posiciona el arma en la posición de la mano
   local hWeapon = table.getAt (this.WeaponList ( ), 0)
   object.setParent (hWeapon, this.Character ( ), true)
   object.setTranslation (hWeapon, bx, by, bz, object.kGlobalSpace)
   object.setRotation (hWeapon, rx, ry, rz, object.kGlobalSpace)
   --Fix para la posición y rotación del arma
   object.setTranslation (hWeapon, 0.4, 0.07, -0.1, object.kLocalSpace)
   object.setRotation (hWeapon, 0, 90, 0, object.kLocalSpace)
end


I tried to call the code in every frame, after the final movement, before the movement, before and after the movement... in all cases, but the problem persist.
I hope you can help me with it, thank you very much!
User avatar
DarkSoul
Gold Boarder
Gold Boarder
 
Posts: 221
Location: Spain

Re: Problem with attached weapon

Postby broozar » 16 Apr 2012, 20:16

hello DarkSoul,

i see a couple of potential problems in this code.

1. setParent. call it only once (onInit or the onEnter phase of a state). also, by default, shiva has "inhertit rotation, translation, and scale" active for child objects. it might very well be that your setTranslation code code is fighting with the parenting function.

2. kLocalSpace in onEnterFrame. if you call a translation in kLocalSpace continuously, the object will move constantly.

3. look at the code snippet in the wiki that deals with attaching things to bones: http://www.stonetrip.com/developer/wiki/index.php?title=Mounting_an_item_to_a_specific_skeletal_bone i think that explains things a lot better.
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3340
Location: Berlin - Germany

Re: Problem with attached weapon

Postby NiCoX » 16 Apr 2012, 20:27

Hi,

There is an alternative (and new since 1.9.1) way to handle that, using:
http://www.stonetrip.com/developer/doc/api/object-bindTransformToParentSkeletonJoint
http://www.stonetrip.com/developer/doc/api/object-unbindTransformFromParentSkeletonJoint

Note that you may have to bind a dummy, parent to the actual ammo, in order to offset the latter. Indeed this function will hardly override the bound object transform (by the joint transform).
User avatar
NiCoX
Platinum Boarder
Platinum Boarder
 
Posts: 5627
Location: France

Re: Problem with attached weapon

Postby broozar » 16 Apr 2012, 20:36

interesting. i assume this function "only" works with translation, so he would have to align his weapon (that is parented to the dummy which again is parented to the joint using this new function) afterwards?
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3340
Location: Berlin - Germany

Re: Problem with attached weapon

Postby DarkSoul » 16 Apr 2012, 22:16

Hello, the problem persist.
NiCoX I tried with object.bindTransformToParentSkeletonJoint but I can't rotate my weapon, so for make it, I created a dummy, I have related the dummy with the character and then, use object.bindTransform... with dummy and the bone. Then I use object.matchTranslation with my weapon and the dummy, but the problem persist! :(
User avatar
DarkSoul
Gold Boarder
Gold Boarder
 
Posts: 221
Location: Spain

Re: Problem with attached weapon  [SOLVED]

Postby NiCoX » 17 Apr 2012, 00:03

i assume this function "only" works with translation

It actually works with translation/scale/scale :)

I created a dummy, I have related the dummy with the character and then, use object.bindTransform... with dummy and the bone. Then I use object.matchTranslation with my weapon and the dummy, but the problem persist!

You have to make your gun child of the dummy, else the problem will indeed be just be the same. In other words:

- main character is the root
- the dummy is child of the main character, and uses object.bindTransformToParentSkeletonJoint
- the gun is child of the dummy, and has some offset/rotation to be placed correctly

Let me know.
User avatar
NiCoX
Platinum Boarder
Platinum Boarder
 
Posts: 5627
Location: France

Re: Problem with attached weapon

Postby broozar » 17 Apr 2012, 00:08

you could also try:

- character is root
- dummy model attached to character joint using object.bindTransformToParentSkeletonJoint
- matchTranslation performed on the dummy and the gun, and matchRotation on gun and main character. no parenting between gun and dummy. with this approach, you eliminate potential problems with TRS inheritance and the gun always stays parallel to the ground, which could be good for your type of game.

@NiCoX nice :) will try it out tomorrow.
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3340
Location: Berlin - Germany

Re: Problem with attached weapon

Postby DarkSoul » 17 Apr 2012, 07:45

Oh, I see, I did not understand the last time, now all works very fine. Thank you very much!
User avatar
DarkSoul
Gold Boarder
Gold Boarder
 
Posts: 221
Location: Spain

Re: Problem with attached weapon

Postby soybean » 10 May 2012, 09:49

Awesome! I've been waiting for this feature :). Now I can add particles and trails on the equips :)
User avatar
soybean
Senior Boarder
Senior Boarder
 
Posts: 71


Return to Game programming