physics - create a floating leaf?

I have a question about physics.

I want to take a JPG file and make it float like a leaf from top to the bottom of the screen. I tried Code Exchange and no luck…can anyone help please?

I am using the physics template. The one where the crate falls from the top of the screen…I would like to make it float down like a leaf would.

Im new to LUA and still reading the crash course book… so bear with me. Any help is appreciated, thanks. [import]uid: 157993 topic_id: 30037 reply_id: 330037[/import]

HI there,
When you say “float down like a leaf”, do you mean a sort of back-and-forth curved zigzag movement? Or just a slower downward movement, affected less by gravity? Each of these would require different approaches. It helps to clarify so we can help you figure it out.

Brent
[import]uid: 9747 topic_id: 30037 reply_id: 120282[/import]

hi thanks for the reply. I mean the left to right floating motion…as it travels down…like a leaf^^

I want to get the movement to be random…so fast leaves and slow leaves, respectively. Thanks a lot! :slight_smile: [import]uid: 157993 topic_id: 30037 reply_id: 120288[/import]

The physics engine takes care of gravity for you, but to model air flow, you’re going to have to do that on your own.

You could either use an enterFrame listener to implement a gameLoop function that would move your leaf side to side periodically, or use a timer.performWithDelay() call to have it fire off a timer after a random interval and move your leaf.

To simulate the air, you would use the physics method applyLinearImpluse

http://docs.coronalabs.com/api/type/Body/applyLinearImpulse.html

to puff wind at the leaf. [import]uid: 19626 topic_id: 30037 reply_id: 120321[/import]

Thanks for the link but i have a few questions about the syntax…

OK so…considering the code below from the docs -

Body:applyLinearImpulse( xForce, yForce, bodyX, bodyY )

the word Body is the name of the variable, right? or is that a key word?

And the part that says “( xForce, yForce, bodyX, bodyY )”, i was wondering how to make those random values if they only accept numbers…

Thanks a lot so far everyone I really appreciate all the help.

[import]uid: 157993 topic_id: 30037 reply_id: 120334[/import]

Hi again,

“body” simply indicates the leaf object… on which you have, of course, applied a physical body. :slight_smile:

You can generate random force values using this call:

local value = math.random( m, n )

“m” and “n” define the range that the random number will fall between.

for “bodyX” and “bodyY” in the force application, typically those will always be “leaf.x” and “leaf.y” (or whatever you call the leaf object). This applies the force to the body’s center point… the alternative being that you can apply force to a different part of a body, thus “spinning” or tipping it around its axis, but the common usage of applying force happens on the center point.

Brent
[import]uid: 9747 topic_id: 30037 reply_id: 120347[/import]

i am definitely going to try this when i get home. I am checking this on my phone so i cant edit it…but i am speeding now to get home!! - Watch out comin’ through in the name of Lua!!! lol! [import]uid: 157993 topic_id: 30037 reply_id: 120351[/import]

ok im probably doing something wrong but i get a runtime error that says there is a error loading the file.

I wanted to say that I am using the physics template example as a base for what Im doing. The one where the crate falls from the top of the screen. I probably should have mentioned that before. Sorry about that.

Have any idea what im doing wrong with the code? Thanks! :slight_smile:

[import]uid: 157993 topic_id: 30037 reply_id: 120367[/import]

I tried this and it doesnt seem to be working. Anyone can tell me what is wrong with it? Thanks everybody.

For physics -

local bigLeaf applyLinearImpulse( 10, 5, bigLeafX, bigLeafY )
local bigLeaf math.random( 0.5, 1 ) [import]uid: 157993 topic_id: 30037 reply_id: 120376[/import]

Should be something closer to this:

local bigLeaf = newImage() --follow the documentation for the parameters  
physics.addBody( bigLeaf, ... ) --again, see physics documentation for "addBody" parameters  
  
local randomXforce = math.random( 0.5, 1 )  
local randomYforce = math.random( 0.5, 1 )  
  
bigLeaf:applyLinearImpulse( randomXforce, randomYforce, bigLeaf.x, bigLeaf.y )  

Does that help? [import]uid: 9747 topic_id: 30037 reply_id: 120381[/import]

Thanks for the post. I havent tried it yet but i would like to just make sure I understand correctly…

local randomXforce = math.random( 0.5, 1 )

In the above, “randomXforce” is the name of my variable, right? >_>
Thanks, Brent! :slight_smile: [import]uid: 157993 topic_id: 30037 reply_id: 120496[/import]

Yes, exactly. Any time you see something like “local ... = ”, with the “...” being some name, that’s a custom local variable declared by the user, not a Lua or Corona reserved name. It might be possible to actually declare variables named the same as a reserved function, but it would be really awful coding practice so I’ve never even considered it. :stuck_out_tongue:

Of course, if you see something like “... = ” (same as above but without the “local” prefix), this could mean two things: 1) either this a global variable being declared right at that time (not recommended; global variables are largely frowned up in Lua)… OR 2) it means that the variable was already declared somewhere above, in which case the statement now references that variable (in other words, it’s not creating a new “copy” of it).

--NEW variable declaration:  
local myVariable = 0  
  
--REFERENCE notation:  
local myVariable = 0 --declare it!  
myVariable = 10 --now change its value!  

Brent
[import]uid: 9747 topic_id: 30037 reply_id: 120497[/import]

HI there,
When you say “float down like a leaf”, do you mean a sort of back-and-forth curved zigzag movement? Or just a slower downward movement, affected less by gravity? Each of these would require different approaches. It helps to clarify so we can help you figure it out.

Brent
[import]uid: 9747 topic_id: 30037 reply_id: 120282[/import]

hi thanks for the reply. I mean the left to right floating motion…as it travels down…like a leaf^^

I want to get the movement to be random…so fast leaves and slow leaves, respectively. Thanks a lot! :slight_smile: [import]uid: 157993 topic_id: 30037 reply_id: 120288[/import]

The physics engine takes care of gravity for you, but to model air flow, you’re going to have to do that on your own.

You could either use an enterFrame listener to implement a gameLoop function that would move your leaf side to side periodically, or use a timer.performWithDelay() call to have it fire off a timer after a random interval and move your leaf.

To simulate the air, you would use the physics method applyLinearImpluse

http://docs.coronalabs.com/api/type/Body/applyLinearImpulse.html

to puff wind at the leaf. [import]uid: 19626 topic_id: 30037 reply_id: 120321[/import]

Thanks for the link but i have a few questions about the syntax…

OK so…considering the code below from the docs -

Body:applyLinearImpulse( xForce, yForce, bodyX, bodyY )

the word Body is the name of the variable, right? or is that a key word?

And the part that says “( xForce, yForce, bodyX, bodyY )”, i was wondering how to make those random values if they only accept numbers…

Thanks a lot so far everyone I really appreciate all the help.

[import]uid: 157993 topic_id: 30037 reply_id: 120334[/import]

Hi again,

“body” simply indicates the leaf object… on which you have, of course, applied a physical body. :slight_smile:

You can generate random force values using this call:

local value = math.random( m, n )

“m” and “n” define the range that the random number will fall between.

for “bodyX” and “bodyY” in the force application, typically those will always be “leaf.x” and “leaf.y” (or whatever you call the leaf object). This applies the force to the body’s center point… the alternative being that you can apply force to a different part of a body, thus “spinning” or tipping it around its axis, but the common usage of applying force happens on the center point.

Brent
[import]uid: 9747 topic_id: 30037 reply_id: 120347[/import]

i am definitely going to try this when i get home. I am checking this on my phone so i cant edit it…but i am speeding now to get home!! - Watch out comin’ through in the name of Lua!!! lol! [import]uid: 157993 topic_id: 30037 reply_id: 120351[/import]

ok im probably doing something wrong but i get a runtime error that says there is a error loading the file.

I wanted to say that I am using the physics template example as a base for what Im doing. The one where the crate falls from the top of the screen. I probably should have mentioned that before. Sorry about that.

Have any idea what im doing wrong with the code? Thanks! :slight_smile:

[import]uid: 157993 topic_id: 30037 reply_id: 120367[/import]

I tried this and it doesnt seem to be working. Anyone can tell me what is wrong with it? Thanks everybody.

For physics -

local bigLeaf applyLinearImpulse( 10, 5, bigLeafX, bigLeafY )
local bigLeaf math.random( 0.5, 1 ) [import]uid: 157993 topic_id: 30037 reply_id: 120376[/import]