Weightless Physics object

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Weightless Physics object

Post by tentus »

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).
Kurosuke needs beta testers
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Weightless Physics object

Post by kikito »

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.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Weightless Physics object

Post by tentus »

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).
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. :'(

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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Weightless Physics object

Post by Robin »

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.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Weightless Physics object

Post by tentus »

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).
Mass should not affect falling speed, unless Box2D is seriously unrealistic.

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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Weightless Physics object

Post by Robin »

tentus wrote:Mass should not affect falling speed, unless Box2D is seriously unrealistic.
Static objects should not fall. How would you have ground objects if not for static bodies?
Help us help you: attach a .love.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Weightless Physics object

Post by tentus »

Robin wrote:
tentus wrote:Mass should not affect falling speed, unless Box2D is seriously unrealistic.
Static objects should not fall. How would you have ground objects if not for static bodies?
Sorry, I was unclear. Any object with mass will fall at the same rate, 9.8 m/s^2 here on Earth.

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
User avatar
ghostwriter
Prole
Posts: 38
Joined: Sat Dec 11, 2010 11:08 pm

Re: Weightless Physics object

Post by ghostwriter »

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()
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Weightless Physics object

Post by tentus »

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
User avatar
ghostwriter
Prole
Posts: 38
Joined: Sat Dec 11, 2010 11:08 pm

Re: Weightless Physics object

Post by ghostwriter »

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.

Code: Select all

body:applyForce(-gravX*bodyMass/world:getMeter(), -gravY*bodyMass/world:getMeter())
I just tried it out and it works. See attached.
Attachments
noGravity.love
(499 Bytes) Downloaded 150 times
Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests