Jump to content

Make Sprite follow Cursor including rotating it, but in steps


bergben
 Share

Recommended Posts

Hey guys!

I am trying to find the best way to implement the following:

I want to create something like an .io game like slither.io and I want nearly the same movement behavior like there using Matter Physics.

 So the most basic way would be:

        let angle = Phaser.Math.Angle.Between(fromX, fromY, toX, toY);
        this.gameObject.setRotation(angle);
        this.physics.moveTo(this.gameObject, toX, toY, gameConfigs.movementSpeed);

but this results in a few problems. The gameObject (player) should ne be able to rotate immediately, there should be a rotation split into a few steps to create some sort of a maximal rotation speed. This should also make a 180° turn impossible. There should always be a maximal speed in which turning is possible. 

Now to do this I thought of the following: 

 1. On mouse move save the current cursor position as the current "goal" 
 2. rotate towards that "goal" in every update cycle like 2 degrees at a time
 3. move the gameObject forward at a constant speed

Now this would include a lot of calculations for the angle in every update cycle, so I wanted to ask if any of you can think of a better, cleaner solution? Performance is key...

Thanks!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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