Character movement using physics

Hi All,

I’m porting a platformer game over from another engine. I was using a physics body that has rotations disabled and applying forces to move, when the character’s velocity reaches the maximum speed I apply a force in the opposite direction to manage speed. This worked well in the other engine but I seem to get slightly jerky movement in Corona. By jerky I mean it very quickly stops and then starts again, this happens only every so often.

I made the body for the character have slightly bevelled edges on the bottom because it occasionally got stuck on my tiled based map, the bevel fixed that issue. I was just wondering what everyone else did for physics based character movement?

Thanks in advance.

Hey monotonic,

I have the same issue, could you solve your problem?

Best regards

   Chris

Hey monotonic,

I have the same issue, could you solve your problem?

Best regards

   Chris

EDIT: What is further down below can kind of be made to work; however, I found a much better way in an example from a Level Director example project. Basically you just use the get and set linear velocity within the enterframe listener. I would strongly suggest downloading Level Director and reviewing the example projects (scroller2). There is a free option but the $19 is well worth it for the Full Edition. I haven’t seen any other examples of 2D platformers that have un-buggy movement.

The basic idea:

   local vx,vy = player:getLinearVelocity()

   -EXTRA CODE TO HANDEL DE-ACCELERATION-

   vx= dx

   player:setLinearVelocity(vx,vy)

Where dx is controlled by the left/right buttons. This does not seem to over-ride physics like some options do; the only thing I found is that I needed to remove friction from the player so that he would not levitate when pushing physics objects.

ORIGINAL POST (slightly buggy movement): The best I got was applyForce

e.g. player:applyForce(0, -1000, player.x, player.y) for jumping I think.

Lateral movement is more complicated. I had the right/left buttons toggle rightrun/leftrun variables on and then have the enterFrame event listener apply forces to the player.

I had to use an if statement to stop ‘applyForce’ from cummulatively making the player faster and faster.

e.g.

 xVelocity, yVelocity = guy:getLinearVelocity()

 if leftrun == true then
  if xVelocity > -150 then
   guy:applyForce(-100,0,guy.x,guy.y)
  end
 end

It is not perfect as movement is still slightly jerky; you can adjust it of course by modifying the threshold for applying the force; the movement seemed more jerky in the corona simlulator than it did on the actual device.

Hope that is some help; applyForce seemed better than other options I tried; this isn’t perfect but it is favourable to over-riding the physics engine for some games; I found options that gave smooth movement but over-rode the physics engine meant that the player could not interact normally with physics objects.

If you’ve found a better solution already I’d be keen to know. I am re-doing it at the moment so I’ll be keen to see if I can improve on it.

EDIT: What is further down below can kind of be made to work; however, I found a much better way in an example from a Level Director example project. Basically you just use the get and set linear velocity within the enterframe listener. I would strongly suggest downloading Level Director and reviewing the example projects (scroller2). There is a free option but the $19 is well worth it for the Full Edition. I haven’t seen any other examples of 2D platformers that have un-buggy movement.

The basic idea:

   local vx,vy = player:getLinearVelocity()

   -EXTRA CODE TO HANDEL DE-ACCELERATION-

   vx= dx

   player:setLinearVelocity(vx,vy)

Where dx is controlled by the left/right buttons. This does not seem to over-ride physics like some options do; the only thing I found is that I needed to remove friction from the player so that he would not levitate when pushing physics objects.

ORIGINAL POST (slightly buggy movement): The best I got was applyForce

e.g. player:applyForce(0, -1000, player.x, player.y) for jumping I think.

Lateral movement is more complicated. I had the right/left buttons toggle rightrun/leftrun variables on and then have the enterFrame event listener apply forces to the player.

I had to use an if statement to stop ‘applyForce’ from cummulatively making the player faster and faster.

e.g.

 xVelocity, yVelocity = guy:getLinearVelocity()

 if leftrun == true then
  if xVelocity > -150 then
   guy:applyForce(-100,0,guy.x,guy.y)
  end
 end

It is not perfect as movement is still slightly jerky; you can adjust it of course by modifying the threshold for applying the force; the movement seemed more jerky in the corona simlulator than it did on the actual device.

Hope that is some help; applyForce seemed better than other options I tried; this isn’t perfect but it is favourable to over-riding the physics engine for some games; I found options that gave smooth movement but over-rode the physics engine meant that the player could not interact normally with physics objects.

If you’ve found a better solution already I’d be keen to know. I am re-doing it at the moment so I’ll be keen to see if I can improve on it.