XNA Creators Club Online
Page 1 of 1 (11 items)
Sort Posts: Previous Next

firing from the tank's cannon

Last post 2/4/2009 10:46 AM by David Ang. 10 replies.
  • 2/1/2009 5:17 PM

    firing from the tank's cannon

    Hi

    I'm trying to attach the particle samples here, specifically the explosion. I want it to emit/fire from the tank's cannon.

    I'm not sure what to do, I tried this one:

    tankPosition + cannonBone.Transform.Translation to pass to the particle system starting position, but it seems its off. (eg, way above the tank).

    Thanks.



  • 2/1/2009 5:31 PM In reply to

    Re: firing from the tank's cannon

    David Ang:
    Hi

    I'm trying to attach the particle samples here, specifically the explosion. I want it to emit/fire from the tank's cannon.

    I'm not sure what to do, I tried this one:

    tankPosition + cannonBone.Transform.Translation to pass to the particle system starting position, but it seems its off. (eg, way above the tank).

    Thanks.



    You must first create a world transform for the cannon (by consecutively multiplying together all its parent bones), then use that matrix's translation.
    "I must Create a System, or be enslaved by another Man's. I will not Reason & Compare; my business is to Create." - William Blake
  • 2/1/2009 6:00 PM In reply to

    Re: firing from the tank's cannon

    Hi,

    The solution sounds strange but did you meant me to do something like this?

    Matrix final = leftBackWheelTransform * leftFrontWheelTransform * leftSteerTransform * turretTransform * cannonTransform;

    Notice I excluded the right*** transforms.

    So now I have a final matrix - combination of all the transformation.

    Do I now do this:

    tankPosition + final.Transform.Translation ?

  • 2/2/2009 4:18 AM In reply to

    Re: firing from the tank's cannon

    Ugh

    Didn't work

    The furthest attempt I tried was this:

    Vector3 emitPos = position + (cannonTransform.Translation + turretTransform.Translation) * uniformScale;

    But it positions at just where the cannon was attached to the turret. Now I need to apply some offset so it moves to the tip of the cannon, but I could not get my head around how to add the offset as it will not correctly align.

    Any help would be greatly appreciated.

    Thanks.


  • 2/2/2009 4:48 AM In reply to

    Re: firing from the tank's cannon

    You'll need to know / guess the length of the cannon's turret, but you should be able to use the "turretTransform.Forward * turretLength" to get the vector from the base of the turret to it's tip. First, get the absolute transform of the turret. This is the relative turret transform muliplied by it's parent bone's absolute transform. In other words it's the same transform that you would set as the current World matrix when rendering the turret ModelMesh. You should then be able to say:

    Vector3 emitPos = turretAbsoluteTransform.Translation + turretAbsoluteTransform.Forward * turretLength;
  • 2/2/2009 8:28 PM In reply to

    Re: firing from the tank's cannon

    Hi

    I'm still a little confuse I hope you can explain further.

    Are you referring to the world matrix when you say absolute transform? I have a bunch of matrices here for the cannon - each one for the bone and a world matrix.

    I also have a position for the tank in world space.

    When you gave this line:

    Vector3 emitPos = turretAbsoluteTransform.Translation + turretAbsoluteTransform.Forward * turretLength;

    I replace turretABsoluteTransform with  (worldMatrix.Translation + turretAbsoluteTransform.Forward * turretScale) * uniformScale?

    Do take note that my tanks are scaled down randomly so I need to apply the scale - so perhaps my uniformScale variable is the culprit.

    So its still way off right now.

    Thanks for your help.




  • 2/2/2009 11:09 PM In reply to

    Re: firing from the tank's cannon

    At some point, when you render your tank, there will be a World matrix used for the turret. That is the absolute, world-space transform of your turret. If you use Translation from that World matrix, you will get the center of the turret. Now, add Matrix.Forward from that world matrix times the length of your turret to that position, and you get the tip of the turret.

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 2/3/2009 7:02 AM In reply to

    Re: firing from the tank's cannon

    Hi Guys

    Sorry, for some reason I could not get my head around it and I'm getting a little frustrated :(. But I think i'm almost there.

    I have a world matrix which contains the position, rotation and scale of my whole tank.

    My whole tank have various bones - but one bone I'm particularly interested is the cannon's bone.

    So before I render the tank, I create a world matrix like this:

    Matrix world = Matrix.CreateScale(tank.UniformScale) * rotation * Matrix.CreateTranslation(tank.Position);

    Then calling draw:

    tankModel.Root.Transform = world;

    This will correctly place my tank in the correct world with the correct rotation and scaling factors.

    Now I made a copy of the cannon's transformation earlier like this:

    cannonTransform = cannonBone.Transform;

    So I need to inherit the orientation, scaling and position from the world right? So i did this:

    cannonTransform *= world;

    Then my EmitPos is something like this:

    Vector3 emitPos = ((cannonTransform.Translation + new Vector3(0f, verticalOffset, 0f)) + cannonTransform.Forward * -cannonLength); 





    And things are really off.

    I don't have problems if my tank does not rotate - but once it started rotating, that's where the emitter position gets distorted and off.





  • 2/3/2009 10:48 AM In reply to

    Re: firing from the tank's cannon



    Matrix world = Matrix.CreateScale(tank.UniformScale) * rotation * Matrix.CreateTranslation(tank.Position);

    looks good so far.


    tankModel.Root.Transform = world;
    This will correctly place my tank in the correct world with the correct rotation and scaling factors.

    Ok..

    Now I made a copy of the cannon's transformation earlier like this:
    cannonTransform = cannonBone.Transform;

    ok..

    So I need to inherit the orientation, scaling and position from the world right? So i did this:
    cannonTransform *= world;


    Then my EmitPos is something like this:
    Vector3 emitPos = ((cannonTransform.Translation + new Vector3(0f, verticalOffset, 0f)) + cannonTransform.Forward * -cannonLength); 


    So, your cannon transform is a matrix and it is being translated & rotated by the tank world.  Looks like it should be where you expect it to be.  Then you move Up a bit and out along the forward direction, which got updated with the CannonTransform*world calculation, so I would think that it should be working.   I'm curious about the -Cannonlength above though.  

    I don't have problems if my tank does not rotate - but once it started rotating, that's where the emitter position gets distorted and off.

    In what way does it get off?  If you position your tank at the world origin and rotate it clockwise, what happens to the emitter position?    It might help to see your draw function as well. 

    Best,
    Byron




    ..shaders make you feel... powerful, or very very stupid.
    http://drjbn.spaces.live.com/
  • 2/3/2009 1:31 PM In reply to

    Re: firing from the tank's cannon

    David Ang:
    So I need to inherit the orientation, scaling and position from the world right? So i did this:

    cannonTransform *= world;


    I'm not sure but I think the multiplication order could be wrong. What happens if you change it to cannonTransform = world * cannonTransform?


    David Ang:
    Now I made a copy of the cannon's transformation earlier like this:

    cannonTransform = cannonBone.Transform;


    Be careful when using this method. Since Bone.Transform is local, or relative to the parent bone, it will only work when the parent bone is at the model's origin. It is probably fine for the tank, but better to use Model.CopyAbsoluteTransformsTo() or recurse the bone hierarchy.


  • 2/4/2009 10:46 AM In reply to

    Re: firing from the tank's cannon

    OMG.

    Thank you all for trying to help. This was the culprit:

    cannonTransform *= world;

    The problem is every frame the world matrix gets concatenated into the cannonTransform - so its value continuously changes.

    It should have been like this:

    cannonTransform = cannonBone.Transform * world.

    I should have reset cannontTransform every frame - not continuously multiply it with the world matrix without resetting its initial transformation.

    That definitely fixed it.

    UGH....

    But still, couldn't do it without everyone's inputs. Thanks so much!


Page 1 of 1 (11 items) Previous Next