Making the game speed faster

Hi all,

I have been using Corona for a few weeks now and except for not being able to build iOS apps on Windows (there could have been a cloud build option for example), I really enjoyed it and I hope I will be able to release my first game soon. 

Anyway, here is my first question on these forums. How can I make the game look faster? I’ve tried playing around with physics.setScale. I have increased the scale and of course the object’s density accordingly, which made the game a lot faster as expected.

However, with this I have one major issue, because my objects are very fast, collision events are triggered way too late.

For instance, I have platforms that can be passed through when the character is below the platform, but not when he’s above. I’ve implemented this the regular way with isSensor. The problem is that after increasing the speed, my collision listener that should set isSensor = false if the character is higher than the platform is triggered much later after my character has already fallen through the platform…

Is there anyway I can make the game faster but having collision event fire in time?

Thanks!

Do you have some code to show us? That would help.

As for building iOS apps on Windows, you can blame that on Apple, which restricts development of its apps to a Mac. It has nothing to do with Corona.  

Hi James,

For iOS apps, I think you didn’t get my point, I know you need xcode, but I believe some competitors of Corona offer cloud builds.

As for the main question, it would be a bit annoying to post my code, so I built a small demo that reproduces exactly my issue.

[lua]local physics = require(“physics”)
physics.start()

physics.setGravity(0,9.8)
local jumpPlatform = display.newRect( 120, 250, 100, 5 )
local ball = display.newCircle(150,350,10)

---- case 1: ball goes through platform when going up, doesn’t go through when going down. AS EXPECTED
physics.setScale(60)
physics.addBody( ball, “dynamic”, { friction=0.2,density = 1.2} )

---- case 2: ball goes through platform when going up, go through when going doing as it seems that by the time isSensor is set to false, the ball already went through…
– physics.setScale(300)
– physics.addBody( ball, “dynamic”, { friction=0.2,density = 20} )

---- case 3: collision event is not even fired when going down…
– physics.setScale(300)
– physics.addBody( ball, “dynamic”, { friction=0.2,density = 40} )

physics.addBody( jumpPlatform, “static”, { friction=0.3,isSensor = true} )
function onFloorCollision(self,event )

vx, vy = ball:getLinearVelocity()

if vy > 0 then – going down
print (“collision while going down”)
self:setFillColor( 1,0,0 )
self.isSensor=false
else
print (“collision while going up”)
end
end

jumpPlatform.collision = onFloorCollision
jumpPlatform:addEventListener( “collision”, jumpPlatform )

ball:applyLinearImpulse(0, -1, ball.x, ball.y)[/lua]

There are 3 cases to comment/uncomment. Each case has a pretty clear description in comment.

Case 1 as expected.

Case 2 kind of annoying because we sent sensor to false but it seems to late.

Case 3 event not even triggered, wtf

Thanks!

Hmm I’m still stuck with that issue. I tried using the property isBullet but it doesn’t change anything.

Strangely, while searching I stumbled across this post http://forums.coronalabs.com/topic/51661-collisions-on-occasion-seem-to-be-missed-using-latest-build-2393/ and it seems that he faced the opposite (ie: collisions only working properly with a high setScale).

Thanks

edit: btw, maybe this is not a newbie enough question, should I repost in the physics sub forum?

Here’s your problem:

if vy \> 0 then -- going down

This actually tests “going up”. Change it to this:

if vy \< 0 then -- going down

Hi James,

Sorry but from my understanding, y = 0 is located at the top of the screen, shouldn’t a positive vy hence indicate going down?

Also, if I make the change you are suggesting, then in case 1 the ball doesn’t go through when going up, which confirms my understanding.

Thanks

Hi @endygwa,

There are 3 things which you can do immediately to improve collision sensitivity on small objects which may be moving very fast:

  1. Make the moving object a “bullet” (.isBullet=true) after you add the physics body on it (doing it before won’t have any affect since it won’t be considered a physical object until you add a body to it).

  2. Increase the position and velocity iteration settings of the physics engine:

http://docs.coronalabs.com/api/library/physics/setPositionIterations.html

http://docs.coronalabs.com/api/library/physics/setVelocityIterations.html

  1. Set your core project FPS to 60 instead of 30 (see the guide here).

Of course, you should note all of the “gotchas” about performance penalties and so forth when you increase the Box2D settings beyond their defaults. Normally it shouldn’t have a noticeable impact, but you should use them carefully.

On another note, is your ball and/or platform especially small/thin? Is there a chance that the ball is “skipping through” the platform entirely, as in, on one time step of the app it’s above, then because of a high speed as it drops, it actually skips entirely through the platform (to underneath it) without actually registering a collision with the platform?

Best regards,

Brent

Hi Brent,

I had already tried 1) and it didn’t change a thing. I had also already tried 3) and in the code I posted above, it makes case 3 equivalent to case 2 (ie: by the time isSensor is set to false, the ball already went through. but at least the event is fired so I guess it already helps a bit.

I will try 2) and let you know if it helps, I didn’t know about these two settings, thanks!

