Jump to content

How to move a DisplayObject left/right taking into account its direction?


badatmath
 Share

Recommended Posts

Here is most of the code to get the point across...

// init time

const direction = new Vector2();
const speed = 5;

// render time

direction.x = mouseX - window.innerWidth / 2;
direction.y = mouseY - window.innerHeight / 2;
direction.normalize();

const deltaY = direction.y;
const deltaX = direction.x;
const angle = Math.atan2(deltaY, deltaX);

player.rotation = angle + 90 * Math.PI / 180;

if (forward) {
  player.position.x += direction.x * speed;
  player.position.y += direction.y * speed;
}

if (backward) {
  player.position.x -= direction.x * speed;
  player.position.y -= direction.y * speed;
}

if (left) {
  // how?
}

if (right) {
  // how?
}

 

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...