Create constant speed for physics body ?

Hi,

unfortunately I have to ask again for help :slight_smile:

So where is my problem?

I want to let crates falling down with the same speed. I was looking in the API and in the forums/google and couldn’t find a solution for this.

I’ve read something about applyForce or linearForce, but this doesn’t seem to work?!

Is there a function or paramter to set and keep the speed of a physics body?

Please help. Thank you.

I modified just the manycrates tutorial for this:

function newCrate() j = display.newImage("crate.png"); j.x = 60 j.y = -100 physics.addBody( j, { density=0.9, friction=0.3, bounce=0.0} ) j:applyForce( 0, 100, j.x, j.y ) j = display.newImage("crate.png"); j.x = 140 j.y = -200 physics.addBody( j, { density=0.9, friction=0.3, bounce=0.0} ) j:applyForce( 0, 100, j.x, j.y ) j = display.newImage("crate.png"); j.x = 220 j.y = -300 physics.addBody( j, { density=0.9, friction=0.3, bounce=0.0} ) j:applyForce( 0, 10, j.x, j.y ) end local dropCrates = timer.performWithDelay( 500, newCrate, 5 )

You need to set the object’s “gravityscale” to 0.  See:  http://docs.coronalabs.com/api/type/Body/gravityScale.html

This will take the default 9.8 meters per second per second acceleration off of the object and cause it to float.  Then your application of linear force should move it at a fixed speed.

Thank you :slight_smile:

I first thought gravity = 0 is nonsense, but thanks to you I understand now what the applyForce-function is for.

physics.setGravity( 0, 0 ) works aswell :slight_smile:

Setting physics to 0,0 impacts all objects…  I was building an airplane game for kids and they needed to fly the plane into a big ball and move it.  I wanted gravity to affect the ball, but not the plane.  Using the gravityScale on the object allows the rest of the physics model continue with normal gravity.

If you are not needing gravity or physics in general, there are other ways that don’t use physics at all, like transitions changing the X, Y in a game loop (looking Runtime enterFrame events) and such.

Okay, but wouldn’t it be easier just to make a plane without a physics body? :smiley: Hehe, but it’s better not to start a discussion now, you solved my problem :slight_smile:

I know what you mean and I had the same thoughts :slight_smile:

Actually I need it for all objects, that way it’s not a problem.

The only reason why I won’t use transition.to() is that if the crates “touch” each other, transition won’t make this collide effect, which I want :slight_smile:

But nevertheless it’s working now, I just don’t know how many and how strong physics is using CPU ressources, but well it works.

Nevertheless hank you for the hint.

The game had a bunch of things that I needed physics for (or to make my life easy) like spinning rods that I couldn’t get working with my own non-physics methods to work right.  I had the ball I needed to behave with gravity.  Believe me, I avoid physics when I don’t need them.

Hi again,

I don’t like to dig in old threads, but well it’s about the same thing:

When I use j:applyForce( 0, 100, j.x, j.y ), I want later to change its value, for example j:applyForce( 0, 10000, j.x, j.y ).

After I tested it, I couldn’t feel/see, that something changed.

Or does it change the Force immediately and I’m wrong?

Can someone explain how it works? And is there a possibility to reset this?

Thesis:

a) j:applyForce( 0, 100, j.x, j.y ); j:applyForce( 0, 100, j.x, j.y ); does it cumulate/stack? means force==200 ?

b)I’m looking for removeForce or resetForce whatever…, or am I totally wrong with this?

Thanks.

Hi @maxisto,

The application of force does “stack” if you continue to apply force vectors to an object. If you don’t see an effect, check that you’re applying the sufficient amount of force to move the object (more force if it’s a heavy object). Required force values can vary wildly depending on the object you’re trying to push, so experiment with the value until you get what you need.

On your other question: there is no way to simply revert the force to 0, since it has already been applied. I suppose you could read/save the directional velocity of the object before the force was applied then apply it later via :setLinearVelocity, but otherwise, you can’t just “undo” force. You could attempt to apply the same force in the opposite direction, but since you can’t read the cumulative value (and it may vary on other factors like damping, gravity, etc.), it will be difficult to get it reversed perfectly.

Hope this helps,

Brent

Hi,

thank you. I’m glad that I asked for help, else I wouldn’t get close to a solution. So I’m right :/.

Well, it must not be perfect, I just want to have a slowdown effect (for example: slow down 90% of the speed).

I don’t know how :setLinearVelocity works and would help me :/. Don’t have much time today to test it.

Apparently,

j:applyForce( 0, 100, j.x, j.y );

j:applyForce( 0, -90, j.x, j.y );

could work?