As for the ball being thin, it’s the case in the code I posted above but not in my game, and I encounter the same behavior. But shouldn’t isBullet=true do the trick for very small objects thanks to continuous collision detection? 

Thanks

endy

Hmm it did not help, but it seems that I still don’t fully understand something about physics and collisions, I’ll open a new thread with sample code in the physics subforum. 

Thanks

Do you have some code to show us? That would help.

As for building iOS apps on Windows, you can blame that on Apple, which restricts development of its apps to a Mac. It has nothing to do with Corona.  

Hi James,

For iOS apps, I think you didn’t get my point, I know you need xcode, but I believe some competitors of Corona offer cloud builds.

As for the main question, it would be a bit annoying to post my code, so I built a small demo that reproduces exactly my issue.

[lua]local physics = require(“physics”)
physics.start()

physics.setGravity(0,9.8)
local jumpPlatform = display.newRect( 120, 250, 100, 5 )
local ball = display.newCircle(150,350,10)

---- case 1: ball goes through platform when going up, doesn’t go through when going down. AS EXPECTED
physics.setScale(60)
physics.addBody( ball, “dynamic”, { friction=0.2,density = 1.2} )

---- case 2: ball goes through platform when going up, go through when going doing as it seems that by the time isSensor is set to false, the ball already went through…
– physics.setScale(300)
– physics.addBody( ball, “dynamic”, { friction=0.2,density = 20} )

---- case 3: collision event is not even fired when going down…
– physics.setScale(300)
– physics.addBody( ball, “dynamic”, { friction=0.2,density = 40} )

physics.addBody( jumpPlatform, “static”, { friction=0.3,isSensor = true} )
function onFloorCollision(self,event )

vx, vy = ball:getLinearVelocity()

if vy > 0 then – going down
print (“collision while going down”)
self:setFillColor( 1,0,0 )
self.isSensor=false
else
print (“collision while going up”)
end
end

jumpPlatform.collision = onFloorCollision
jumpPlatform:addEventListener( “collision”, jumpPlatform )

ball:applyLinearImpulse(0, -1, ball.x, ball.y)[/lua]

There are 3 cases to comment/uncomment. Each case has a pretty clear description in comment.

Case 1 as expected.

Case 2 kind of annoying because we sent sensor to false but it seems to late.

Case 3 event not even triggered, wtf

Thanks!

Hmm I’m still stuck with that issue. I tried using the property isBullet but it doesn’t change anything.

Strangely, while searching I stumbled across this post http://forums.coronalabs.com/topic/51661-collisions-on-occasion-seem-to-be-missed-using-latest-build-2393/ and it seems that he faced the opposite (ie: collisions only working properly with a high setScale).

Thanks

edit: btw, maybe this is not a newbie enough question, should I repost in the physics sub forum?

Here’s your problem:

if vy \> 0 then -- going down

This actually tests “going up”. Change it to this:

if vy \< 0 then -- going down

Hi James,

Sorry but from my understanding, y = 0 is located at the top of the screen, shouldn’t a positive vy hence indicate going down?

Also, if I make the change you are suggesting, then in case 1 the ball doesn’t go through when going up, which confirms my understanding.

Thanks

Hi @endygwa,

There are 3 things which you can do immediately to improve collision sensitivity on small objects which may be moving very fast:

  1. Make the moving object a “bullet” (.isBullet=true) after you add the physics body on it (doing it before won’t have any affect since it won’t be considered a physical object until you add a body to it).

  2. Increase the position and velocity iteration settings of the physics engine:

http://docs.coronalabs.com/api/library/physics/setPositionIterations.html

http://docs.coronalabs.com/api/library/physics/setVelocityIterations.html

  1. Set your core project FPS to 60 instead of 30 (see the guide here).

Of course, you should note all of the “gotchas” about performance penalties and so forth when you increase the Box2D settings beyond their defaults. Normally it shouldn’t have a noticeable impact, but you should use them carefully.

On another note, is your ball and/or platform especially small/thin? Is there a chance that the ball is “skipping through” the platform entirely, as in, on one time step of the app it’s above, then because of a high speed as it drops, it actually skips entirely through the platform (to underneath it) without actually registering a collision with the platform?

Best regards,

Brent

Hi Brent,

I had already tried 1) and it didn’t change a thing. I had also already tried 3) and in the code I posted above, it makes case 3 equivalent to case 2 (ie: by the time isSensor is set to false, the ball already went through. but at least the event is fired so I guess it already helps a bit.

I will try 2) and let you know if it helps, I didn’t know about these two settings, thanks!

As for the ball being thin, it’s the case in the code I posted above but not in my game, and I encounter the same behavior. But shouldn’t isBullet=true do the trick for very small objects thanks to continuous collision detection? 

Thanks

endy

Hmm it did not help, but it seems that I still don’t fully understand something about physics and collisions, I’ll open a new thread with sample code in the physics subforum. 

Thanks