How to rotate and move a sprite towards the cursor? - javascript

Lets say that I have a player sprite, located at (player.x, player.y). On a mousemove event, I am capturing the cursor's position, which is (e.pageX, e.pageY). In the game, the player will be constantly moving, always towards the direction of the cursor. I am trying to find a way to to rotate the player sprite to always face the mouse cursor, and also move 10 pixels closer to the cursor every game tick. So far, this has me stumped. I have seen many examples of this online, and have tried all of them, but none seem to work. Any help that can be provided would be very much appreciated.

First of all, you must learn the basics of trigonometry. Without this knowledge, you will can't make something like you want.
Center of you image is the center of an circle. Knowing position of center and mouse position, you can calculate the angle. Calculated angle you can use to rotate image.

Related

how to trigger a function whenever the mouse on screen

I'm being stuck in a problem
I'm creating something like this: https://www.fariasviolins.com/
My English is not good :3
Specifically, I'm creating a infinity layout, the layout will be moved according to the mouse position(The larger the position, the greater the speed), it means that when the mouse to directions, the layout will be moved. I have tried mousemove and it worked, but I also want the layout still move whenever the mouse on screen(even when the mouse not moving, the the speed will calculate by the position of the mouse), I have tried mouseover, but it didn't work as I expected. please help me huhuhuhhu, thank you so much
I think mousemove is the right way to go!
On the example website you mentioned, it looks like the speed depends on the distance between your mouse and the center of the page.
If you calculate the speed that way, the speed shouldn't change if you don't move your mouse. (Because the distance between your mouse and the center of the page doesn't increase either.)

threejs TrackballControls - Rotate around picked point & Zoom in direction of mouse

I am using threejs (r73). I use a PerspectiveCamera. My goal is to implement mouse interaction. For that purpose I want to use
TrackballControls.js . But I want a slightly different behaviour.
I want to rotate around the point that the user picked on the screen
I want to zoom in direction of the mouse position
For the second point I already found a "solution" at stackoverflow. The zoom works, but when I change the target vector of the control, panning and rotating does not work any longer.
Can anyone provide such an modified implementation of TrackballControls or help me with that?
EDIT
With the applied "solution" panning still works but rotating doesn't.
The idea from https://stackoverflow.com/a/16817727/2657179 also does not work.

How to detect mouseenter/mouseleave event on lots of items without slow-down

I have realy specific question about Canvas handling mouse-events.
I'm working on a isometric game, I have a displayed map with all tiles, and I want know on which one the mouse are.
On a basic isometric map it's easy to transform the position on the screen (blue on the next image) on a map position (orange) with basic affine function (ax+b, with 'a' is width_tile/height_tile and b is the current position of the map view, red line on the image)..
But I have a complication, each tile of the game have a specific elevation (displayed by red arrow on second image). So i can't use a function for each line(y)/column(x) of the map.
On the same technique I'll try to calculate if mouse position are on EACH tile one by one for EACH mouse event (move, click, ..) but I'm afraid for the heavy code : if I have a 100x100 map and I shack the mouse, I'm sure all this test will ruin the client browser..
I realy don't know how can I do it better ?!
Someone have an idea, or a tips to optimize this check ?

Background sprites that respond to cursor movement

I'm attempting to create an effect that moves image sprites in a certain direction depending on the direction the mouse cursor is moving. I've looked into parallax scrolling but couldn't find much help. Any ideas on where I can find some more info or tutorials on objects relating to cursor movement?
For a better idea of what I'm talking about go to http://web-features.net/ and create a new layer that responds to mouse move.
Thanks
Google is your friend. This is the only tutorial I could find specific to your problem, but I'm sure there are many more tutorials about doing things with mouse position in javascript.
I don't know of any tutorials but check out the answer to this post :
Rotating an element based on cursor position in a separate element
That will get you started. Instead of rotating an element you can make changes to the background position.
Basically you want to imagine a right triangle where one end of the hypotenuse is the center of your element and the other is your cursor.
Using javascripts Math.atan2() you can pass in the y,x positions and it returns the angle in radians.
Is this enough info to get you going?
///////// Update /////////
Heres a demo I made for you also using css3 transitions : http://codepen.io/jeffpowersd/pen/ryBJz
This helped me!
http://www.w3schools.com/css/css_image_sprites.asp
Hope this helps!
Also note, that when moving right on the image sprite, the css direction is negative. For example,
#prev{background:url('img_navsprites_hover.gif') -47px 0;}
#prev a:hover{background: url('img_navsprites_hover.gif') -47px -45px;}
On the hover, the image is moved 47px right and 45px down.

Javascript rotate image with mouse coordinates

Im using a parallax effect that moves a ball according to the users mouse position (http://webdev.stephband.info/jparallax/index.html). I am trying to figure out a way that the ball will also rotate. So it would appear that as the user moves their mouse over the area, the ball rolls across the screen.
I have found some javascript examples that show how to rotate an item on mouse click, I am just not skiled enough in js to put it all together.
here is a Jsfiddle that shows an example:
jsFiddle
This is more tangential advice than an answer, but in order to make the rolling ball look realistic, make sure that it rotates at a rate that is consistent with the ball's circumference. Otherwise it will look fakey.
This jQuery plugin might be useful.

Categories

Resources