#I can't get this collision detection to work.

1 messages · Page 1 of 1 (latest)

quaint basin
#
        let player = new Player({
            position: {
                x: 50,
                y: 760
            },
            velocity: {
                x: 0,
                y: 0
            }}, socket.id, username)
        PLAYER_LIST[socket.id] = player
        socket.on('keypress', function(data){
            if(data.inputID === 'right'){
                player.pressingRight = data.pressed
                player.velocity.x = 5
            }
            if(data.inputID === 'left'){
                player.pressingLeft = data.pressed
                player.velocity.x = 5
            }
            if(data.inputID === 'down'){
                player.pressingDown = data.pressed
                player.velocity.y = 5
            }
            if(data.inputID === 'up'){
                player.pressingUp = data.pressed
                player.velocity.y = 5
            }
            if(data.inputID === 'shift'){
                if (data.pressed) {
                    player.isDashing = true
                } else {
                    player.isDashing = false
                }
            }
        })```
I have more code that i might add to messages.
#
    boundaries.forEach(boundary => {
        for(let i in PLAYER_LIST){
            let player = PLAYER_LIST[i]
            if (player.position.x <= 0 || player.position.x >= 840 || player.position.y <= 0 || player.position.y >= 840){
                player.velocity.x = 0
                player.velocity.y = 0 
                
            }
            if(player.position.y + player.velocity.y
                <= 
                boundary.position.y + boundary.height &&
            player.position.x + player.width + player.velocity.x 
                >= 
                boundary.position.x &&
            player.position.y + player.height + player.velocity.y 
                >= 
                boundary.position.y &&
            player.position.x + player.velocity.x 
                <= 
                boundary.position.x + boundary.width){
                player.velocity.x = 0
                player.velocity.y = 0
            } 
        }
    })
}```
#
    constructor({position, velocity}, id, username){
        this.position = position
        this.velocity = velocity
        this.id = id
        this.username = username
        this.height = 20
        this.width = 20
        this.pressingRight = false
        this.pressingLeft = false
        this.pressingUp = false
        this.pressingDown = false
        this.isDashing = false
        this.angle = 0
        this.rotation
        this.mouseX
        this.mouseY
        this.targetVelocityX
        this.targetVelocityY
        this.health = 200
        this.powerUp
        this.isConsuming
        this.powerUpTimer = 0
        this.powerUpTime = 200
        this.score = 0
    }
    updatePosition(){
        if(this.pressingRight){
            this.position.x += this.velocity.x
        }
        if(this.pressingLeft){
            this.position.x -= this.velocity.x
        }
        if(this.pressingUp){
            this.position.y -= this.velocity.y
        }
        if(this.pressingDown){
            this.position.y += this.velocity.y
        }
        if(this.isDashing){
            this.dash()
        }
         
        playerBoundaryCollisionDetection()
        playerConsumableCollisionDetection()
    }```
#

I have been stuck on this for a while it would be awesome if someone could help me.

hardy helm
#

Do you have a GitHub link or link with all the files?

quaint basin