Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event System #8

Closed
liabru opened this issue Mar 7, 2014 · 6 comments
Closed

Event System #8

liabru opened this issue Mar 7, 2014 · 6 comments
Assignees

Comments

@liabru
Copy link
Owner

liabru commented Mar 7, 2014

An event system is required for external input into the physics engine.

It should at least include events for

  • before and after engine tick
  • before and after render
@liabru liabru self-assigned this Mar 7, 2014
@liabru
Copy link
Owner Author

liabru commented Mar 11, 2014

An event system is now available in the Matter.js edge build, under the namespace Matter.Events.

See the events demo of the event system in use.

Usage is generally:

Matter.Events.on(object, eventNames, callback)

E.g.

Matter.Events.on(myEngine, 'collisionStart', function(event) {
     var pairs = event.pairs;
     // do something with the pairs that have started collision
});

A full example of usage is shown in the latest demo code.

Currently only the Engine has defined events, which include tick and collisionStart, collisionActive, collisionEnd among others.

See the API Docs (edge) for more info, specifically Events and Engine Events.

This system is very new, so please log any issues you find, cheers.

@liabru liabru mentioned this issue Mar 11, 2014
@abataille
Copy link

Great.

@abataille
Copy link

I suggest another event, when the mouse touched a body.
Here is what I added to:
MouseConstraint.update = function(mouseConstraint, bodies) {
var mouse = mouseConstraint.mouse,
constraint = mouseConstraint.constraints[0];

    if (mouse.button === 0 || mouse.button === 2) {
        if (!constraint.bodyB) {
            for (var i = 0; i < bodies.length; i++) {
                var body = bodies[i];
                if (Bounds.contains(body.bounds, mouse.position)  && Vertices.contains(body.vertices, mouse.position)) {
                    constraint.pointA = mouse.position;
                    constraint.bodyB = body;
                    constraint.pointB = { x: mouse.position.x - body.position.x, y: mouse.position.y - body.position.y };
                    constraint.angleB = body.angle;
                    Sleeping.set(body, false);

----> Events.trigger(_engine, 'bodytouched', body);
}
}
}
} else {
constraint.bodyB = null;
constraint.pointB = null;
}

    if (constraint.bodyB) {
        Sleeping.set(constraint.bodyB, false);
        constraint.pointA = mouse.position;
    }
};

@liabru
Copy link
Owner Author

liabru commented Apr 1, 2014

The event system is now included in the latest release:
https://github.com/liabru/matter-js/releases/tag/0.7.0-alpha

If you still need to detect if the mouse has clicked on a body, see here for reference:
https://github.com/liabru/matter-js/blob/master/demo/js/Demo.js#L837

This is the demo code that allows body removal by right clicking in the demo.

Since the event system is now in place, I will close this thread.
Any new issues should be opened in a new thread, cheers.

@liabru liabru closed this as completed Apr 1, 2014
@df257
Copy link

df257 commented Sep 15, 2020

I want use event like this:
var circleA = Bodies.circle(100, 80, 50, null, 20)
Matter.Events.on(circleA , 'mousedown', function(e) {
console.log(111);
});

Can it be achieved?

@liabru
Copy link
Owner Author

liabru commented Sep 15, 2020

That won't work, probably the easiest way would be the startdrag event on a MouseConstraint though I'd suggest looking at the source to see how it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants