Game code as ICE Kinematics reference

Discussions about SOFTIMAGEs© Interactive Creative Environment©
Post Reply
User avatar
dwigfor
Posts: 395
Joined: 17 Nov 2009, 17:46

Game code as ICE Kinematics reference

Post by dwigfor » 08 Nov 2010, 18:01

EDIT: Here's some pictures! Unfortuately it seems that Flickr doesn't want to allow placing .jpgs to display on forums anymore. They want you to click a link to take you to Flickr, which sucks. So I attached some images... The LookAt isn't really cleared up for me.. I found a method that worked in one instance, then ran into problems with another. On my car rig, I was able to use "Increment Rotation by 2 Vectors". If you know of better methods to replicate this code, please attach pictures (and an explanation of why it's better would be great! :-bd )

Hi everyone. I'm currently setting up an ICE compound to use Xbox 360 controller as input device. It got me thinking - there's a lot of stuff that's relevent to ICE with game programming. Lots of stuff dealing with 3d vectors, matrices, etc. While matrices are very slowly starting to sink in, they're still a bit too advanced for me. Especially when dealing with rotation matrix. While I'm starting to get why they are useful, I'm still a bit unclear as to how they are implemented.

For instance, here's how to create a 3rd person camera - http://msdn.microsoft.com/en-us/library ... o.31).aspx

Code: Select all

1.Decide on a camera offset vector from the avatar. An offset of the form (0, N, −N) will put the camera above and behind the avatar at a 45-degree angle.

Vector3 thirdPersonReference = new Vector3(0, 200, -200);
I was gonna post a picture of 3d vector, but since I'm limited to 3 pics, I've removed it. Easy enough - a 3d vector.

Code: Select all

3.Create the rotation Matrix with CreateRotationY, using the avatar's current rotation as its parameter. 

Matrix rotationMatrix = Matrix.CreateRotationY(avatarYaw);
Create a 3x3 Matrix based on Avatar's rotation
Create a 3x3 Matrix based on Avatar's rotation
Gamecode-ICE_RotMatrix.jpg (64.59 KiB) Viewed 4141 times

Code: Select all

4.Transform a copy of the camera offset vector by using the Transform method and the rotation Matrix. 

// Create a vector pointing the direction the camera is facing.
Vector3 transformedReference = 
    Vector3.Transform(thirdPersonReference, rotationMatrix);
Multiplies a 3x3 vector by a 3x3 rotation matrix.  If the Avatar rotates, this compound will essentially "parent" the camera to it.
Multiplies a 3x3 vector by a 3x3 rotation matrix. If the Avatar rotates, this compound will essentially "parent" the camera to it.
Gamecode-ICE_TransformMatrix.jpg (108.52 KiB) Viewed 4141 times

Code: Select all

5.Calculate the camera's position as the avatar's position, and calculate the transformed camera offset. 

// Calculate the position the camera is looking from.
Vector3 cameraPosition = transformedReference + avatarPosition;
(add the two 3d vectors)
Add the offset position plus the avatar's pos. When you transform the vector, it is always around 0,0,0 so you need to add the avatar's position to keep it "attached"

Code: Select all

6.Create a view Matrix with the CreateLookAt method. The avatar's current position will be the cameraTarget parameter. 

view = Matrix.CreateLookAt(cameraPosition, avatarPosition, 
    new Vector3(0.0f, 1.0f, 0.0f));
Rotates the camera to look at the avatar.
Rotates the camera to look at the avatar.
Gamecode-ICE_LookAt.jpg (99.18 KiB) Viewed 4141 times

Code: Select all

7.Create a new projection Matrix with CreatePerspectiveFieldOfView. The projection Matrix controls how camera coordinate values are transformed to screen coordinates. 

Viewport viewport = graphics.GraphicsDevice.Viewport;
float aspectRatio = (float)viewport.Width / (float)viewport.Height;

proj = Matrix.CreatePerspectiveFieldOfView(viewAngle, aspectRatio, 
    nearClip, farClip);
(Is that part and further steps needed?)

I'm still a bit lost and not exactly sure how this works, but I think it's very useful and want to understand it. Please help explain it better.

-Edited the title.

Thanks,
-Dave
Last edited by dwigfor on 07 Dec 2010, 17:55, edited 2 times in total.

User avatar
dwigfor
Posts: 395
Joined: 17 Nov 2009, 17:46

Re: Game code as ICE reference?

Post by dwigfor » 08 Nov 2010, 18:26

I think this link will be helpfull - http://forums.create.msdn.com/forums/p/8105/42759.aspx

Couple of links to look at, but I also found this juicy tidbit:
Some code will probably be best here. A possible implementation of the Vector3.Transform() method could be:

Code: Select all

public static void Transform(ref Vector3 position, ref Matrix matrix, out Vector3 result)
{   
    result.X = (position.X * matrix.M11) + (position.Y * matrix.M21) + (position.Z * matrix.M31) + matrix.M41;
    result.Y = (position.X * matrix.M12) + (position.Y * matrix.M22) + (position.Z * matrix.M32) + matrix.M42;
    result.Z = (position.X * matrix.M13) + (position.Y * matrix.M23) + (position.Z * matrix.M33) + matrix.M43;
}
The Vector3.Transform() method just multiplies a matrix with a vector. The reason why it's in a special method is because in linear algebra you can't multiply a 4x4 matrix (as is the case of XNA's Matrix type) with a 3 component vector. You can only multiply a 4x4 matrix by 4 component vector. You can turn a 3 component vector into a 4 component vector using what's known as homogeneous coordinates. The links that Kris posted should cover this material.
The links are really good in that above link, so I make them easier to access:
http://www.gamedev.net/reference/articl ... cle695.asp
http://www.gamedev.net/reference/articl ... le1129.asp (Visualizing 2d vector addition)
http://www.gamedev.net/reference/list.a ... yid=28#259 - There's a TON of stuff there!
Last edited by dwigfor on 08 Nov 2010, 19:54, edited 4 times in total.

User avatar
dwigfor
Posts: 395
Joined: 17 Nov 2009, 17:46

Re: Game code as ICE reference?

Post by dwigfor » 08 Nov 2010, 19:17

Andy Nicholas has some articles with nice visual animations about the matrix on his blog -
http://www.andynicholas.com/?p=861
http://www.andynicholas.com/?p=911

User avatar
dwigfor
Posts: 395
Joined: 17 Nov 2009, 17:46

Re: Game code as ICE reference?

Post by dwigfor » 17 Nov 2010, 12:25

Hey guys, I've run into a new problem that I'm not sure how to properly setup in ICE. I'm try to figure out how

Code: Select all

+=
and

Code: Select all

*=
translate into ICE.

I tried declaring a value (set value self.X360_LStickX) which gets executed first. Then later in ICE tree, I get data from previous frame (self.X360_LStickX) and add or multiply it with the output port of my X360 Controller Compound's Left Stick X (which is from a null's global pos x rescaled to -1..+1 (which is targetted from the device capture joystick input).

When I let go of joystick, my angle input resets back to 0, rather than staying in place.

Thanks,
-Dave

User avatar
dwigfor
Posts: 395
Joined: 17 Nov 2009, 17:46

Re: Game code as ICE reference?

Post by dwigfor » 24 Nov 2010, 18:24

Hi everyone. Just wondering if you had a chance to go thru this stuff. If you haven't I HIGHLY recommend it. It has been super useful!!! ;)

There was a sentence in one of the articles that took me a while to fully appreciate how true it is:
For serious 3D graphics, you will need to use matrix math.
I might change that to, if you want to use ICE Kinematics, you will need to use matrix math!!

It took me a long time to understand how it works, but I finally had the OMG moment. :-o One of the biggest difficulties that I've had with ICE Kinematics is that every needs to use Global Coordinates. I can't parent anything or work in local, like I am normally used to. Matrices are how you work in local coordinates. Parenting is based on matrices. The entire Scale, Rotation, Position system is all based on matrices. I'll try to edit my first post with ICE nodes that replicate the code, as fas as I've been able to get to work.. :-bd The one I'm not entirely sure about is the lookat method.

User avatar
dwigfor
Posts: 395
Joined: 17 Nov 2009, 17:46

Re: Game code as ICE reference?

Post by dwigfor » 29 Nov 2010, 23:26

Added some pics to the first post.

User avatar
Hirazi Blue
Administrator
Posts: 5107
Joined: 04 Jun 2009, 12:15

Re: Game code as ICE reference?

Post by Hirazi Blue » 30 Nov 2010, 10:36

Just to add some support to this otherwise "lonesome" thread:
What you're trying to do looks very interesting.
I will have to look into it myself soon (as time permits). :-bd
Stay safe, sane & healthy!

User avatar
dwigfor
Posts: 395
Joined: 17 Nov 2009, 17:46

Re: Game code as ICE reference?

Post by dwigfor » 04 Dec 2010, 17:40

Here's another thread that I've been replying to that coincides with this. Hopefully redundant info helps it sink in: http://www.xsibase.com/forum/index.php? ... 0#lastPost
And to answer the other question I had in this thread (how to continually add values): http://www.xsibase.com/forum/index.php? ... adid=43846

Post Reply

Who is online

Users browsing this forum: trendiction [Bot] and 35 guests