我正在设计一个3D游戏,在这个游戏中,我在宇宙飞船后面有一个摄像头,可以四处移动。我也在游戏中添加了一些小行星,我想添加子弹。我使用四元数来控制宇宙飞船和它后面的相机的路径。然而,当我在子弹上使用相同的四元数(具体的旋转以便我可以计算出子弹的路径)时,我得到了一个非常有趣的效果,路径是完全错误的。你知道为什么会发生这种情况吗?
public void Update(GameTime gameTime)
{
lazerRotation = spaceship.spaceShipRotation;
Vector3 rotVector = Vector3.Transform(new Vector3(0, 0, -1), lazerRotation);
Position += rotVector* 10 * (float)gameTime.ElapsedGameTime.TotalSeconds;
//
}
//spaceShipRotation = Quaternion.Identity;
//Quaternion additionalRot = Quaternion.CreateFromAxisAngle(new Vector3(0, -1, 0), leftRightRot) * Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), upDownRot);
//spaceShipRotation *= additionalRot;
//Vector3 addVector = Vector3.Transform(new Vector3(0, 0, -1), spaceShipRotation);
// futurePosition += addVector * movement * velocity;
public void Draw(Matrix view, Matrix projection)
{
// //Quaternion xaxis = Quaternion.CreateFromAxisAngle(new Vector3(0, 0, -1), spaceship.leftrightrot);
// //Quaternion yaxis = Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), spaceship.updownrot);
// //Quaternion rot = xaxis * yaxis;
//Matrix x = Matrix.CreateRotationX(spaceship.leftrightrot);
//Matrix y = Matrix.CreateRotationY(spaceship.updownrot);
//Matrix rot = x * y;
Matrix[] transforms = new Matrix[Model.Bones.Count];
Model.CopyAbsoluteBoneTransformsTo(transforms);
Matrix worldMatrix = Matrix.CreateFromQuaternion(lazerRotation) * Matrix.CreateTranslation(Position);
foreach (ModelMesh mesh in Model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = worldMatrix * transforms[mesh.ParentBone.Index]; //position
effect.View = view; //camera
effect.Projection = projection; //2d to 3d
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;
}
mesh.Draw();
}
}请记住,我的宇宙飞船是子弹物体的一个领域,这就是我如何获得宇宙飞船的。旋转四元数。提前谢谢。
发布于 2011-04-30 09:08:25
连接effect.World矩阵的顺序应该与当前连接的顺序相反。这应该不会影响对象的形状,但会解决其他问题。应该是这样的:
effect.World = transforms[mesh.ParentBone.Index] * worldMatrix;XNA是一个行主框架,连接方式是从左到右。你的代码(从右到左)就是你在OpenGL中的方式,一个主要的专栏框架。
发布于 2011-04-30 03:56:58
我的理解是,根据你的代码,如果你的船改变方向,你的投射物在发射后会改变方向。
您需要创建一个射弹对象,并存储每个单独射弹的方向每个射弹的对象。实例化投射物时,将其方向设置为与船相同(如上所述)。但是,在该弹丸的整个剩余生命周期中,您不需要改变其方向。
例如:子弹1从A点向B点发射,然后船转向并从C点向D点发射子弹2。
子弹头1和子弹头2将各自具有唯一的向量,它们在上面移动。
https://stackoverflow.com/questions/5836111
复制相似问题