Rotating an object
by Andrea Farid Marsili · in iTorque 2D · 04/28/2011 (1:01 pm) · 11 replies
Hi. I need to rotate an object using touch position.
I managed to do it with TGB but the SAME code doesn't work on iTGB so I need some advice about how to rotate object using touch.
I managed to do it with TGB but the SAME code doesn't work on iTGB so I need some advice about how to rotate object using touch.
#2
Obviously the iOS version of the code is a bit different but the logic is exactly the same.
04/29/2011 (6:13 am)
if (!isObject(MouseRotableBehavior)){
%template = new BehaviorTemplate(MouseRotableBehavior);
%template.friendlyName = "Mouse Rotable";
%template.behaviorType = "Mouse Input";
%template.description = "Rotate an actor using the mouse";
}
function MouseRotableBehavior::onBehaviorAdd(%this){
%this.owner.setUseMouseEvents(true);
$initRotation = %this.owner.getRotation();
$rotPosX = %this.owner.getPositionX();
$rotPosY = %this.owner.getPositionY();
$initAngle = mAtan($mousePositionY - $rotPosY, $mousePositionX - $rotPosX);
}
function MouseRotableBehavior::onMouseDragged(%this, %modifier, %worldPosition, %clicks){
$mousePositionX = getWord(%worldPosition, 0);
$mousePositionY = getWord(%worldPosition, 1);
%myAngle = mAtan($mousePositionY - $rotPosY, $mousePositionX - $rotPosX);
%this.owner.setRotation(($initRotation + (%myAngle - $initAngle)) * 45);
}Obviously the iOS version of the code is a bit different but the logic is exactly the same.
#3
If you want to use functions like you would on the PC you can take a look at my multi-touch patch:
http://www.garagegames.com/community/forums/viewthread/125394
You should be able to use your existing code with this but you'll just have to change onMouseDragged to say onMouseMove. Having said this I'm yet to test the code on behaviours but I don't see why it wouldn't work.
04/29/2011 (6:18 am)
OK on the iPhone you have to use:function oniPhoneTouchMove( %touchCount, %touchX, %touchY )
{
}Which is in screen space and not attached to a specific object.If you want to use functions like you would on the PC you can take a look at my multi-touch patch:
http://www.garagegames.com/community/forums/viewthread/125394
You should be able to use your existing code with this but you'll just have to change onMouseDragged to say onMouseMove. Having said this I'm yet to test the code on behaviours but I don't see why it wouldn't work.
#4
04/29/2011 (6:42 am)
I already change onMouseDragged with oniPhoneTouchMove but it doesn't worked. I'll post the iOS code when I'll be at home.
#5
04/29/2011 (7:07 am)
You may have to make sure you ticked the Enable MultiTouch option.
#6
PS: I replace onMouseDragged with onMouseMove
04/29/2011 (1:57 pm)
Enable MultiTouch option is ticked now and my script is working but only on the simulator...PS: I replace onMouseDragged with onMouseMove
#7
Here the code:
04/29/2011 (2:54 pm)
Now I replaced onMouseMove with oniPhoneTouchMove this is the new code but there're problems.Here the code:
function rotable::onLevelLoaded(%this, %scenegraph){
$initRotation = %this.getRotation();
$rotPosX = %this.getPositionX();
$rotPosY = %this.getPositionY();
$initAngle = mAtan($mousePositionY - $rotPosY, $mousePositionX - $rotPosX);
}
function oniPhoneTouchMove(%touchCount, %touchX, %touchY){
$mousePositionX = getWord(%touchX, 0);
$mousePositionY = getWord(%touchY, 0);
%myAngle = mAtan($mousePositionY - $rotPosY, $mousePositionX - $rotPosX);
rotable.setRotation(($initRotation + (%myAngle - $initAngle)) * 45);
echo("Mouse Position X: " @ $mousePositionX);
echo("Mouse Position Y: " @ $mousePositionY);
}
#8
Only one direction. I'm trying to code a behavior version now...
EDIT: I don't think that a behavior version will be possible because onMouseMove callback doesn't work on iTGB
04/30/2011 (2:06 am)
And finally it's working.function rotable::onMouseDown(%this, %modifier, %worldPosition, %clicks){
rotable.rotating = true;
$initRotation = %this.getRotation();
$rotPosX = %this.getPositionX();
$rotPosY = %this.getPositionY();
$initAngle = mAtan($mousePositionY - $rotPosY, $mousePositionX - $rotPosX);
echo("Init Angle: " @ $initAngle);
}
function rotable::onMouseUp(%this, %modifier, %worldPosition, %clicks){
rotable.rotating = false;
}
function oniPhoneTouchMove(%touchCount, %touchX, %touchY){
if(rotable.rotating){
$mousePositionX = getWord(%touchX, 0);
$mousePositionY = getWord(%touchY, 0);
%myAngle = mAtan($mousePositionY - $rotPosY, $mousePositionX - $rotPosX);
rotable.setRotation(($initRotation - (%myAngle + $initAngle)));
echo("Mouse Position X: " @ $mousePositionX);
echo("Mouse Position Y: " @ $mousePositionY);
echo("Rotable Angle: " @ rotable.getRotation());
}
}Only one direction. I'm trying to code a behavior version now...
EDIT: I don't think that a behavior version will be possible because onMouseMove callback doesn't work on iTGB
#9
You can most likely create a behaviour version using the multi-touch patch I linked above.
Cheers
04/30/2011 (1:41 pm)
I'm glad that's sorted.You can most likely create a behaviour version using the multi-touch patch I linked above.
Cheers
#10
05/01/2011 (12:22 am)
I have to recompile the engine?
#11
05/01/2011 (6:08 am)
Yes, though this is a required step to build the iPhone version anyway and all the preconfigured projects are present in the buildFiles folder of your individual project.
Torque 3D Owner Alistair Lowe3