Box2D: detecting contact before contact

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Box2D: detecting contact before contact

Post 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'?
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Box2D: detecting contact before contact

Post 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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Box2D: detecting contact before contact

Post 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
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Box2D: detecting contact before contact

Post 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!
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests