Skip to content

startFollow , produces vibration in the game #7174

@tiuvi

Description

@tiuvi

In this example, I test startFollow at a slow speed and zoomed in, which is how it looks best.
Then I comment out startFollow and use my function, which I have enabled in a prerender.

https://cell1.tiuvi.com:1029/mp4/startFollowVibration.mp4

In this video, I test my function, which is my least favorite part: having to round the player's x and y positions.
Also, the camera speed needs to be slightly slower than the player's.
https://cell1.tiuvi.com:1029/mp4/newFunctionStartFollow.mp4

If this function could be done natively, it might perform better.

        state.events.on('prerender', () => {
                
                const velocity = state.global.player.body.velocity;

                const speedPlayerPx = Math.sqrt(velocity.x * velocity.x + velocity.y * velocity.y);

                // Accedemos a la cámara principal y al jugador
                const cam = state.cameras.main;
                const player = state.global.player;

                const px = player.x;
                const py = player.y;

                const centerMapX = cam.width / 2
                const centerMapY = cam.height / 2

                const playerPositionX = px + centerMapX;
                const playerPositionY = py + centerMapY

                const cameraPositionX = cam.scrollX + centerMapX;
                const cameraPositionY = cam.scrollY + centerMapY;

                const marginWidth = (cam.width * (cam.zoom * cam.zoom) / 2) * 0.2

                const marginHeight = (cam.height * (cam.zoom * cam.zoom) / 2) * 0.2


                let moviment = speedPlayerPx === 0 ? 1 : speedPlayerPx * 0.7
                if(moviment < 1){
                    moviment = 1;
                }

                let newCamX = cam.scrollX;
                let newCamY = cam.scrollY;

                if ((cameraPositionX + marginWidth) > playerPositionX) {

                    newCamX = cam.scrollX - moviment;

                } else if ((cameraPositionX + cam.width - marginWidth) < playerPositionX) {

                    newCamX = cam.scrollX + moviment;
                }


                if ((cameraPositionY + marginHeight) > playerPositionY) {

                    newCamY = cam.scrollY - moviment;

                } else if ((cameraPositionY + cam.height - marginHeight) < playerPositionY) {


                    newCamY = cam.scrollY + moviment;
                }

                if (cam.scrollX === newCamX && cam.scrollY === newCamY) {
                    return;
                }


                player.x = Math.round(px)
                player.y = Math.round(py)

                newCamX = Math.round(newCamX);
                newCamY = Math.round(newCamY);


                cam.scrollX = newCamX;
                cam.scrollY = newCamY;
            });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions