Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / physics run different every time

Author
Message
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 1st Jul 2013 20:08
Hi, I have been working on a level editor for a while now, taking a break from it now and then Anyhow I was just wondering why a physics scenario, the same every time,runs differently,objects land and halt at different positions each run of the simulation.To show this I include a video HERE of the latest rewrite of the editor,running the same physics simulation a few times and observing the end scenario.

Hail to the king, baby!

http://davidjohnwheeler.blogspot.co.uk
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 1st Jul 2013 20:18 Edited at: 1st Jul 2013 20:23
it used the frame time. you can use a constant time with
stepphysics command. different time steps results in variance
with this physics engine.

with the last agk beta my frametime jumps 5 fps or more around and i don't know why, its looks like an impact somewhere.
i closed all background task for compare before.
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 1st Jul 2013 22:38
ah,thanks.

Hail to the king, baby!

http://davidjohnwheeler.blogspot.co.uk
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 1st Jul 2013 22:54
hmmm! I have tried using StepPhysics(0.02) before the sync() in the main loop, but this still yields different results each run! is this the only command I must call?! changing the values simply makes the physics faster/slower respectively.

Hail to the king, baby!

http://davidjohnwheeler.blogspot.co.uk
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 1st Jul 2013 23:21
hmmm,curios.
i don't know.
lilpissywilly
AGK Developer
13
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 1st Jul 2013 23:55
Quote: "hmmm! I have tried using StepPhysics(0.02) before the sync() in the main loop, but this still yields different results each run! is this the only command I must call?! changing the values simply makes the physics faster/slower respectively."


This would still make it dependent on FPS.

Anyway, I don't understand the problem. Try setting up some cans and balls in the real world similar to your program and see if the balls and cans end up in the same position every time your let them topple (they won't, don't try it). The whole point of a physics system is that it's physics, not scripted.


The built in box2d actually runs very well at different FPS but if you want to control the stepPhysics yourself there are threads here that discuss this.

My hovercraft is full of eels
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 2nd Jul 2013 09:37
i expect the same result with the same beginning conditions.
easter bunny
11
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 2nd Jul 2013 10:09
instead of calling Sync() at all, try this:
Update(0.02)
Render()
Swap()

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 2nd Jul 2013 11:08
You really shouldn't use stepPhysics unless you have to. I only use it to pause physics IE. stepPhysics(0)

If you could post an example of the problem in code I'm pretty sure I can help you solve the problem. If you are intent on using stepPhysics try using it like this (it should be "better"):

stepPhysics(getFrameTime())

Or if you are 100% certain that your framerate will be at a certain speed like 60fps then:

stepPhysics(0.0166666666666667) rem 1/60


