Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Parameter Passing of a Float  (Read 2418 times)
0 Members and 1 Guest are viewing this topic.
Offline mulvey

Junior Newbie





« Posted 2008-09-21 20:59:20 »

Hi there!  Smiley

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:

1  
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:

1  
2  
3  
4  
5  
6  
7  
8  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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:

1  
2  
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
Offline Jackal von ÖRF

Junior Member





« Reply #1 - Posted 2008-09-21 21:13:26 »

1. What is the value of m_fAngle before you add m_fRotSpeed to it? I believe it is 2.44404437E11 minus that 6.6 (which probably the same as 2.44404437E11 because of floating point inaccuracy). So the problem is not in m_fElapsedTime but in the original value of m_fAngle.

2. The value of m_fAngle is not used. You pass m_fAngle1 as a parameter to glRotatef(), not m_fAngle.

Offline mulvey

Junior Newbie





« Reply #2 - Posted 2008-09-21 21:15:14 »

Apologies, it's the same angle variable in the code.

The value of m_fAngle is initialised to 0.0f at the Renderer creation.

Edited my post!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jackal von ÖRF

Junior Member





« Reply #3 - Posted 2008-09-21 21:17:09 »

Post more of your code. Especially all methods where m_fAngle is modified.

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #4 - Posted 2008-09-22 01:59:36 »

Also, why not just do this:

1  
2  
3  
4  
5  
6  
public void update()
{
     triangleRot += 0.1f;
     if (triangleRot >= 2*Math.PI)
          triangleRot = 0;
}


It seems overly complicated to use the timer.

See my work:
OTC Software
Offline mulvey

Junior Newbie





« Reply #5 - Posted 2008-09-22 11:24:16 »

The triangle rotates just fine when I increment it with a local value, but the problem was that I couldn't seem to pass in an external value without rotation error. I've been working on my timer, and I want to be able to use it!

Thanks to Jackal's excellent suggestion, I've watched the value of m_fAngle before and after it is modified and I now suspect my problem is a threading issue.

What I was hoping for:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
Applet init.
Applet start.
OpenGL init.
INIT GL IS: com.sun.opengl.impl.GLImpl
OpenGL reshape.
=== Main Game Count 0 ===
Game loop elapsed time value = 0.0
=== Renderer Count 0 ===
Renderer elapsed time value = 0.0
Angle before = 0.0
Angle after = 0.0
=== Main Game Count 1 ===
Game loop elapsed time value = 33.0
=== Renderer Count 1 ===
Renderer elapsed time value = 33.0
Angle before = 0.0
Angle after = 6.6
..etc...


All nice and in sequential order.

But here's what I get!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
Applet init.
=== Main Game Count 0 ===
Game loop elapsed time value = 0.0
Applet start.
OpenGL init.
INIT GL IS: com.sun.opengl.impl.GLImpl
OpenGL reshape.
OpenGL reshape.
=== Renderer Count 0 ===
Renderer elapsed time value = 0.0
Angle before = 0.0
Angle after = 0.0
=== Renderer Count 1 ===
Renderer elapsed time value = 0.0
Angle before = 0.0
Angle after = 0.0
=== Main Game Count 1 ===
Game loop elapsed time value = 1.22207535E12
=== Renderer Count 2 ===
Renderer elapsed time value = 1.22207535E12
Angle before = 0.0
Angle after = 2.4441507E11
=== Main Game Count 2 ===
Game loop elapsed time value = 5.0
=== Renderer Count 3 ===
Renderer elapsed time value = 5.0
Angle before = 2.4441507E11
Angle after = 2.4441507E11
=== Main Game Count 3 ===
Game loop elapsed time value = 34.0
=== Renderer Count 4 ===
Renderer elapsed time value = 34.0
Angle before = 2.4441507E11
Angle after = 2.4441507E11
=== Main Game Count 4 ===
Game loop elapsed time value = 32.0


I think I need to read more JOGL and applet info, particularly with regards to implementing your own render loop properly...
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #6 - Posted 2008-09-22 15:52:38 »

Also, why not just do this:

1  
2  
3  
4  
5  
6  
public void update()
{
     triangleRot += 0.1f;
     if (triangleRot >= 2*Math.PI)
          triangleRot = 0;
}


