Jump to content

How to set a R.U.B.E. generated body's linear velocity


kleepklep
 Share

Recommended Posts

Took me forever to figure this out so wanted to share so someone else doesn't have to take forever to figure this out!

 

If you load in a R.U.B.E. generated Box2d scene, setting body.velocity.x or body.velocity.y will not work, nor will body.moveLeft(n), body.moveRight(n), body.moveUp(n) and body.moveDown(n). You have to set body.data.m_linearVelocity.x and body.data.m_linearVelocity.y instead.

Link to comment
Share on other sites

I also want to point out that even if you uncheck "Sleeping allowed" in R.U.B.E., the body can still go to sleep, in which case it will not react even if you change the linear velocity. I don't see anything about sleep in the Phaser Box2D plugin API, so perhaps it wasn't implemented. To force a body to stay awake, which is not recommended from a performance standpoint but was necessary for my purposes, you can set body.data.m_sleepTime = 0 or use body.applyForce(0, 0) in Phaser's update function.

With these 2 lines of code in my update function if have a wheel that follows / accelerates to the mouse / pointer and then stops. Yes, the plus sign is intentional since the quadrant system is different in R.U.B.E.

wheel.data.m_linearVelocity.x = (wheel.x + game.input.x) * 0.05;wheel.data.m_sleepTime = 0;
This code does the same thing for bodies generated within Phaser.

sprite.body.velocity.x = (sprite.x - game.input.x) * n;//sprite.body.applyForce(0, 0); // doesn't seem to be necessary in this case!
Link to comment
Share on other sites

  • 3 weeks later...

I had, but I must have screwed something up in my test code, because sprite.body.data.SetSleepingAllowed(false) works both on sprites created and box2d enabled in Phaser and on bodies loaded in from R.U.B.E. generated scenes. Although body.bodyDef.allowSleep is never updated, the debugger indicates that the body does stay awake.

Thanks for pointing me back in the right direction! :)

I'm guessing this also means that I could use body.data.SetLinearVelocity(x, y) rather than setting body.data.m_linearVelocity.x like I'm doing.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...