Apply force to object each frame

New to Moai? Get started here with our best tips and tutorials.

Apply force to object each frame

Postby BlueByLiquid » Tue Jul 03, 2012 10:36 pm

Trying to make a lighter than air object and need to apply an upward force each frame. I am using RapaNui but I can't find any examples with or without it that show how to apply a force directly. Any help?
BlueByLiquid
 
Posts: 105
Joined: Fri May 11, 2012 8:54 pm

Re: Apply force to object each frame

Postby ibisum » Wed Jul 04, 2012 12:29 am

Box2D? Set the gravity for the air object to -0.25 (or some such thing).
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info
User avatar
ibisum
 
Posts: 1024
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Apply force to object each frame

Postby BlueByLiquid » Wed Jul 04, 2012 8:15 am

ibisum wrote:Box2D? Set the gravity for the air object to -0.25 (or some such thing).


Oh I didn't know you could set gravity per object. Thanks for that tip that should work well. But for future reference how do you apply a force to an object? For example a rocket, an explosion, etc?
Last edited by BlueByLiquid on Wed Jul 04, 2012 9:32 am, edited 1 time in total.
BlueByLiquid
 
Posts: 105
Joined: Fri May 11, 2012 8:54 pm

Re: Apply force to object each frame

Postby nosheet » Wed Jul 04, 2012 8:21 am

Check out documentation for MOAIBox2DBody, there are following API available (among many others):
Code: Select all
  1. MOAIBox2DBody:applyForce ( MOAIBox2DBody self, number forceX, number forceY [, number pointX, number pointY ] )

  2.  

  3. MOAIBox2DBody:applyLinearImpulse ( MOAIBox2DBody self, number impulseX, number impulseY [, number pointX, number pointY ] )

  4.  



Force should be applied continuously every frame, while impulse is a momentary impact on body. You can also use applyTorque and applyAngularImpulse to rotate the body.
Alternatively, you can directly setLinearVelocity, but this should be used with precaution as it can cause unpredictable results...
You may hate my haircut, but you'll love my games:
http://www.spin-up-game.com and http://www.foosballhero.com
User avatar
nosheet
 
Posts: 201
Joined: Mon May 28, 2012 2:40 pm
Location: Madrid, Spain

Re: Apply force to object each frame

Postby BlueByLiquid » Wed Jul 04, 2012 9:31 am

@nosheet, Thanks! really appreciate the help. I am feeling a bit like a four year old with MOAI sometimes because of the lack of documentation. Am I just not seeing an API reference documentation? I thought at least we would have that.

Thanks again!
BlueByLiquid
 
Posts: 105
Joined: Fri May 11, 2012 8:54 pm

Re: Apply force to object each frame

Postby ibisum » Wed Jul 04, 2012 10:14 am

Oh I didn't know you could set gravity per object.


I don't think you can, I really should have checked before I submitted.. I of course meant Force, not Gravity.

Am I just not seeing an API reference documentation?


http://getmoai.com/docs/class_m_o_a_i_box2_d_body.html
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info
User avatar
ibisum
 
Posts: 1024
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Apply force to object each frame

Postby BlueByLiquid » Wed Jul 04, 2012 12:55 pm

No problem ibisum.

Thanks for the link to the documentation link. Apparently I need to apologize for asking these basic questions I didn't know their was the class documentation or I would have just looked through that. Is this new? I don't remember seeing it before.

Thanks again.
BlueByLiquid
 
Posts: 105
Joined: Fri May 11, 2012 8:54 pm

Re: Apply force to object each frame

Postby ibisum » Thu Jul 05, 2012 1:35 am

Its not that new, but its not that obvious where the Docs are online, either.

I yearn for a return to the days when the HTML docs could be generated automatically from the git repo.
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info
User avatar
ibisum
 
Posts: 1024
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Apply force to object each frame

Postby Andre » Mon Nov 19, 2012 9:18 am

Apologies for reviving this somewhat old post, but I'm trying to do something similar (create floating or lighter than air objects).

I see in the Box2D documentation there is a function to do this: (http://www.box2d.org/manual.html)

Code: Select all
  1. // Set the gravity scale to zero so this body will float

  2. bodyDef.gravityScale = 0.0f;

  3.  



How do you do this in Moai though? It doesn't look like this variable is exposed in the Moai API. Failing this, I want to use the method described in the other posts above, applying a force every Box2D simulation tick. Where would be the best place to do that?
Andre
 
Posts: 10
Joined: Sat Nov 10, 2012 4:30 pm

Re: Apply force to object each frame

Postby ibisum » Mon Nov 19, 2012 11:11 am

Use the second arg of setGravity() to set up your gravity so things either sink or float .. a positive number makes things go up, negative go down. First arg is for horizontal gravitational force, I don't think you'll need that .. ;)

Applying the force? Use a coroutine in your main thread.
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info
User avatar
ibisum
 
Posts: 1024
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Apply force to object each frame

Postby skaflux » Mon Nov 19, 2012 11:16 am

Hello,

I'm a noob here but I hope this helps.

From RNPhysics.lua, you start the physics world by calling RNPhysics.start()

Code: Select all
  1. --- starts physics simulation

  2. -- @param value boolean:optional value not available at the moment

  3. function RNPhysics.start(value)

  4.     if value ~= nil then

  5.         print("noSleep not available at the moment")

  6.     end

  7.     world = MOAIBox2DWorld.new()

  8.     world:setGravity(0, 10)

  9.     world:start()

  10.     world:setUnitsToMeters(0.06)

  11. end



So at any point after that you can change the gravity by doing

Code: Select all
  1. world:setGravity(gravityX,gravityY)



As you can see by default the gravity is set to 10 which is close to the 9.86 Earth's gravity.
skaflux
 
Posts: 7
Joined: Sun Nov 04, 2012 11:59 pm

Re: Apply force to object each frame

Postby Andre » Mon Nov 19, 2012 11:25 am

ibisum wrote:Use the second arg of setGravity() to set up your gravity so things either sink or float .. a positive number makes things go up, negative go down. First arg is for horizontal gravitational force, I don't think you'll need that .. ;)

Applying the force? Use a coroutine in your main thread.


Thanks for the quick response, but I only want to make certain objects float - setting the world gravity will make everything float!

The function in Box2D looks like it's intended to do exactly that, you can set the GravityScale per body, to make certain objects react differently to gravity. I just can't seem to figure out how to call that SetGravityScale from Moai.
Andre
 
Posts: 10
Joined: Sat Nov 10, 2012 4:30 pm

Re: Apply force to object each frame

Postby skaflux » Mon Nov 19, 2012 12:03 pm

from:
http://www.iforce2d.net/b2dtut/custom-gravity

Note: the above is a typical solution for the v2.1.2 release of Box2D used for these tutorials. As of v2.2.1 each body has a 'gravity scale' to strengthen or weaken the effect of the world's gravity on it. This removes the need to manually apply a force every frame. You can set this in the body definition when you create it, or use:

Code: Select all
  1. //Box2D v2.2.1 onwards

  2. body->SetGravityScale(0);//cancel gravity (use -1 to reverse gravity, etc)



Can someone confirm what version of BOX2D we are using?
I didn't see SetGravityScale() reference on the SDK documentation page.

@Andre: You can try body:setGravityScale(0) to see what happens.
skaflux
 
Posts: 7
Joined: Sun Nov 04, 2012 11:59 pm

Re: Apply force to object each frame

Postby Andre » Mon Nov 19, 2012 12:22 pm

skaflux wrote:Can someone confirm what version of BOX2D we are using?


In the moai-dev package, under the 3rdparty directory there is a "box2d-2.2.1" directory.

skaflux wrote:I didn't see SetGravityScale() reference on the SDK documentation page.

@Andre: You can try body:setGravityScale(0) to see what happens.


It doesn't work :(

Code: Select all
  1. attempt to call method 'setGravityScale' (a nil value)

Andre
 
Posts: 10
Joined: Sat Nov 10, 2012 4:30 pm

Re: Apply force to object each frame

Postby ibisum » Mon Nov 19, 2012 1:08 pm

Yup .. a quick look at moai-dev/src/moaicore/MOAIBox2DBody.[cpp,h] indicates that in fact this method hasn't been exposed through the Lua interface. Could be a quick hack to add it, though.

However, it looks like "Catch up with Box2D" needs to be on the Zipline agenda ..
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info
User avatar
ibisum
 
Posts: 1024
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Apply force to object each frame

Postby Andre » Tue Nov 20, 2012 5:59 am

Thanks, at least I know where to go look now if I want to add that method myself :)

For now I'm following your suggestion of applying force every frame in a co-routine. Because I have more floating objects than those that have gravity I'm setting gravity to 0 and apply force to the objects that should not be floating.
Andre
 
Posts: 10
Joined: Sat Nov 10, 2012 4:30 pm

Re: Apply force to object each frame

Postby ibisum » Tue Nov 20, 2012 7:34 am

Sounds like a plan .. lets hear if you run into any problems with this approach, or also success ..
;
--
ibisum@gmail.com
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info
User avatar
ibisum
 
Posts: 1024
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Apply force to object each frame

Postby Andre » Tue Nov 20, 2012 9:08 am

Seems to have worked, not sure if I'm doing it the most efficient way!

Here is a snippet that demonstrates setting gravity to 0, causing objects to float by default and then apply force to those you want gravity to affect.

It's also possible to turn it around, and set gravity on by default and then applying force to the objects you want floating, but it might be difficult to work out exactly how much force is needed. I did it as a test with gravity -10 and the force +5 with the same code below, and the body slowly drifts upwards. Perhaps the coroutine is not called exactly each step?

Code: Select all
  1.  

  2. MOAISim.openWindow ( "test", 640, 480 )

  3.  

  4. viewport = MOAIViewport.new ()

  5. viewport:setSize ( 640, 480 )

  6. viewport:setScale ( 16, 0 )

  7.  

  8. layer = MOAILayer2D.new ()

  9. layer:setViewport ( viewport )

  10. MOAISim.pushRenderPass ( layer )

  11.  

  12. -- set up the world and start its simulation

  13. world = MOAIBox2DWorld.new ()

  14. world:setGravity ( 0, 0 ) -- Set gravity to zero for floating objects

  15. world:setUnitsToMeters ( 2 )

  16. world:start ()

  17. layer:setBox2DWorld ( world )

  18.  

  19. -- Add floating body

  20. floatBody = world:addBody ( MOAIBox2DBody.DYNAMIC )

  21. floatFixture = floatBody:addRect ( 0.5, -0.5, 1.5, 0.5 )

  22. floatFixture:setDensity ( 1 )

  23. floatFixture:setFriction ( 0.3 )

  24. floatFixture:setFilter ( 0x01 )

  25. floatBody:resetMassData ()

  26.  

  27. -- Add dropping body

  28. dropBody = world:addBody ( MOAIBox2DBody.DYNAMIC )

  29. dropFixture = dropBody:addRect ( -1.5, -0.5, -0.5, 0.5 )

  30. dropFixture:setDensity ( 1 )

  31. dropFixture:setFriction ( 0.3 )

  32. dropFixture:setFilter ( 0x01 )

  33. dropBody:resetMassData ()

  34.  

  35. -- Add a ground body

  36. groundBody = world:addBody ( MOAIBox2DBody.STATIC )

  37. groundFixture = groundBody:addRect ( -5, -5, 5, -3 )

  38.  

  39. -- Co routine to apply force

  40. mainThread = MOAICoroutine.new ()

  41. mainThread:run (

  42.         function ()

  43.                 while true do

  44.                         coroutine.yield()

  45.                         -- Apply force every frame to dropping body

  46.                         dropBody:applyForce(0, -5, dropBody:getWorldCenter())

  47.                 end

  48.         end

  49. )

  50.  

Andre
 
Posts: 10
Joined: Sat Nov 10, 2012 4:30 pm

Re: Apply force to object each frame

Postby stun » Tue Nov 20, 2012 11:39 am

Be careful of the units on the various MOAI Box2d functions. I think you'll find applyForce is in Newtons which is kg / m / s squared. Put simply that means the mass of the particular object (MOAIBox2dBody.getMass) is a factor in how much force you want to apply. If you apply a flat -5 force as you have above, heavier objects will 'feel' the effect less than lighter ones (which may be want you want anyway).

I'm a newbie too, just writing an asteroids clone in MOAI with a zero g box2d environment and it took me a little while to get to grips with box2d. The official manual is fairly good though, definitely worth reading from start to finish.

There are also some simply excellent box2d tutorials on line, like this: http://www.iforce2d.net/b2dtut/collision-anatomy

In fact the whole http://www.iforce2d.net website is a treasure trove of excellent box2d tutorials.
stun
 
Posts: 74
Joined: Wed Jul 25, 2012 2:31 pm

Re: Apply force to object each frame

Postby stun » Thu Dec 13, 2012 10:41 pm

Nobody exposed 'setGravityScale' in MOAI? I find myself wanting to experiment with it so I'll give it a crack and post the results here. Ah, it's trivial. Wonder why it wasn't added initially.

MOAIBox2DBody:setGravityScale: https://github.com/c0d3monk33/moai-dev/ ... 545e123156
stun
 
Posts: 74
Joined: Wed Jul 25, 2012 2:31 pm


Return to New Users

Who is online

Users browsing this forum: No registered users and 0 guests

cron

x