It seems overly complicated to use the timer.

You're leaking rotation.

1  
2  
3  
4  
5  
6  
public void update()
{
     triangleRot += 0.1f;
     if (triangleRot >= 2*Math.PI)
          triangleRot -= 2*Math.PI;
}

Play Minecraft!
Offline DzzD
« Reply #7 - Posted 2008-09-22 16:02:47 »

ouch, never make a sum with float !
Quote
triangleRot += 0.1f;


try that to see why (only 10 iteration) :
1  
2  
3  
4  
5  
6  
float n=0.0f;
for (int x=0;x<10;x++)
{
 n+=0.1f;
 System.out.println("n="+n);
}


or rather (10000 iterations):

1  
2  
3  
4  
5  
6  
7  
float n=0.0f;
for (int x=0;x<10000;x++)
{
 n+=0.1f;
 
}
System.out.println("n="+n);  


n=999.9029

nb: addition and substraction are not really good with float they are more intended for multiplication and division.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 742
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2008-09-22 16:57:40 »

Just recently I made a similar mistake...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
int max = 500;
int[] values = new int[max];

void dummy(float select)
{
   if(select >= max)
      select = max - 0.001f;

   int index = (int)select; // potentially index==max
  float fract = select - index;

   int value = values[index]; // potential AIOOBE
}



But well, floats are not really specially intended for multiplication, think about DOT and matrix transformations... lots of ADD and MUL.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ewjordan

Junior Member





« Reply #9 - Posted 2008-09-22 17:51:55 »

@mulvey: Something funky is happening with your timer - I'm guessing it's that in the first time through the loop your base time (m_iLastLoopTime) is set at zero, right?  System.currentTimeMillis() gives me 1.22209855E12 at the moment, which tells me that's where that bizarre timing value came from (see the log you posted).  Make sure to initialize m_iLastLoopTime to System.currentTimeMillis() when you start out, then you should get more reasonable deltas and hopefully the rest of your code will work right.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline mulvey

Junior Newbie





« Reply #10 - Posted 2008-09-22 23:12:17 »

@mulvey: Something funky is happening with your timer - I'm guessing it's that in the first time through the loop your base time (m_iLastLoopTime) is set at zero, right?  System.currentTimeMillis() gives me 1.22209855E12 at the moment, which tells me that's where that bizarre timing value came from (see the log you posted).  Make sure to initialize m_iLastLoopTime to System.currentTimeMillis() when you start out, then you should get more reasonable deltas and hopefully the rest of your code will work right.

That was the problem all along - well spotted, I can't believe I missed that!

 Smiley
Offline DzzD
« Reply #11 - Posted 2008-09-23 22:17:37 »

Quote
Just recently I made a similar mistake...
I remender some years ago, at work they found that financial value had some error on an application they made after some monthes in production!, one of my work knowledge have used float for doing sum on money value.. they was stupefact when they see how float can be wrong after severals operations. float stay accurate for a little number of ops.

the best way to minimize that is to combine mathematical ops in a big one to reduce numbr of operations, and also to use int when dealing with integer.

short exemple for the above function :

1  
2  
3  
4  
5  
6  
public void update()
{
     triangleRot += 0.1f;
     if (triangleRot >= 2*Math.PI)
          triangleRot = 0;
}


can be coded like below to reduce errors:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
int step;
public void update()
{
     step++;
     triangleRot = step*0.1f;
     if (triangleRot >= 2*Math.PI)
    {
          step=0;
          triangleRot = 0f;
    }
}



Quote
But well, floats are not really specially intended for multiplication, think about DOT and matrix transformations... lots of ADD and MUL.

yup  Sad  float introduce a lot of bug (especially in collision detection....), this is why matrix should be combined when possible rather than performed sequentially.

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #12 - Posted 2008-09-24 03:41:09 »

You're leaking rotation.

1  
2  
3  
4  
5  
6  
public void update()
{
     triangleRot += 0.1f;
     if (triangleRot >= 2*Math.PI)
          triangleRot -= 2*Math.PI;
}

True. I was just whipping up an example. Also, I usually force my programs to disallow negative rotations so that everything is 0 <= angle <= 2*Pi. I find it easier to follow that way.

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (12 views)
2014-07-24 01:59:36

Riven (10 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (42 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!