6r4c3
6r4c3

Reputation: 1

Values between objects are different but behavior suggests they are all the same

I have a number of objects in a browser game I'm working on, each object is a particle and is given a random velocity upon creation, however when playing, all particles move in the same direction as a group, I have double and triple checked that, yes, all the values are indeed set correctly, and each particle has its own random velocity. Yet all particles still move in the same direction, I'm totally stumped.

Update method for the particle class (runs every frame)

    update(){
        this.lifespan += -20
        if(this.lifespan <= 0) this.del_me = true

        this.pos.x += this.velocity.x
        this.pos.y += this.velocity.y

        this.draw()
        }

Constructor for particle class

    constructor({ pos, velocity, color, lifespan }){
        this.pos = pos
        this.velocity = velocity
        this.size = {w: 5, h: 5}
        this.color = color
        this.start_lifespan = lifespan
        this.lifespan = this.start_lifespan
        this.del_me = false
        }

Function for spawning a particle

function spawnParticle(pos, velocity, color, lifespan){
    objects.push(
        new Particle({
            pos: pos,
            velocity: velocity,
            color: color,
            lifespan: lifespan
            })
        )
    }

Function for confetti effect

function splat(pos, particles, color, lifespan){
    for(let i=0; i<particles; i++)
        spawnParticle(pos, {x: random_int(10)/10, y: random_int(10)/10}, color, lifespan)
    console.log(objects)
    }

(console output indicates that all velocities are indeed different and random, including negative values)

Originally I thought the problem was that my random function was only returning positive values, which would lead to all particles going to the bottom right of the screen, however I fixed this problem and they continued to behave like this. Even more confusing is that I tried setting a random velocity every frame in the above "update" method and the particles dispersed more yet still moved as a "cloud" instead of randomly spreading out like expected.

Upvotes: -3

Views: 36

Answers (0)

Related Questions