Page 1 of 1

Box2D: detecting contact before contact

Posted: Thu Aug 26, 2021 6:19 am
by togFox
I'd like to detect when two objects are approaching before the contact so evasive action is taken before a real contact event.

I could calculate distance between every object every second or so.

I also thought about making the body larger/wider than the image/graphic so the contact event fires before the images actually contact. I would then have to code my own 'real' contact event - which might be a lighter load on the processor.

Any other methods? Can I put two bodies of differing size centred on the same image? The first contact signals 'about to contact' and the second signal is a 'real contact'?

Re: Box2D: detecting contact before contact

Posted: Thu Aug 26, 2021 6:40 am
by darkfrei
togFox wrote: Thu Aug 26, 2021 6:19 am I'd like to detect when two objects are approaching before the contact so evasive action is taken before a real contact event.

I could calculate distance between every object every second or so.

I also thought about making the body larger/wider than the image/graphic so the contact event fires before the images actually contact. I would then have to code my own 'real' contact event - which might be a lighter load on the processor.

Any other methods? Can I put two bodies of differing size centred on the same image? The first contact signals 'about to contact' and the second signal is a 'real contact'?
Looks like

Code: Select all

function love.update(dt)
	if is_contact (dt) then
		do_contact (dt)
	end
end
Where is_contact (dt) and do_contact (dt) are almost same functions, but the first one moves fake objects, but same as original.

Or after the
v = v + dt*a
s = s + dt*v
check the collision and then back:
s = s - dt*v, you get the same situation as before, but you know that you can get the collision in this tick.

Re: Box2D: detecting contact before contact

Posted: Thu Aug 26, 2021 8:46 am
by bobbyjones
I haven't used box2d in awhile but you could indeed have two separate shapes and fixtures for one body. Mark one fixture as a sensor and it will not act on collisions. Look at the diagram in the link below to get a better idea.

https://love2d.org/wiki/love.physics

Re: Box2D: detecting contact before contact

Posted: Thu Aug 26, 2021 11:47 am
by pgimeno

Re: Box2D: detecting contact before contact

Posted: Fri Aug 27, 2021 6:00 am
by togFox
I'll try overlaying two shapes over the same image with the same origin and see how that goes. I've used Box2D on multiple Love projects so that seems the less intimidating to me. I'll call back on the other suggestions if necessary. Thanks all!