Skip to main content
GameDev.net gamedev.net
🔒 Locked

How do you make objects spin?

Started by Elite19 Nov 23, 2003 at 10:43 AM 4 replies 900+ views
Original Post
Elite19
Elite19
Hi, I have made a simple model viewer with c# and mdx and I would like a button that makes whatever object is being displayed spin. Here is what I tried but it didn''t work //Globals static int iTime = Environment.TickCount % 1000; static float fAngle = iTime * (2.0f * (float)Math.PI) / 1000.0f; static Matrix ident = Matrix.Identity; static Matrix spin = Matrix.RotationY( fAngle ); Matrix crnt = ident; //Main loop static void Main() { using (WinForm frm = new WinForm()) { if (!frm.InitializeGraphics()) // Initialize Direct3D { MessageBox.Show("Could not initialize Direct3D. This tutorial will exit."); return; } frm.Show(); // While the form is still valid, render and process messages while(frm.Created) { frm.Render(); Application.DoEvents(); } } } The Render() meathod has a call to SetupMatrices() just after beginscene() //Setup matrices private void SetupMatrices() { device.Transform.World = crnt; device.Transform.View = Matrix.LookAtLH( new Vector3( 0.0f, 3.0f,-5.0f ), new Vector3( 0.0f, 0.0f, 0.0f ), new Vector3( 0.0f, 1.0f, 0.0f ) ); device.Transform.Projection = Matrix.PerspectiveFovLH( (float)Math.PI / 4, 1.0f, 1.0f, 100.0f ); } and finally the buttons event private void button1_Click(object sender, System.EventArgs e) { curnt = spin; } it compiles but the button does nothing. Any ideas? Thanks for this, it is greatly appreciated
--------------------------------------------------------------------- There are 10 types of people in this world, those that understand binary and those who don't.
Nik02
Nik02
curnt = spin;

Typo?

It's a good idea to keep strict declarations on, so that the compiler would warn you on writing to non-declared variables, such as this.

-Nik

[edited by - Nik02 on November 23, 2003 12:31:47 PM]
Niko Suni
Elite19
Elite19
??
Yeah, that was just a typo here but its not in my code and it still doesn''t work.
--------------------------------------------------------------------- There are 10 types of people in this world, those that understand binary and those who don't.
Nik02
Nik02
I believe that
"static float fAngle = iTime * (2.0f * (float)Math.PI) / 1000.0f;"
results in 0 everytime, because it is initialized in the beginning of the program.

Move it to the click handler, as well as the part where you construct the rotation matrix.

EDIT:
In fact you should move all the time-related code to the click handler, instead of the app initialization. Remember that expressions are evaluated when, and only when, they are called.

EDIT2:
Here's some sample code on how i would tackle this problem.
Note that this is not the definite solution, but it'll make the geometry spin until you click the button again


//globals

bool bSpin = false;
int iTime = 0;

static void Main()
{
using (WinForm frm = new WinForm())
{
if (!frm.InitializeGraphics()) // Initialize Direct3D

{
MessageBox.Show("Could not initialize Direct3D. This tutorial will exit.");
return;
}
frm.Show();

while(frm.Created)
{
frm.Render();
Application.DoEvents();
}
}
}

private void SetupMatrices()
{
if (bSpin) //if button has been clicked to indicate the desire to spin the geometry...

{
iTime = Environment.TickCount % 1000; //...update the time...

}

//...and make a rotation of that time unconditionally.

float fAngle = iTime * (2.0f * (float)Math.PI) / 1000.0f;
device.Transform.World = Matrix.RotationY( fAngle );

//then proceed as normal.

device.Transform.View = Matrix.LookAtLH( new Vector3( 0.0f, 3.0f,-5.0f ), new Vector3( 0.0f, 0.0f, 0.0f ), new Vector3( 0.0f, 1.0f, 0.0f ) );

device.Transform.Projection = Matrix.PerspectiveFovLH( (float)Math.PI / 4, 1.0f, 1.0f, 100.0f );
}

private void button1_Click(object sender, System.EventArgs e)
{
//crnt = spin;

bSpin != bSpin; //switch the spinning flag when button clicked


}


[edited by - Nik02 on November 24, 2003 7:38:42 AM]
Niko Suni
ms291052
ms291052
You may want fAngle to be gloabal so just declare it globally (non-static, of course) and then modify it first thing in SetupMatrices. That way, like Nik02 said, it will be updated every frame instead of staying at zero.
Elite19
Elite19
Cool, Thanks Nik!!!
Works nicely
Now I think I''m getting the hang of this,
Thanks again
--------------------------------------------------------------------- There are 10 types of people in this world, those that understand binary and those who don't.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.