I've run into this thrice now, and need some help.
In my game (which uses physics for collision), I have a few objects which need to have velocity but no weight (mass). Moving platforms are the easiest example: they move linearly in a set direction (using setLinearVelocity()), but if they're told to simply move horizontally, they sink over time, because of gravity. Bodies have to have mass if you use velocity (0 mass == static body, at least according to the wiki). I have tried using applyForce() to counteract gravity, but since the direction varies, there is no one magic value that seems to work.
Any ideas? Is there an equation that takes the ydirection and set mass and spits out what will cancel out gravity? (Gravity is set to 700, btw).
Weightless Physics object
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Weightless Physics object
Kurosuke needs beta testers
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Weightless Physics object
You can give them mass=0 and use setLinearVelocity instead of applyForce or applyImpulse. They will be immune to gravity. And any other forces. They will keep moving until you change their linear movement again. And they will be moving with an infinite momentum - expect weird behaviors if your platforms hit things in the same direction in which they are moving. Specially if that something is another platform or the ground - "what happens when an infinite force encounters an indestructible obstacle?" You can find out with Box2D).
When I write def I mean function.
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Weightless Physics object
I hate to disagree, but that's not what I have observed. After trying to get 0 mass bodies to be affected by setLinearVelocity several times, I finally threw this together to create a simpler testing environment. Kikito, 0 mass motion seems impossible in love. :'(kikito wrote:You can give them mass=0 and use setLinearVelocity instead of applyForce or applyImpulse. They will be immune to gravity. And any other forces. They will keep moving until you change their linear movement again. And they will be moving with an infinite momentum - expect weird behaviors if your platforms hit things in the same direction in which they are moving. Specially if that something is another platform or the ground - "what happens when an infinite force encounters an indestructible obstacle?" You can find out with Box2D).
Code: Select all
function love.load()
world = love.physics.newWorld(0, 0, 800, 600)
world:setGravity(0, 700)
world:setMeter(64)
body1 = love.physics.newBody(world, 200, 200, 0)
shape1 = love.physics.newCircleShape(body1, 0, 0, 12)
pressed = false
end
function love.draw()
love.graphics.circle("fill", body1:getX(), body1:getY(), 12)
if pressed then
love.graphics.print("Pressed", 8, 8)
end
end
function love.update(dt)
world:update(dt)
end
function love.keypressed(key, unicode)
body1:setLinearVelocity(10, 0)
pressed = true
end
Kurosuke needs beta testers
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Weightless Physics object
Zero mass is static for Box2D. But can't you just set a very low mass instead? After all, even light has mass (albeit practically 0).
Help us help you: attach a .love.
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Weightless Physics object
Mass should not affect falling speed, unless Box2D is seriously unrealistic.Robin wrote:Zero mass is static for Box2D. But can't you just set a very low mass instead? After all, even light has mass (albeit practically 0).
I guess the only way to pull this off is to set gravity to zero, and then fake it for everything that should have weight. A bad solution, but it should work (in theory). I'll get back to you guys on that.
Kurosuke needs beta testers
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Weightless Physics object
Static objects should not fall. How would you have ground objects if not for static bodies?tentus wrote:Mass should not affect falling speed, unless Box2D is seriously unrealistic.
Help us help you: attach a .love.
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Weightless Physics object
Sorry, I was unclear. Any object with mass will fall at the same rate, 9.8 m/s^2 here on Earth.Robin wrote:Static objects should not fall. How would you have ground objects if not for static bodies?tentus wrote:Mass should not affect falling speed, unless Box2D is seriously unrealistic.
Static objects are an elegant way to replace spherical forces with linear forces, I am not dismissing their usefulness. I just wish I could tell Love to skip gravity in specific instances.
Kurosuke needs beta testers
- ghostwriter
- Prole
- Posts: 38
- Joined: Sat Dec 11, 2010 11:08 pm
Re: Weightless Physics object
Assuming there's no "right way" to do this, couldn't you get the same effect by applying a force opposite to gravity?
Code: Select all
gravX, gravY= world:getGravity()
body:applyForce(-gravX*body:getMass(), -gravY*body:getMass()
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Weightless Physics object
You would think that would work, but the result is an object that rockets skyward. Given a mass of 1, in my game those get()s produce an upward force of 700, while a force of 64 almost works in practice. Good idea though, maybe I can use that as a start to find a functional value?
Kurosuke needs beta testers
- ghostwriter
- Prole
- Posts: 38
- Joined: Sat Dec 11, 2010 11:08 pm
Re: Weightless Physics object
Okay I found the problem. The issue is that world:setGravity(x,y) sets the gravity in pixels, while body:applyForce works in SI units (newtons, meters, etc).
What you need to do is divide your force by world:getMeter to scale it from pixels to meters.
I just tried it out and it works. See attached.
What you need to do is divide your force by world:getMeter to scale it from pixels to meters.
Code: Select all
body:applyForce(-gravX*bodyMass/world:getMeter(), -gravY*bodyMass/world:getMeter())
- Attachments
-
- noGravity.love
- (499 Bytes) Downloaded 150 times
Who is online
Users browsing this forum: No registered users and 46 guests