kozy Posted May 13, 2015 Share Posted May 13, 2015 Is it possible to change the default 0 point of a sprite's rotation? So when a sprite rotates to say 135*, 135* becomes the new 0 point? Help is greatly appreciated! Quote Link to comment Share on other sites More sharing options...
xerver Posted May 13, 2015 Share Posted May 13, 2015 Not sure what you are trying to accomplish by doing that. The direct answer to your question is "no", but that doesn't mean you can't achieve whatever it is you are trying to achieve. Quote Link to comment Share on other sites More sharing options...
kozy Posted May 13, 2015 Author Share Posted May 13, 2015 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.. Quote Link to comment Share on other sites More sharing options...
xerver Posted May 13, 2015 Share Posted May 13, 2015 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. Quote Link to comment Share on other sites More sharing options...
kozy Posted May 14, 2015 Author Share Posted May 14, 2015 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(); Quote Link to comment Share on other sites More sharing options...
kozy Posted June 14, 2015 Author Share Posted June 14, 2015 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.