Posted: 16th Aug 2011 22:18
Something with SetPhysicsScale and StepPhysics is not adding up.

I am assuming that Box2D is defaulting to 10 m/s^2 for the y acceleration. If this is the case, then an object released from rest will fall 5 m.

I setup a small program to do just that.

+ Code Snippet
setvirtualresolution(480,320)

loadimage(1, "ball.jpg")

createsprite(1, 1)
setspriteposition(1,100,0)
setspritephysicson(1, 2)

time = timer()
do
    print(timer() - time)
    print(getspritey(1))
    sync()
    if (timer() - time) >= 1
        repeat
        until getpointerpressed() = 1
    endif
loop

end


According to the program, the ball falls around 15 pixels. If 5 m corresponds to 15 pixels, then the default scale is 0.333 m/pixel. According to the help file for SetPhysicsScale, the default is 0.2.

Since the physics is done numerically, I thought it may be a problem with the step size. I called StepPhysics with progressively smaller values. Two things should have occured. 1) If the step size was too large to begin with, the result of the calculation should change and then converge to a new value. 2) The simulation should take longer and longer to run. I didn't observe either of these.

There seems to be multiple inconsistencies. Can anyone resolve these?
Posted: 16th Aug 2011 22:20
5 meters in 1 second
Posted: 16th Aug 2011 23:37
+ Code Snippet
time = timer()


is casting a float to an int which may be throwing off the timing. I get 25 pixels which gives a scale of 0.2
Posted: 17th Aug 2011 0:40
Thanks Paul. You are absolutely right about the casting problem. However, a problem still exists. I now get 35 pixels which gives a scale of 0.14. We run the same program but we get different results??? Do you have any suggestions for resolving this?

+ Code Snippet
setvirtualresolution(480,320)

loadimage(1, "car.jpg")

createsprite(1, 1)
setspriteposition(1,100,0)
setspritephysicson(1, 2)

time# = timer()
do
    print(timer() - time#)
    print(getspritey(1))
    sync()
    if (timer() - time#) >= 1
        repeat
        until getpointerpressed() = 1
    endif
loop

end
Posted: 17th Aug 2011 3:17
It may be the initial physics step is not using a well defined interval. Normally it goes off the time spent rendering the last frame, which doesn't exist for the first frame.

Try adding

+ Code Snippet
setvirtualresolution(480,320)
sync()
sync()


at the start of your code to get the simulation started maybe that will be more reliable.
Posted: 17th Aug 2011 3:55
Thanks for following up on this Paul. I'll try your suggestion.

I also had success adding SetPhysicsScale and SetPhysicsGravity at the VERY start of the program.

1) I started with a scale of 0.2 meters/pixel (even though it supposed to be default) and a y acceleration of 50 pixels/second^2 (10 meters/second^2 - even though it supposed to be default). After 1 s, y should be approximately 5 m. I got 25 pixels which is consistent with the scale.

2) I then tried a scale of 0.05 meters/pixel and a y acceleration of 200 pixels/second^2 (10 meters/second^2). After 1 s, y should be approximately 5 m. I got 95 pixels which is consistent with the scale.

Anyway, it seems reproducible so I'm happy.