How to handle floating point (and some other questions)

First I would like to ask if there is a guideline for asking multiple questions at once? Is it usually better to have a post per question so the results are easily find-able, or clump them together when there in the same category to not clutter the feed.

(for now I will clump them)

I just got done going through all the beginner/intermediate exercises and skimmed through the advanced section. I plan on rewriting my unity prototype of a 2D shooter into a more complete JME written game. My game requires that it be deterministic for some of the features I want to add (replay system using inputs instead of saving the locations etc…). The problem is that floating precision only has precision up to 7.22 decimal places.

In unity I scaled everything up by 10^7 and made them integers, then did all my math and logic in that space then scaled back down to give unity the floats it needs for rendering. It worked but was messy (atleast with using unity) especially when you had to square the numbers or take a cos etc… Now since I am using JME I was wondering if there is a better solution I could be missing? I know a lot of JME functions take a vector3f so just switching to something like a vector3BigDecimal would probably not be the best. Any ideas? Should I just stick with the original method of scaling up? If it helps to know, I will most likely end up implementing my own physics and collision detection/handling.

EDIT: I found the java keyword strictfp which in short insures consistent but less likely to be correct results. This might be a good way (would still need to figure out how to apply it to the already existing JME library), but still exploring options.

How easy is it to take a single-player JME game and make it multiplayer following the style guidelines in the wiki? I have heard people say you should start thinking about multiplayer towards the beginning of development, but as of right now I do not have a server to play with (I guess you can do it locally however) and I was thinking of having an offline version of the game anyway. would having a server-client like method for offline play would be the best like i mentioned before?

One per question usually.

As for deterministic:
Apart from the physics, all stuff in jme is deterministic.
I suggest to have your gamestate outside in own objects, then if you impelement them deterministic it will work, (and minimal floating point related rendering differences wont accumulate)
Usually it is a bad design to use spatials/etc for your game state, try to seperate rendering and logic more.

Multiplayer is not trivial, however doing it after you wrote it a single player game is worse in my opinion. So it might make sense to directly write a local server for the moment.

Thanks for your response! I think I am misunderstanding a little bit. It makes sense to separate my rendering and logic more than what I did in my prototype, but what about the floating point error on the logic side (ignoring rendering)? say I am making a rectangular hitbox, would you recommend representing the width as a scaled integer / BigDecimal / float (with or without strictfp) / double or maybe some other method?

mhh Bullet is deterministic, isn’t it?

Bullet is only deterministic if you use fixed timestep, but yes than it is.
However there might be some differences between the different os compilations, (aka different compilers ect)

What I meant, make your own logic deterministic.
Eg you could define 100integer = 1meter and only convert back to float for interaction with the scenegraph.
That way you eleminate the whole float problematic out of your logic.

Ok that is what I thought, much more clear now. I think I will go with a Integer format as well, there is a chance I might use floats and use strictfp that way I dont have to worry about getting a float (and having to reformat it) if say I want to rotate the hitbox and need to call cos or sin etc… But I need to do slightly more research. Thank you!

Can you make bullet physics work in 2D? im assuming there is probably a work around but most likely inefficient. I wouldnt want to do that for this game but I am curious :stuck_out_tongue:

You can, but as you say, it’s a bit hackish.
Some contributors use other physic engine for 2D, I don’t recall the name exactly but maybe @ndebruyn can have more informations about it.

I think Dyn4j and Box2D are the big ones. I have direct experience with dyn4j and it’s just great. I highly recommend it for 2D physics.

1 Like

So if anyone is interested or wants to do a quick sanity check before I go along with this. I think the least hassle solution right now would be to represent my game logic/2D colliders in floats using strictfp and java.lang.StrictMath since strictfp does not cascade to out of scope calcualtions. Im not completely sure, but I think I can get by but what is supplied there to make basic 2D shapes, apply a version of 2D physics, detect collisions and handle them.

StrictMath (Java Platform SE 7 ).