XNA模型变长了?
清晨曦月 元老
博客专家认证 2012-01-19 05:14:42 是酱紫滴:
圆柱体底面中心(0,0,0),顶面中心(0,0,1),半径0.5。我在3DS MAX里面用10个半径0.5,高度0.1的圆柱体垒起来(轴都对齐到原点)。然后放到场景里,并且旋转120。这时候问题来了:
Dim transforms = New Matrix(model.Bones.Count - 1) {}
model.CopyAbsoluteBoneTransformsTo(transforms)
For Each mesh As ModelMesh In model.Meshes
For Each be As BasicEffect In mesh.Effects
be.EnableDefaultLighting()
be.Projection = camera.projection
be.View = camera.view
be.World = transforms(mesh.ParentBone.Index) * _World
Next
mesh.Draw()
Next
创建的摄影机:
view = Matrix.CreateLookAt(New Vector3(0, 0, 10), Vector3.Zero, Vector3.Up)
红色那句,应该就是根源了。这样写的话,不经任何旋转缩放操作的时候,圆柱体用上面函数显示出来竟然底面中心(0,0,0),顶面中心(0,1,0),本来沿着Z轴的,怎么就沿着Y轴了?
当我旋转的时候还没其他问题,但是当我进行pick的时候,竟然发现本应该在(0,0,1)那里的那个BoundingSphere的中心跑到了(0,0,2)那里去,难道我的圆柱体变长了?!
很奇怪的问题,反复尝试很多次,发现只有红色那句像现在这么写还将就着能旋转,但我还要在柱头上加其他模型,还要平移操作,总不能不知所谓的就*2啊。。
求解!
注:PICK的代码用的XNA网站上的示例。红色那行写法也一致啊。。我的_World是这样计算的:
_World = _ScaleWorld * _RotationWorld1 * _TranslationWorld * _RotationWorld2
第一个是缩放操作,第二个是类似自转的旋转,第三个是平移,第四个是类似公转的旋转。感觉没什么问题,把代码贴上:
Protected Friend Overridable Sub Translation(offset As Vector3)
_TranslationWorld *= Matrix.CreateTranslation(offset)
_World = _ScaleWorld * _RotationWorld1 * _TranslationWorld * _RotationWorld2
End Sub
Protected Friend Overridable Sub Rotation1(v3 As Vector3, angle As Single)
_RotationWorld1 *= Matrix.CreateFromAxisAngle(v3, angle)
_World = _ScaleWorld * _RotationWorld1 * _TranslationWorld * _RotationWorld2
End Sub
Protected Friend Overridable Sub Rotation2(v3 As Vector3, angle As Single)
_RotationWorld2 *= Matrix.CreateFromAxisAngle(v3, angle)
_World = _ScaleWorld * _RotationWorld1 * _TranslationWorld * _RotationWorld2
End Sub
Protected Friend Overridable Sub Scale(i As Single)
_ScaleWorld *= Matrix.CreateScale(i)
_World = _ScaleWorld * _RotationWorld1 * _TranslationWorld * _RotationWorld2
End Sub