Note that there are some explanatory texts on larger screens.

plurals
  1. PORotating cubes in XNA
    primarykey
    data
    text
    <p>So far I have managed to create a <code>Quaternion</code> for rotation. Only problem is now, how do I apply it to only certain cubes? As when I press the right key on the keyboard at the moment, every cube is being rotated continiously around the origin. </p> <p>For reference, I have 8 cubes positioned in a similar set-up to that of a Rubik's Cube (2x2x2). So when I press the right/left arrow, the right/left face of the 'Cube' (big cube made up of the 8 smaller cubes), rotates 90 degrees clockwise/anti-clockwise.</p> <p>An example of one of the Cubes (out of eight cubes in total) declaration:</p> <pre><code>GameObject subCube3 = new GameObject(); Vector3 subCube3Pos = new Vector3(-0.55f, -0.55f, 0.55f); </code></pre> <p>In my update method:</p> <pre><code>// declare rotation floats float updownRotation = 0.0f; float leftrightRot = 0.0f; // get state of keyboard KeyboardState keys = Keyboard.GetState(); // if key is pressed, change value of rotation if (keys.IsKeyDown(Keys.Right)) { leftrightRot = -0.10f; } // if key is pressed, change value of rotation if (keys.IsKeyDown(Keys.Left)) { leftrightRot = 0.1f; } // if key is pressed, change value of rotation if (keys.IsKeyDown(Keys.Up)) { updownRotation = 0.1f; } // rotation around axis Quaternion addRot = Quaternion.CreateFromAxisAngle(new Vector3(1.0f, 0.0f, 0.0f), leftrightRot); //rotation of cubes cubeRotation = cubeRotation * addRot; </code></pre> <p>My draw function:</p> <pre><code>void DrawGameObject(GameObject gameobject) { //graphics.GraphicsDevice.RenderState.CullMode = CullMode.None; foreach (ModelMesh mesh in gameobject.model.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.PreferPerPixelLighting = true; effect.World = Matrix.CreateScale(gameobject.scale) * Matrix.CreateFromQuaternion(cubeRotation) * Matrix.CreateTranslation(gameobject.position); effect.Projection = cameraProjectionMatrix; effect.View = cameraViewMatrix; } mesh.Draw(); } } </code></pre> <p>What I think is the problem is that <code>Matrix.CreateTranslation(gameobject.position)</code> is obviously affecting all my cubes. I've tried creating new <code>Vector3</code> i.e: <code>c_component1 = Vector3.Transform(cube1pos, cubeRotation);</code> but even then, I am unsure where to put that and actually use it.</p> <p>Any ideas anyone? Any help will be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload