Matter.js - check colliding
Hi, I tried somehow check colliding of between two bodies, checked official documentation I decide use Matter.Detector, it have method 'collisions' - which take 'detector' as argument, but you know, I just can't create 'detector', Matter.Detector - doesn't have any methods to create 'detector'(in microStudio)
I'm confused, maybe there another way to check colliding?
init = function()
engine = Matter.Engine.create()
boxA = Matter.Bodies.rectangle(100, 0, 60, 60)
boxB = Matter.Bodies.rectangle(150, 40, 60, 60)
Matter.World.add(engine.world, [boxA, boxB])
detector = Matter.Detector.create(object
bodies = [boxA, boxB]
end)
// Matter.Engine.run(engine)
Matter.Events.on(engine, 'afterUpdate', checkCollision)
end
checkCollision = function()
local collisions = Matter.Detector.collisions(detector)
if (collisions.length > 0) then
print("Collision detected!")
end
end
update = function()
Matter.Engine.update( engine, 1000/60 )
end
draw = function()
screen.setColor("orange")
screen.drawRect( boxA.position.x, boxA.position.y, boxA.bounds.max.x - boxA.bounds.min.x, boxA.bounds.max.y - boxA.bounds.min.y )
screen.drawRect( boxB.position.x, boxB.position.y, boxB.bounds.max.x - boxB.bounds.min.x, boxB.bounds.max.y - boxB.bounds.min.y )
end
You can also add an event about the beginning and end of the collision - which should make it easier to detect events when, for example, the player is standing on the ground. You will receive information about the fact that it touched the ground and when it jumped - and there will be no constant information that it is standing and there is a collision.
emm, yea, but in class 'Detect' I have only two methods: 'collisions' and 'canCollide', maybe I just... doing some wrong? oh... seems like I download old version of engine.. any way thanks a lot for help :3
Matter.js has a certain style of managing structures.
Maybe it's because it's faster, or maybe because the library allows you to add plugins to this engine.
You have to adapt to it.
You are doing the Detector creation correctly.
In "checkCollision" the "Matter.Detector.collisions(detector)" method will return a list of objects that collide with each other.
Based on this, you can decide what to do next with these objects.
collisions[0] = object
pair = [object]
collided = true
bodyA = [object]
bodyB = [object]
parentA = [object]
parentB = [object]
depth = 0.00005000000000165983
normal = [object]
tangent = [object]
penetration = [object]
supports = [list]
end