Jump to content

Rotating a sprite towards the pointer


Kristoffer Darj
 Share

Recommended Posts

Hello.
 
I'm trying to follow http://gamemechanicexplorer.com/#homingmissiles-1 but my missiles end up pointing in the wrong direction all the time. 
 
So I simplified and removed basically everything but the rotation of the bullet. But even with a centered stationary bullet it ends up facing in another direction than where the pointer is.
 

Missile.prototype.update = function() {    var targetAngle = this.game.math.angleBetween(        this.x, this.y,        this.game.input.activePointer.x, this.game.input.activePointer.y    );    this.rotation = targetAngle;}; 

I'm using 2.0.3
Any thoughts on what might be wrong here?

Link to comment
Share on other sites

Yeah, I had the same results when messing with that code. It works in the version he is using: 2.0.3 with some patches from 2.0.4, but not in just the plain 2.0.3. As for the difference between the two... I have no idea what is going on there. My guess is it has something to do with calculating the mouse position with respect to the offset of the canvas element, but I'm not sure. Let me know if you find a solution, or just use that patched version ;)

Link to comment
Share on other sites

  • 4 weeks later...

Hello.

 

I'm trying to follow http://gamemechanicexplorer.com/#homingmissiles-1 but my missiles end up pointing in the wrong direction all the time. 

 

So I simplified and removed basically everything but the rotation of the bullet. But even with a centered stationary bullet it ends up facing in another direction than where the pointer is.

 

Missile.prototype.update = function() {    var targetAngle = this.game.math.angleBetween(        this.x, this.y,        this.game.input.activePointer.x, this.game.input.activePointer.y    );    this.rotation = targetAngle;}; 

I'm using 2.0.3

Any thoughts on what might be wrong here?

 

I ran into the same issue with this, but not from the bullet example but the lightning example.

 

I ended up using physics.arcade.angleToPointer(this.object) it basically just performs a Math.atan2(y1-y2, x1-x2).

 

This seems to be the same thing that math.angleBetween is doing, but maybe just a version mismatch.

Link to comment
Share on other sites

  • 4 weeks later...

I am running into an issue in that my sprites are built vertically but the rotation angle is calculated from the right side it seems.  The example of rotating toward pointer uses an arrow sprite that is pointing to the right so that the calculation is correct.   Am I missing something simple or is there a way to tell it to rotate starting from the top (while anchored in the center)? Or is the only solution to offset the calculation by 90 degrees?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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