Jump to content

Movement and speed


NoxBrutalis
 Share

Recommended Posts

hey all, 

when i I use physics.moveTo method, collidisions work as expected, however I can't control the speed of the movement as such. Here's the code I use to move using moveTo:

 

this.move = function(delta)
    {
        let x = this.sprite.x;
        let y = this.sprite.y;
        let xR = Math.sin(this.sprite.rotation) * this.speed;
        let yR = -Math.cos(this.sprite.rotation) * this.speed;
        let pos = {x: (x+xR), y: (y+yR)};
        this.scene.physics.moveTo(this.sprite,pos.x, pos.y);
    }

This works how I want, except that the *speed part seems to have no impact on the movement. Which is undesirable because i want the player to have a different speed to enemies. When I use this code (the same as I implemented it in a different game with SFML):

 

this.move = function(delta)
    {
        let x = this.sprite.x;
        let y = this.sprite.y;
        let xR = Math.sin(this.sprite.rotation) * this.speed;
        let yR = -Math.cos(this.sprite.rotation) * this.speed;
        let pos = {x: (x+xR), y: (y+yR)};
        this.sprite.setPosition(pos.x, pos.y);
        
    }

The speed is taken into account - hurray, except the the collisions no longer work. I'm guessing this is because setPosition doesn't check for collisions.

So is there either: a) a way to write the code the first way, using moveTo where the speed variable works as intended?, or b) a way to check if the pos variable collides before applying it to the sprite? 

Thanks in advance for any help - it's much appreciated :)

Link to comment
Share on other sites

I found a method for doing collision on the fly - I assume? But it's not detecting any collisions still (this is using the setPosition style of movement):


 

this.move = function(delta)
    {
        var x = this.sprite.x;
        var y = this.sprite.y;
        var xR = Math.sin(this.sprite.rotation) * this.speed;
        var yR = -Math.cos(this.sprite.rotation) * this.speed;
        var pos = {x: (x+xR), y: (y+yR)};
        this.sprite.setPosition(pos.x, pos.y);
        if(this.scene.physics.collide(this.sprite, this.scene.layer5))
        {
            console.log('detected collision');
            this.sprite.setPosition(x, y);
        }
        //this.scene.physics.moveTo(this.sprite,pos.x, pos.y);
    }

..

Link to comment
Share on other sites

Ideally on it's rotation and speed. Like I say, I was originally using the moveTo method and with that form of movement collision detection works, etc. But when I do it that way, the speed multiplier has no effect.
 

and this is how I get the angle to the target - mouse pointer in this case. Just for completeness:

 

let angle = Phaser.Math.Angle.Between(this.sprite.x, this.sprite.y, target.sprite.x, target.sprite.y);

 

Link to comment
Share on other sites

This simple answer was plain for me to see the whole time. I wasn't sure before where to find the moveTo in the docs because there's a whole moveTo class, and I forgot that physics.moveTo in my case physics is arcade physics.

anyway, I looked at arcade physics class in the docs and there is a speed param that I simply didn't know was there.

This is how the working function looks:
 

this.move = function(delta)
{
    var x = this.sprite.x;
    var y = this.sprite.y;
    var xR = Math.sin(this.sprite.rotation);
    var yR = -Math.cos(this.sprite.rotation);
    var pos = {x: (x+xR), y: (y+yR)};
    this.scene.physics.moveTo(this.sprite,pos.x, pos.y, this.speed);
}

 

Link to comment
Share on other sites

Yes, you're right and in fact I understand your earlier question more clearly now. The moveTo function actually moves you to a position in a way that would be useful for point and click, but I need to use velocity instead, because Im updating the coordinates for sprites constantly in real time - if that makes sense. So yes, upon further reading the arcade physics class I noticed velocity from rotation. Somthing about it feel kinda off, like the rotation is pointed to the top left corner of the cursor rather than the cursors origin or something.

Still , thanks for the help Samme. We can count this as solved.  

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...