Hi there!
I’m a bit new to Java so please forgive me for what is probably a very straightforward problem!
I’ve posted code below so you can see what I’m getting at, but quite simply I’m trying to rotate a single triangle with time-based movement.
I can’t seem to convert the elapsed time from long to float and pass this into the required object without error. I feel like an idiot!
I’ve made a Timer class which gives me the elapsed time in millis as a long and casts to a float:
m_fElapsedTime = (float)( System.currentTimeMillis() - m_iLastLoopTime );
My game loop takes this float from my Timer object and passes it into my Renderer object. The game loop:
while ( bRunning )
{
float fElapsedTime = Timer.GetElapsedTime();
m_Timer.Begin();
m_Renderer.SetElapsedTime( fElapsedTime );
m_Canvas.display();
m_Timer.End();
}
The problem is, my float never seems to pass correctly from the Timer object to the Renderer object. The code to pass the elapsed time into Renderer is simple:
public class Renderer implements GLEventListener
{
// cut...
private float m_fElapsedTime;
public void SetElapsedTime( float f ) { m_fElapsedTime = f; }
//...cut
}
Is there a problem passing this value? If I were to do a System.out.println( m_fElapsedTime ) right there, it would show me the correct value 33 milliseconds.
Yet if I now go to use m_fElapsedTime in my triangle rotation code like this:
public class Renderer implements GLEventListener
{
// cut...
public void display( GLAutoDrawable drawable )
{
// cut...
m_fRotSpeed = 0.2f * m_fElapsedTime;
m_fAngle += m_fRotSpeed;
gl.glRotatef( m_fAngle, 0.0f, 1.0f, 0.0f );
// ...cut
}
it goes wonky. Printing out these values shows strange results:
m_fRotSpeed = 6.6 Correct!
m_fAngle = 2.44404437E11 Wrong!
Can anybody shed some light on what’s going on with m_fElapsedTime?
One last thing - how do you indent code on a forum post?
Many thanks,
Mulvey