this.mess = abs(sin(times#))
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 2nd Jul 2013 11:18
@baxslash
the problem was he want see the same result each run
and i thought a constant time step results in the same state/positional.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 2nd Jul 2013 11:20
I have found that when I have changed the time step manually it has resulted in less predictable behaviour, not sure why.

It would help if there was an example to test though.


this.mess = abs(sin(times#))
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 2nd Jul 2013 12:01
Try forcing a fixed fps with setsyncrate(60,1). This uses more cpu but iirc the only way to do it.

Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 2nd Jul 2013 22:04
Box2D is not supporting a strict deterministic behavior. It can vary depending on the device, and from time to time when you run it. It has to do with float number handling by the processor/system. You need a fixed point math physics system to get the same result every time with the same setup.

Does it get the same result every time you start the program, but not when you reset the simulation?

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 2nd Jul 2013 23:18
ah sorry, been a bit busy. I will see the results on every start of the program for you.. I had a slight hunch it could be to do with float, the whole reason I was inquiring is that I was going to do some
cannon knocking down stuff type game, but really the outcome of the physics sim needs to be the same every time else obviously some levels could be impossible!

Hail to the king, baby!

http://davidjohnwheeler.blogspot.co.uk
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 3rd Jul 2013 00:08
Ok, it seems the same every run, just changes every reset of the sim!
I would show some code but it ain't easy to do with it being an editor an all! but here is all the code anyhow...



Hail to the king, baby!

http://davidjohnwheeler.blogspot.co.uk
Ranietz
AGK Gold Backer
19
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 3rd Jul 2013 00:22
How do you reset the simulation? I could't find it in the code. Do you just re-position every sprite? If so, do you set the velocity of each sprite back to zero?
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 3rd Jul 2013 00:26 Edited at: 3rd Jul 2013 00:27
just press F1 to reload,but perhaps you have a point about the velocity!

Hail to the king, baby!

http://davidjohnwheeler.blogspot.co.uk
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 3rd Jul 2013 00:36 Edited at: 3rd Jul 2013 00:50
nope..doesn't matter if I reset the velocity to 0, though I was deleting sprites anyhow, I have this funky sprite recycle system going on!
*** edit *** included is a download to the whole project, for those curious or if anyone can benefit from it, you can resize with w-a-s-d
.. save with F2, load with F1, change physics type with m, change phyics shape with n, scroll through textures using [],rotate with r, reset rotate with v..run physics simulation withp... and finally clear level with c! you cannot delete a sprite yet, needs adding..

Hail to the king, baby!

http://davidjohnwheeler.blogspot.co.uk

Attachments

Login to view attachments
Ranietz
AGK Gold Backer
19
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 3rd Jul 2013 00:48
If you delete and recreate the sprites when you reset the simulation you should not have to set the velocity to zero. I've only looked at your code using my phone so far but I'll see if I can come up with some other suggestions once I get the time to try it out.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 3rd Jul 2013 00:49
this part i found strange.
you turn n on but outside is a r loop
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 3rd Jul 2013 00:54 Edited at: 3rd Jul 2013 00:58
i have the ability to simply make more than one layer of sprites at the same coordinates (up to 10) hence the line:

near the top, so each layer could have 5000 sprites, times 10 layers, of any sprite size.
but note this isnt a finished project,but mostly done, i will release the whole lot for free as i have said,when i am happy with it

Hail to the king, baby!

http://davidjohnwheeler.blogspot.co.uk
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 3rd Jul 2013 01:28
i just wonder why you use n and not tile[n,r].spritenumber there.
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 3rd Jul 2013 01:39
I did have it the other way around, the next r after the next n, but it wouldn't work oddly enough, however all seems to work ok on saving and loading!

Hail to the king, baby!

http://davidjohnwheeler.blogspot.co.uk
Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 3rd Jul 2013 11:38
Even if you find a way to completely reset the system between simulations, so that it runs the same on one device, it will behave different on a different device. So unless you make the program for one type of device only, it will not behave the same. You just need to design your levels with a bit of room for random physics behavior, so that they will be playable on all devices. Maybe make a "I'm stuck" cheat button, or a "Reset level and try again" button.

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
Ranietz
AGK Gold Backer
19
Years of Service
User Offline
Joined: 2nd Sep 2004
Location: Norway
Posted: 3rd Jul 2013 12:46
I did some testing on my own program and it seems like things will behave a little different each time. I guess it's because the frame rate is not constant.

However, I found a trick that may help you. Try to update the physics several times each loop but in smaller increments.
Replace:

With:


I assume it will use more CPU power so I'm not sure if it's a good solution though...
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 3rd Jul 2013 13:41
i make a test program for it that saves a image after some seconds and
later you can switch with windows photo view through.

Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 3rd Jul 2013 13:43
Box2D does not use any random number, or numbers based on timers, it's just the floats that messes it all up. almost like the butterfly effect. A small rounding "error" and all changes.

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Jul 2013 13:48
I guess like physics in real life it's hard to account for cumulative errors and get an exact copy each time. Maybe it's the tiniest difference that changes it...

I guess if you need 100% the same each time you may not achieve it using box2D. Maybe you should calculate the positioning yourself? I don't know how complicated your game is but it might be safer if that's the effect you want.

It's not impossible to calculate normals / reflection angles etc. using maths (for circular objects at least). Might be worth considering?


this.mess = abs(sin(times#))
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 3rd Jul 2013 14:09
i made a other test with colors.
(i don't think a cpu multiply two values each time with different results. i know the cause and effect...)

Impetus73
12
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 3rd Jul 2013 15:09
Box2D uses Gauss-Seidel to approximately solve constraints. Box2D also uses Semi-implicit Euler to approximately solve the differential equations. Box2D also does not have exact collision. Polygons are covered with a thin skin (around 0.5cm thick) to avoid numerical problems. This can sometimes lead to unexpected contact normals. Also, some shapes may begin to overlap and then be pushed apart by the solver.

Any sufficient advanced program, is indistinguishable from magic... :-P

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
shadey
14
Years of Service
User Offline
Joined: 25th Jan 2010
Location:
Posted: 3rd Jul 2013 19:56 Edited at: 4th Jul 2013 00:33
so,if this is the case, then maybe you cannot possibly do an angry birds type game with this physics( we all know that game so I choose this as an example!)
because that game would need exact results as I have found in some levels, you have to take the exact shot with exact everything! But perhaps if the 'skin' as you say is 0.5 thick. I should position the objects with this gap?
Ranietz:
Tried your solution, it improves it but still minor differences,better than before though,I will think up a rather cool simulation set up to test it more

Hail to the king, baby!

http://davidjohnwheeler.blogspot.co.uk

Login to post a reply

Server time is: 2024-05-04 02:40:28
Your offset time is: 2024-05-04 02:40:28