Game Development Community

Attaching an object to path while retaining rotation ability

by Thomas Steffen · in iTorque 2D · 07/07/2012 (10:53 am) · 0 replies

Hello, I have an object which can turn and shoot when you click/drag the mouse, which will represent the touch screen feature.

I am trying to attach my object to a path, no problem. I would like it so it can move along the path and rotate and fire where the user touches/clicks. However, once the object is attached to the path, it wants to only be rotated in the direction the path is leading it. How can I tell this object to stop being rotated in that direction and to go back to being able to rotate and stay rotated where the mouse touch is?

Here is the code I am using for the rotate without being on a path:

if (!isObject(FaceTouchBehavior))
{
   %template = new BehaviorTemplate(FaceTouchBehavior);
   
   %template.friendlyName = "Face Touch";
   %template.behaviorType = "Input";
   %template.description  = "Set the object to always face the touch";
   
   %template.addBehaviorField(turnSpeed, "The speed to rotate at (degrees per second). Use 0 to snap", float, 0.0);
   %template.addBehaviorField(rotationOffset, "The rotation offset (degrees)", float, 0.0);
}

function FaceTouchBehavior::onBehaviorAdd(%this)
{
   %this.touchID = "";
   %this.owner.setUseMouseEvents(true);
}

function FaceTouchBehavior::onAddToScene(%this, %scenegraph)
{
   %this.owner.setMouseLocked(true);
}

function FaceTouchBehavior::onTouchDown(%this, %touchID, %worldPos)
{
   if (%this.touchID $= "")
      %this.touchID = %touchID;
}

function FaceTouchBehavior::onTouchMoved(%this, %touchID, %worldPos)
{
   if (%touchID !$= %this.touchID)
      return;

   %vector = t2dVectorSub(%worldPos, %this.owner.position);
   %targetRotation = mRadToDeg(mAtan(%vector.y, %vector.x)) + %this.rotationOffset;
   
   if (%this.turnSpeed == 0)
      %this.owner.setRotation(%targetRotation);
   else
      %this.owner.rotateTo(%targetRotation, %this.turnSpeed, true, false, true, 0.1);
}

function FaceTouchBehavior::onTouchDragged(%this, %touchID, %worldPos)
{
   if (%touchID !$= %this.touchID)
      return;
      
   %this.onTouchMoved(%touchID, %worldPos);
}

function FaceTouchBehavior::onTouchUp(%this, %touchID, %worldPos)
{
   if (%this.touchID $= %touchID)
      %this.touchID = "";
}


Here is the code for being attached to the path:

if (!isObject(AttachPathBehaviour))
{
   %template = new BehaviorTemplate(AttachPathBehaviour);
   
   %template.friendlyName = "Attach Path";
   %template.behaviorType = "AI";
   %template.description  = "Attach an object to a path";
   
   %template.addBehaviorField(path, "The path", object, "", t2dPath);
   %template.addBehaviorField(speed, "The speed at which the object will move along the path", float, "1.0");
   %template.addBehaviorField(direction, "1 to specify forward movement, -1 to specify reverse movement", integer, "1");
   %template.addBehaviorField(startnode, "The node that the object starts at (0=default)", integer, "0");
   %template.addBehaviorField(endnode, "The node that the object ends at (0=default)", integer, "0");
   %followM = "WRAP" TAB "REVERSE" TAB "RESTART";
   %template.addBehaviorField(followmode, "The action to take upon reaching the end of the path", enum, "WRAP", %followM);
   %template.addBehaviorField(loops, "The number of loops to take around the path (0=unlimited)", integer, "0");
   %template.addBehaviorField(sendtostart, "True to send the object directly to the start node, false to have the object move there", bool, "0");
}


function AttachPathBehaviour::onLevelLoaded(%this, %scenegraph)
{
   if (isObject(%this.path))
      %this.path.attachObject(%this.owner, %this.speed, %this.direction, %this.startnode, %this.endnode, %this.followmode, %this.loops, %this.sendtostart);
}

I'm pretty sure I need to modify the AttachPath behavior, but what would I add or change to make it work?
Any help will be greatly appreciated, I'm pretty lost with modifying it right now.

Thanks in advanced!
-Thomas