Page 1 of 1

Box2d questions and Moving box2d objects

Posted: Tue Jul 06, 2021 12:22 pm
by Gunroar:Cannon()
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.

Re: Box2d questions and Moving box2d objects

Posted: Tue Jul 06, 2021 1:18 pm
by ivan
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.

Re: Box2d questions and Moving box2d objects

Posted: Tue Jul 06, 2021 5:34 pm
by Gunroar:Cannon()
Hey there! :awesome: 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...

Re: Box2d questions and Moving box2d objects

Posted: Tue Jul 06, 2021 9:56 pm
by idbrii
> 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'}},
})

Re: Box2d questions and Moving box2d objects

Posted: Wed Jul 07, 2021 1:09 pm
by Gunroar:Cannon()
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 :awesome:
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 :P

Re: Box2d questions and Moving box2d objects

Posted: Fri Dec 23, 2022 5:42 pm
by Nawias
I'm trying to ignore some objects exported from Tiled (I'm not using STI, but my own implementation). I think I'm doing everyghing right: Player has class 'Player' and every loaded collision class gets 'Player' assigned into their ignored table. Yet I still collide with those shrooms! It used to be hit or miss, sometimes I would collide and sometimes not, but now it just collides every time. When I used idbrii's fork it worked one time and then reverted to colliding again. What might I be doing wrong?

Image

EDIT:
I noticed something very, very weird. The problem went away again after I used a different terminal to run the game. Then, it started colliding again, so I tried a different terminal - same story, first try was good, then the problem came back. Does Love2D have some weird cache or something?