Physics.gravityScale PROBLEM!

Hi everyone…

Look at this code

myCar1 = display.newImageRect( "images/car1.png", 50, 75 ) group:insert(myCar1) myCar1.x = display.contentCenterX myCar1.y = display.contentCenterY physics.addBody( myCar1, { density=1.0, bounce=0.01 } ) myCar1.isFixedRotation = true myCar1.gravityScale = -0.03 background:addEventListener('tap', function(e) &nbsp;&nbsp; &nbsp;if e.x \> myCar1.x then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(myCar1, {time=100, x=e.x, y=e.y}) &nbsp;&nbsp; &nbsp;elseif e.x \< myCar1.x then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(myCar1, {time=100, x=e.x, y=e.y}) &nbsp;&nbsp; &nbsp;else &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("that's it") &nbsp;&nbsp; &nbsp;end end)

I tap on the screen, and move the car anywhere…

the car seems to move forward, because the gravity -0.03

not too fast, very slow

but as I keep playing, little by little, the car start to going faster and faster

like

gravity -0.03

gravity -0.08

gravity -0.1

gravity -0.4

gravity -0.9

and so on…

Why?

Hi.

First, you’re combining physics motion with transition, so your end result may be hard to quantify.  i.e. You may see weird behavior if a transition works in opposition of the physics system.  

Second,  gravity is a force.  Forces accelerate objects.  If you re-arrange the well known equation, you get:

F = MA —> A = F/M  i.e. Acceleration equals Force divided by Mass.  Acceleration increases directly proportionally to Force.

So, by changing gravityScale, you are changing the magnitude of acceleration, but not removing or negating it.  

If you want to limit acceleration, you’ll need to do so manually or experiment with obj.linearDamping to find a value you like.  

Note: Even with damping, you still have acceleration.  The only way to limit it is by checking velocity and setting it to a lower value if it is too high.  You’ll need to know vector math to do this.

-Ed

One more thing.  Gravity is not changing.

Acceleration is making your object move faster over time.  If we ignore mass, velocity resolves to this linear equation:

Velocity = Force * Time

Let’s try some values:

  • Gravity == 10 (this is really an acceleration but because the relationship between force and acceleration is linear, and we are ignoring mass, we can treat it equivalently to a force.)
  • Gravity Scale = 0.03
  • Time = 10 seconds

Velocity == 10 * 0.03 * 10 == 3 pixels per-second

In 100 seconds, that will be 300 pixels-per-second and so forth.

PS - If it wasn’t clear above, acceleration is inversely proportional to mass.  i.e. Higher mass equals lower acceleration.

Hi roaminggamer…

Thanks for all that information, it’s like 2 years of collage in 10 lines…

It’s a lot for me at this point…

I just wanted to have a little car image moving left or right when tap on screen like this

function

    if I tap on the left of the center of the image car1.png move the car1.png to the left exactly where I tapped

    or if I tape to the right then move it to the right

end

and then little blocks will be moving from top of the screen towards the bottom of the screen, either with

   transition.to BLOCKS from top to bottom

or with gravity

to simulate the car is moving, and then detect if the BLOCKS are hitting the car1.png the kids loose points

I hope you could help me with this

thanks

Hi.

First, you’re combining physics motion with transition, so your end result may be hard to quantify.  i.e. You may see weird behavior if a transition works in opposition of the physics system.  

Second,  gravity is a force.  Forces accelerate objects.  If you re-arrange the well known equation, you get:

F = MA —> A = F/M  i.e. Acceleration equals Force divided by Mass.  Acceleration increases directly proportionally to Force.

So, by changing gravityScale, you are changing the magnitude of acceleration, but not removing or negating it.  

If you want to limit acceleration, you’ll need to do so manually or experiment with obj.linearDamping to find a value you like.  

Note: Even with damping, you still have acceleration.  The only way to limit it is by checking velocity and setting it to a lower value if it is too high.  You’ll need to know vector math to do this.

-Ed

One more thing.  Gravity is not changing.

Acceleration is making your object move faster over time.  If we ignore mass, velocity resolves to this linear equation:

Velocity = Force * Time

Let’s try some values:

  • Gravity == 10 (this is really an acceleration but because the relationship between force and acceleration is linear, and we are ignoring mass, we can treat it equivalently to a force.)
  • Gravity Scale = 0.03
  • Time = 10 seconds

Velocity == 10 * 0.03 * 10 == 3 pixels per-second

In 100 seconds, that will be 300 pixels-per-second and so forth.

PS - If it wasn’t clear above, acceleration is inversely proportional to mass.  i.e. Higher mass equals lower acceleration.

Hi roaminggamer…

Thanks for all that information, it’s like 2 years of collage in 10 lines…

It’s a lot for me at this point…

I just wanted to have a little car image moving left or right when tap on screen like this

function

    if I tap on the left of the center of the image car1.png move the car1.png to the left exactly where I tapped

    or if I tape to the right then move it to the right

end

and then little blocks will be moving from top of the screen towards the bottom of the screen, either with

   transition.to BLOCKS from top to bottom

or with gravity

to simulate the car is moving, and then detect if the BLOCKS are hitting the car1.png the kids loose points

I hope you could help me with this

thanks