Search code examples
matter.js

How can I move camera in matter.js


How can I move the camera to a specific position in matter.js?

I want the camera to follow my main character.

I tried this tutorial: http://brm.io/matter-js/demo/#views but truth be told, I did not understand it.


Solution

  • I found how to make it: http://codepen.io/vanuatu/pen/VeQMpp

    We need to control render bounds, like this:

    //
    hero = bubbleBall
    
    // Follow Hero at X-axis
    engine.render.bounds.min.x = centerX + hero.bounds.min.x
    engine.render.bounds.max.x = centerX + hero.bounds.min.x + initialEngineBoundsMaxX
    
    // Follow Hero at Y-axis
    engine.render.bounds.min.y = centerY + hero.bounds.min.y
    engine.render.bounds.max.y = centerY + hero.bounds.min.y + initialEngineBoundsMaxY
    
    // Update Mouse
    Mouse.setOffset(mouseConstraint.mouse, engine.render.bounds.min);
    

    I hope that can help someone