## Box2d questions and Moving box2d objects

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Gunroar:Cannon()
Party member
Posts: 605
Joined: Thu Dec 10, 2020 1:57 am

### Box2d questions and Moving box2d objects

Helloo, Two questions pls,

1)I'm using box2d with windfield and I'm trying to simulate particles(like elements). So when an element with a random velocity hits an obstacle I want it to bounce to another directions. I can think of setting it's velocity to the velocity of the object it hit, but how about if it hits a wall with zero velocity. Sometimes when it does that it just slides in one direction.

This is what I use to move it:

Code: Select all


function Element:update(dt)
--collider is windfield collider, works like box2d body I think
if self.collider:stay("enter") then

local collision_data = self.collider:getEnterCollisionData('Shape')
local element = collision_data.collider:getObject()
self:resetVelocity()
end

end

function Element:resetVelocity()
self.collider:
--(??) applyLinearImpulse
setLinearVelocity(
lume.randomchoice({-vel,vel}),
lume.randomchoice({-vel,vel})
)
end

2) With windfield/box2d is it possible to make some objects ignore other objects, but still be in the same world? Like how bump.lua has a filter method.
-It might look like nonsense but there's so much proof.
-Some more if you pls... .
It has to be real then, all that talk about the end .
-How to be saved
ivan
Party member
Posts: 1736
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: Box2d questions and Moving box2d objects

If you want the simulation to behave "realistically" then do not change the velocity manually.
The math involved in figuring out how to resolve the collision is complicated and depends on friction, restitution, mass and other factors.
If you can calculate the resulting forces yourself, then you don't need Box2D at all.
Gunroar:Cannon()
Party member
Posts: 605
Joined: Thu Dec 10, 2020 1:57 am

### Re: Box2d questions and Moving box2d objects

Hey there! The main thing I need is circle collision but I used foresight (the Pokémon move that's useless) and saw I would probally need it later on.
Plus I don't need realism that much

The gravity is 0, so that's where it comes from. Gas particles flowing in empty space. Plus I would like to speed it up.
I wouldn't want to divert from using box2d for this, so please, any help appreciated...
...
2 questions...
-It might look like nonsense but there's so much proof.
-Some more if you pls... .
It has to be real then, all that talk about the end .
-How to be saved
idbrii
Prole
Posts: 12
Joined: Sat Jun 12, 2021 5:07 am

### Re: Box2d questions and Moving box2d objects

> So when an element with a random velocity hits an obstacle I want it to bounce to another directions.

Reflecting momentum should be the default behaviour of box2d (unless you want to bounce to be a random direction?). Try using setLinearVelocity/applyLinearImpulse to set the *initial* movement of the particles and let the physics simulation handle the rest.

Ensure you're using 'dynamic' colliders!

> 2) With windfield/box2d is it possible to make some objects ignore other objects, but still be in the same world? Like how bump.lua has a filter method.

In plain love.physics: https://love2d.org/wiki/Fixture:setFilterData

In windfield: when you use World:addCollisionClass, include ignores in your second argument:

Code: Select all

world:addCollisionClass('Player', {ignores = {'NPC', 'Enemy'}})
You can also checkout my windfield fork. I merged a bunch of the fixes from PRs on the original and I've added new improvements. One of which is being able to add your collision classes all at once without worrying about order:

Code: Select all

world:addCollisionClassTable({
Player = {ignores = {'NPC', 'Enemy'}},
NPC = {},
Enemy = {ignores = {'NPC'}},
})
Gunroar:Cannon()
Party member
Posts: 605
Joined: Thu Dec 10, 2020 1:57 am

### Re: Box2d questions and Moving box2d objects

Wow, thnX, idbrii. Number 2 question solved though that one still isn't(partly atleast). It did lead me to find fixture:setRestitution(res) which I made res 1 and now it bounces
But now it loses some of its kinetic energy and gets slower as it moves. Anyway to fix this?

Edit(after -like- a month): blgghhhrh! It'sa me the boogy monstyrrr!! Roawwr
-It might look like nonsense but there's so much proof.
-Some more if you pls... .
It has to be real then, all that talk about the end .
-How to be saved

### Who is online

Users browsing this forum: Bing [Bot] and 42 guests