Thursday, 15 January 2015

c# - How rotate a 3D cube at its center XNA? -



c# - How rotate a 3D cube at its center XNA? -

i seek rotate 3d cube on center, not edge. here code used.

public rotatemycube() { ... matrix newtransform = matrix.createscale(scale) * matrix.createrotationy(rotationloot) * matrix.createtranslation(translation); my3dcube.transform = newtransform; .... public void updaterotatecube() { rotationloot += 0.01f; }

my cube rotate fine, not center. here schematic explains problem.

and need this:

my finish code

private void updatematricecubetorotate() { foreach (list<instancedmodel> listinstance in liststructureinstance) { foreach (instancedmodel instanceloot in listinstance) { if (my3dcube.isaloot) { vector3 scale; quaternion rotation; vector3 translation; //i position, rotation, scale of cube my3dcube.transform.decompose(out scale,out rotation,out translation); var rotationcenter = new vector3(0.1f, 0.1f, 0.1f); //create new transformation new rotation matrix transformation = matrix.createtranslation(- rotationcenter) * matrix.createscale(scale) * matrix.createrotationy(rotationloot) * matrix.createtranslation( translation); my3dcube.transform = transformation; } } } //incremente rotation rotationloot += 0.05f; }

a rotation matrix rotates vertices around origin of coordinate system. in order rotate around point have create origin. can done subtracting rotation point every vertex in shape.

var rotationcenter = new vector3(0.5f, 0.5f, 0.5f); matrix transformation = matrix.createtranslation(-rotationcenter) * matrix.createscale(scaling) * matrix.createrotationy(rotation) * matrix.createtranslation(position);

c# matrix xna rotation transform

No comments:

Post a Comment