Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
Zync

TweenMax plugin lookAt or rotateTo?

Recommended Posts

G'day Jack,

 

Just a quickie Q about TweenMax and the plugin system. Do you know if it's possible to make an addon to TweenMax which would allow an object to have it rotation tweened to look at or rotate to another object?

 

I know Math scares off a lot of people when it comes to getting a game up and running that's why I really want to make this tutorial set super simple. If you could point me in the right direction to making a plugin that would be great.

 

So instead of:

 

var dy:Number = gunTurret.y - stage.mouseY;
var dx:Number = gunTurret.x - stage.mouseX;
var angle:Number = Math.atan2(dy, dx) * 180 / Math.PI;
mcTurret.rotation = angle;

 

I could just use.

 

//ie:
TweenMax.to(gunTurret, 0.5, {lookAt:new Point(mouseX, mouseY)});
//.

 

 

Cheers

 

-Z

Link to comment
Share on other sites

Actually nvm. Did a little bit of digging around in the files and think I got it:

 

package com.greensock.plugins
{
import com.greensock.TweenLite;

/**
 * ...
 * @author Zync
 */
public class RotateToObject extends TweenPlugin
{
 /** @private **/
 public static const API:Number = 2; //If the API/Framework for plugins changes in the future, this number helps determine compatibility

 public function RotateToObject()
 {
  super("rotateToObject,rotation");
 }

 override public function _onInitTween(target:Object, value:*, tween:TweenLite):Boolean
 {
  var dy:Number = target.y - value.y
  var dx:Number = target.x - value.x
  var angle:Number = Math.atan2(dy, dx) * 180 / Math.PI;

_addTween(target, "rotation", target.rotation, angle-90);
  return true;
 }
}
}

RotateToObject.zip

  • Like 2
Link to comment
Share on other sites

Very cool, Zync! That looks like it would indeed rotate to the spot you wanted originally, but it would not follow the mouse (or whatever). So, for example, if the mouse is at x:200,y:100 and you started a 1-second tween but then moved your mouse to a different spot, the object would still end up looking at 200,100. You could, of course, use DynamicPropsPlugin to do something dynamic like that. But maybe the dynamic updating isn't a requirement for your project.

 

Thanks for sharing the plugin!

Link to comment
Share on other sites

Cheers mate and yeah that was the desired effect but I probably didn't explain it that well. It's for a missile command type-of game tutorial where the player clicks on a point on the stage, the turret rotates into position and when the tween completes it fires a missile.

 

But I will check out that dynamic props cos I do need another missile battery to always track the mouse. Thanks! Although would there be any harm in setting a 0 second tween in say a mousemove handler to always track a target exactly? Like:

 

stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);


private function handleMouseMove(e:MouseEvent):void 
{
TweenMax.to(this, 0, { rotateToObject:new Point(stage.mouseX, stage.mouseY) } );
}

Link to comment
Share on other sites

Although would there be any harm in setting a 0 second tween in say a mousemove handler to always track a target exactly? Like:

 

stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);


private function handleMouseMove(e:MouseEvent):void
{
TweenMax.to(this, 0, { rotateToObject:new Point(stage.mouseX, stage.mouseY) } );
}

No, there's no harm in doing something like that, but performance-wise it certainly isn't optimal.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×