Jump to content

change zero point of sprite rotation


kozy
 Share

Recommended Posts

My problem is that I'm trying to rotate a sprite from one angle to the next without generating obtuse angles based on the sprite's current angle (i.e. from 180* to -270*). This causes giant rotations sometimes when my sprite moves from one tile to the next, though just in one quadrant of the 2d plane, I've found.

 

 

Having the sprite's rotation reset to 0 when the sprite stops would fix this I think, as the angle I generate to the next direction would be much less.

 

I'm experimenting with adding http://greensock.com/DirectionalRotationPlugin to my onUpdateCallback function and might be getting somewhere..

Link to comment
Share on other sites

I would need to see code details, but I don't think "resetting to zero" would change anything at all if you are using deltas already.

 

Like you should be adding/subtracting from the angle "sprite.rotation+=Math.PI;" is going to be the same amount of rotation regardless of what the number of the rotation of the sprite is currently.

Link to comment
Share on other sites

My chainedTween is set in the for loop but the "angle: degs" rotates to degree, not /by/ degree.

 

So I'm trying to figure out how to set my chainedTween where I can actually use "sprite.angle += turnRate" to change the heading of the sprite on the fly

 

I'm pretty sure I need to set a Timeout for each iteration in the loop so the tween can run inside the loop while it appears on the canvas. This will give the chainedTween.onUpdateCallback access to those variables inside the loop

 

 

    var chainedTween = game.add.tween(sprite);    var animationTime = 500; //higher = slower    var sourceAngle = sprite.angle;    var targetAngle;    var degs;    var turnRate;    //attach    for (var i = 1; i < steps.length; i++) { //skip first starting tile        sourceAngle = steps[i].degs;        targetAngle = ((i < steps.length - 1) ? steps[i + 1].degs : sourceAngle);        degs = turnByDegs(sourceAngle, targetAngle);        chainedTween.to({            x: steps[i].x,            y: steps[i].y,            //angle: degs        }, animationTime, Phaser.Easing.Linear.None);        turnRate = degs/animationTime;    }    chainedTween.onUpdateCallback(function () {        console.log("update");        sprite.angle += turnRate;    });chainedTween.start();
Link to comment
Share on other sites

  • 1 month later...

I figured it out! Only took a month or two of free time! And stackoverflow!

 

Here's the crux of the code:

if (Math.abs(degs - prevAngle) > 180) { //find out if target angle (degs) is < or > 180*    var turnSide = findTurnSide(prevAngle, degs); //find which way to turn    var shortAngle = getDifference(prevAngle,degs)*turnSide; //get the shortest angle between prev and targ points on a circle and multiply by turnside (+ or -)    var newTargetAngle = prevAngle + shortAngle; //calc new target angle    steps[i].degs = newTargetAngle; //set angle in array for chained tween}

before this code I am pushing an x and y and an angle to rotate to (degs) into steps - the x and y come from a pathfinding route array, which I can calculate the angle to the next tile to get degs.

 

Hope this helps someone!

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