Gamvas Player Controlled Physics Objects

Demonstrates using physics objects that move by player (or AI) interaction

Move mouse to move the violet ball, which is a KINEMATIC object positioned absolute by mouse position.

Cursor keys move the blue ball, which is a DYNAMIC object with forces applied to it.

Following code shows the handling of the mouse controlled kinematic violet ball:

// the 'brain' of the mouse controlled KINEMATIC body
mouseState = gamvas.ActorState.extend({
	onMouseMove: function(x, y, ev) {
        // set position according to mouse position
        var st = gamvas.state.getCurrentState();
        // use mouseposition in world coordinates, instead of screen coordinates
        var worldMouse = st.camera.toWorld(x, y);
        this.actor.setPosition(worldMouse.x, worldMouse.y);
    }
});

// the mouse controlled ball (violet)
mouse = gamvas.Actor.extend({
        create: function(name, x, y) {
            this._super(name, x, y);

            var st = gamvas.state.getCurrentState();
            this.setFile(st.resource.getImage('mouse.png'));
            this.restitution = 0.4;
            // gamvas.physics.KINEMATIC describes objects, that are not
            // under physics control, but are part of the physics world
            // so objects will collide with KINEMATIC objects
            //
            // other then STATIC objects, you may move KINEMATIC objects
            this.bodyCircle(this.position.x, this.position.y, 16, gamvas.physics.KINEMATIC);

            // mouse can move fast, so lets ensure that collisions are detected
            // even when user moves mouse at a speed where it would jump over
            // barriers
            this.setBullet(true);

            // insert brain, aka add and set playerState
            this.addState(new mouseState('brain'));
            this.setState('brain');

            // make our actor receive mouse and keyboard events
            st.registerInputEvents(this);
        }
});