Yes, that should work. If you want to “revert” to a (nearly) original state after a series of force applications, you may be able to keep track of each vector applied in sequence, then reverse-loop through that series and apply all of them in the opposite direction. For example, if you did this along the X axis:

100, 100, 20, 50, 60.5

Then, loop backward and the object should revert to nearly where it was to start:

-60.5, -50, -20, -100, -100

I haven’t tested this, however, so I can’t promise it’ll work exactly as expected.

Brent

Thank you very much, with your smart advice I reached the effect I wanted<2
I really didn’t have the thought of a “reverse” force.

target:applyForce( 0, (((-1)*speed)+1000), target.x, target.y )
target:applyForce( 0, speed-1000, target.x, target.y )

Problem solved. :slight_smile:

You need to set the object’s “gravityscale” to 0.  See:  http://docs.coronalabs.com/api/type/Body/gravityScale.html

This will take the default 9.8 meters per second per second acceleration off of the object and cause it to float.  Then your application of linear force should move it at a fixed speed.

Thank you :slight_smile:

I first thought gravity = 0 is nonsense, but thanks to you I understand now what the applyForce-function is for.

physics.setGravity( 0, 0 ) works aswell :slight_smile:

Setting physics to 0,0 impacts all objects…  I was building an airplane game for kids and they needed to fly the plane into a big ball and move it.  I wanted gravity to affect the ball, but not the plane.  Using the gravityScale on the object allows the rest of the physics model continue with normal gravity.

If you are not needing gravity or physics in general, there are other ways that don’t use physics at all, like transitions changing the X, Y in a game loop (looking Runtime enterFrame events) and such.

Okay, but wouldn’t it be easier just to make a plane without a physics body? :smiley: Hehe, but it’s better not to start a discussion now, you solved my problem :slight_smile:

I know what you mean and I had the same thoughts :slight_smile:

Actually I need it for all objects, that way it’s not a problem.

The only reason why I won’t use transition.to() is that if the crates “touch” each other, transition won’t make this collide effect, which I want :slight_smile:

But nevertheless it’s working now, I just don’t know how many and how strong physics is using CPU ressources, but well it works.

Nevertheless hank you for the hint.

The game had a bunch of things that I needed physics for (or to make my life easy) like spinning rods that I couldn’t get working with my own non-physics methods to work right.  I had the ball I needed to behave with gravity.  Believe me, I avoid physics when I don’t need them.

Hi again,

I don’t like to dig in old threads, but well it’s about the same thing:

When I use j:applyForce( 0, 100, j.x, j.y ), I want later to change its value, for example j:applyForce( 0, 10000, j.x, j.y ).

After I tested it, I couldn’t feel/see, that something changed.

Or does it change the Force immediately and I’m wrong?

Can someone explain how it works? And is there a possibility to reset this?

Thesis:

a) j:applyForce( 0, 100, j.x, j.y ); j:applyForce( 0, 100, j.x, j.y ); does it cumulate/stack? means force==200 ?

b)I’m looking for removeForce or resetForce whatever…, or am I totally wrong with this?

Thanks.

Hi @maxisto,

The application of force does “stack” if you continue to apply force vectors to an object. If you don’t see an effect, check that you’re applying the sufficient amount of force to move the object (more force if it’s a heavy object). Required force values can vary wildly depending on the object you’re trying to push, so experiment with the value until you get what you need.

On your other question: there is no way to simply revert the force to 0, since it has already been applied. I suppose you could read/save the directional velocity of the object before the force was applied then apply it later via :setLinearVelocity, but otherwise, you can’t just “undo” force. You could attempt to apply the same force in the opposite direction, but since you can’t read the cumulative value (and it may vary on other factors like damping, gravity, etc.), it will be difficult to get it reversed perfectly.

Hope this helps,

Brent

Hi,

thank you. I’m glad that I asked for help, else I wouldn’t get close to a solution. So I’m right :/.

Well, it must not be perfect, I just want to have a slowdown effect (for example: slow down 90% of the speed).

I don’t know how :setLinearVelocity works and would help me :/. Don’t have much time today to test it.

Apparently,

j:applyForce( 0, 100, j.x, j.y );

j:applyForce( 0, -90, j.x, j.y );

could work?

Yes, that should work. If you want to “revert” to a (nearly) original state after a series of force applications, you may be able to keep track of each vector applied in sequence, then reverse-loop through that series and apply all of them in the opposite direction. For example, if you did this along the X axis:

100, 100, 20, 50, 60.5

Then, loop backward and the object should revert to nearly where it was to start:

-60.5, -50, -20, -100, -100

I haven’t tested this, however, so I can’t promise it’ll work exactly as expected.

Brent