I have a model I draw in the 3D world which is a gun, and I position my camera next to it so you can see the gun in your field of view.
I use an Xbox controller's right thumbstick to rotate left and right based on the X value, and up and down based on the Y value.
I am attempting to create a limit on the Y value, so that like a real person, you can't flip continuously over your head.
No player model is drawn, only a gun, because you wouldn't be able to see the player and at this point I am trying to make this as simple as possible.
I have tried Riemer's tutorial on the X-Wing that I am sure many of you are familiar with, and the problem I run into is that the multiple axis rotation seems to distort the model and make it face sideways. I was almost sure I had it correctly this time and I have tried multiplying different rotations first, and I still run into problems. All I want at this point is a completely independent model rotation axis, so when I rotate up and down it doesn't matter what rotation my left and right is, the model will still be straight up and down as if it were a gun in real life.
Out of all of this all I want is a model I can rotate left and right all the way around as if a person was holding it, and up and down about 30 degrees each way. I need help rotating the model without it distorting itself and twisting so that it looks like I am holding it sideways in my hands. No matter what I try the model eventually ends up looking sideways relative to something, even when using transforms correctly as I thought.
My gun rotation code:
private void HandleController()
{
if (GamePad.GetState(PlayerIndex.One).Triggers.Left > 0)
{
cameraPos = new Vector3(0, .1f, 0);
}
else
{
cameraPos = new Vector3(-.1f, .1f, .3f);
}
//////
modelRotHorizontal = -GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.X / 10;
float maxan = 50;
if ((dis.Y < maxan) && (dis.Y > -maxan))
{
modelRotVertical = GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y / 10;
}
else
{
float variation = GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y / 10;
if ((dis.Y > maxan))
{
if (variation < 0)
{
modelRotVertical = variation;
}
else
{
modelRotVertical = 0;
}
}
if ((dis.Y < -maxan))
{
if (variation > 0)
{
modelRotVertical = variation;
}
else
{
modelRotVertical = 0;
}
}
}
Vector3 camup = new Vector3(1, 0, 0);
camup = Vector3.Transform(camup, rotation);
Vector3 camleft = new Vector3(0, 1, 0);
camleft = Vector3.Transform(camleft, rotation);
//rotation = new Matrix();
//rotation *= Matrix.CreateFromAxisAngle(rotation.Up, modelRotHorizontal);
rotation *= Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), modelRotHorizontal);
////again find the for
rotation *= Matrix.CreateFromAxisAngle(camup, modelRotVertical);
}
That is to get input from the user, and the actual drawing of the model:
private void DrawGun()
{
Matrix[] transforms = new Matrix[player.Bones.Count];
player.CopyAbsoluteBoneTransformsTo(transforms);
Vector3 campos = cameraPos;
campos = Vector3.Transform(campos, rotation);
campos += playerPos;
Vector3 newpos = new Vector3(0, 0, 0);
Vector3 addVector = Vector3.Transform(new Vector3(0, 0, -50), rotation);
newpos += addVector;
dis = addVector;
Vector3 camup = new Vector3(0, 1, 0);
camup = Vector3.Transform(camup, rotation);
viewMatrix = Matrix.CreateLookAt(campos, Vector3.Zero + newpos, camup);
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f), aspectratio, .000000001f, 10000f);
//Draw model
foreach (ModelMesh mesh in player.Meshes)
{
foreach (BasicEffect fx in mesh.Effects)
{
fx.EnableDefaultLighting();
fx.World = transforms[mesh.ParentBone.Index] * Matrix.CreateScale(0.005f, 0.005f, 0.005f) * rotation * Matrix.CreateTranslation(playerPos);
fx.View = viewMatrix;
fx.Projection = projectionMatrix;
}
//Draw mesh using code above
mesh.Draw();
}
}
I thought I had this right until I drew a floor from one of riemer's tutorials, and it creates a problem if I go crazy with my controller and rotate quickly in all different directions. ANY help would be greatly appreciated, this is such a simple task I would think it would be much easier.

New Topic/Question
Reply


MultiQuote






|