Does anyone know if it is possible to detect what side of a physics object has been collided with? For example if the bottom of a square has been hit and then it does something, but NOT do anything when it is hit from any other side?
@JonoGaming00 This kind of interested me, so I thought I’d create a small example. The red circles show the contact points. This should work with any type body, but I was lazy and only used circles. The function that determines the contact points for collisions is the bod:collide function. Everything else is just standard code. I tried to make it as simple as I could so it would be easy to understand.
function setup()
b1=bod()
c=physics.body(CIRCLE,70)
c.x=370
c.y=600
c.restitution=.5
c.gravityScale=.1
b1:add(c)
c=physics.body(CIRCLE,50)
c.x=190
c.y=280
c.restitution=.8
c.type=STATIC
b1:add(c)
c=physics.body(CIRCLE,50)
c.x=410
c.y=370
c.type=STATIC
b1:add(c)
c=physics.body(CIRCLE,20)
c.x=320
c.y=254
c.type=STATIC
b1:add(c)
end
function draw()
background(0)
b1:draw()
end
function collide(c)
b1:collide(c)
end
bod=class()
function bod:init()
self.bodies={}
self.contacts={}
end
function bod:add(b)
table.insert(self.bodies,b)
end
function bod:draw()
stroke(255)
strokeWidth(2)
noFill()
-- draw circles
for a,b in pairs(self.bodies) do
ellipse(b.x,b.y,b.radius*2)
end
noStroke()
fill(255,0,0)
-- draw contact points
for c,d in pairs(self.contacts) do
for e,f in pairs(d.points) do
ellipse(f.x,f.y,20)
end
end
end
function bod:collide(c)
if c.state==BEGAN then
self.contacts[c.id]=c
elseif c.state==ENDED then
self.contacts[c.id]=nil
end
end
@JonoGaming00 In your first post you’re talking about a square. To determine which side of a square was hit, use the x,y values of the squares corners and the x,y values from my program. That can tell you which side was hit.