Jump to content
  • Advertisement
Sign in to follow this  

[SOLVED] c++ (unmanaged) to c# (XNA) problem

This topic is 3802 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys! Havent posted here in a while as i havent been coding for a while (got a new job taking up time and coding is just a hobbie for me). I used to play around with Direct3D using c++ but recently decided to check out XNA. First impression is it rocks btw! ;) Not sure how easy it will be to publish/share my programs but i will address that when i come to it. BUT i am trying to get some old code converted to c# XNA from c++ (unmanaged) and i am having a problem. My old code was a camera class. ie keep track of position and look vectors etc and create a veiw and projection matrix from the information. Mostly i have rewritten it fine in c# but for some reason some specific functions do not work. example:- This function tilts the camera up (the view matrix is made later in an update() functin)
void CAMERA::tiltUp(float deltaTime)

and is called using

where KEYDOWN is a macro that checks the key state. In XNA c# i thought this would work:-
public void tiltUp(int millisecs)
            //do the tilting
            Matrix.CreateFromAxisAngle(ref right, (-1.0f * (millisecs * turnSpeed)),out worker);
            Vector3.TransformNormal(look, worker); 
            Vector3.TransformNormal(up, worker);

            //As changed these best normalize again

where worker is just a matrix i use for all the classes calculations. This is called in the game classes overidden update member function with the following:-
if (Keyboard.GetState().IsKeyDown(Keys.Up))

but nothing happens. What is wierd is the move forward/backward functions work fine as does the function that actually makes the viewMatrix (a one liner in XNA - colour me impressed!). My problem is that i dont know if the code is wrong as im new to c# or if i am using the wrong methods from XNA or if my math is just wrong (i am rusty). Any help appreciated. Regards P.S. This is the code for a Property (the first property i ever made! A good feature of c# in my opinion) of the look vector in the above cove code. It is hideous. There must be a better way of doing this?!
public Vector3 Look 
                //Check value is not all zero
                if (value == Vector3.Zero) throw new ArgumentOutOfRangeException("Look");
                //store and nomalize this new value
                Vector3 newLook = value;
                //Assuming look is also normalised

                //Find cross vector (it will be rotation axis)
                Vector3 cross = Vector3.Cross(newLook, look);

                //Find cos of angle between new and old look
                float dot = Vector3.Dot(newLook, look);

                //un-cos it
                dot = (float)Math.Acos(dot);

                //Rotate up BY this angle AROUND cross vector to maintain orthoganal vectors
                Matrix trans = Matrix.CreateFromAxisAngle(cross,dot);
                up = Vector3.Transform(up, trans);

                //Finally asign new look value
                look = newLook;

                //recalc right vector
                right = Vector3.Cross(up, look);

                //Note maintained normalized (unit vector) status after final asignment
            } get { 
                return look; 

[Edited by - galapogos22 on July 15, 2008 5:10:34 PM]

Share this post

Link to post
Share on other sites
The answer to your first problem, I believe, is that you are ignoring the return value of the TransformNormal functions. The TransformNormal function doesn't modify the input vector, it returns a new one.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!