Box2d questions and Moving box2d objects

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.
Post Reply
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Box2d questions and Moving box2d objects

Post 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.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Box2d questions and Moving box2d objects

Post 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.
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Box2d questions and Moving box2d objects

Post 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...
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
idbrii
Prole
Posts: 34
Joined: Sat Jun 12, 2021 5:07 am

Re: Box2d questions and Moving box2d objects

Post 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'}},
})
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Box2d questions and Moving box2d objects

Post 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
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
Nawias
Prole
Posts: 9
Joined: Sun Apr 10, 2022 4:13 pm
Location: Poland

Re: Box2d questions and Moving box2d objects

Post 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?
Post Reply

Who is online

Users browsing this forum: No registered users and 84 guests