I
I
Inter Carpenter2016-01-07 21:43:15
JavaScript
Inter Carpenter, 2016-01-07 21:43:15

How to implement the movement of the player behind the mouse cursor?

Prompt in what direction to think.
I do not know how to implement the movement of the player behind the mouse cursor
The game is written using Canvas in JS, the size of the playing field is 100 * 100 cells of the array that contains objects.
A screen of 20 * 20 objects is unloaded to the player.
When moving up, down, right, left, I simply load a piece of the map relative to the starting point
. Thus, the player is always in the center of the screen, and the map moves.
But you need a control in which the player moves behind the mouse pointer, the speed is constant, when the pointer is hovered over the player, there is no movement.
I catch the position of the mouse inside the Canvas through onmousemove and the values ​​layerX, layerY
As I understand it, it is necessary to calculate the angle relative to the position of the mouse and the center of the canvas block, but I won’t know what to do with it longer

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Dovnar, 2016-01-07
@SolidlSnake

Read it, good stuff though basics.

A
Alec Onim, 2016-01-11
@Caitiff_One

Pythagorean theorem to help you :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question