Question About Rotating To An Object
by Orion the Hunter · in Torque Game Builder · 04/03/2013 (7:43 am) · 10 replies
Hello, I downloaded from TDN this code:
It works pretty well, but the problem is it rotates to the object once, then it doesn't rotate again. Do you have any idea of how to make it keep rotating to the object through an update callback or something?
What I have in mind is in a complex level for my platformer, the waypoint will rotate towards an invisible object which marks the way out so the player will know which way to go. ANY help is appreciated. Thanks!
//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
if (!isObject(FaceObjectBehavior))
{
%template = new BehaviorTemplate(FaceObjectBehavior);
%template.friendlyName = "Face Object";
%template.behaviorType = "AI";
%template.description = "Set the object to face another object";
%template.addBehaviorField(object, "The object to face", object, "", t2dSceneObject);
%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 FaceObjectBehavior::onBehaviorAdd(%this)
{
%this.owner.enableUpdateCallback();
}
function FaceObjectBehavior::onUpdate(%this)
{
if (!isObject(%this.object))
return;
%vector = t2dVectorSub(%this.object.position, %this.owner.position);
%targetRotation = mRadToDeg(mAtan(%vector.y, %vector.x)) + 90 + %this.rotationOffset;
if (%this.turnSpeed == 0)
%this.owner.setRotation(%targetRotation);
else
%this.owner.rotateTo(%targetRotation, %this.turnSpeed, true, false, true, 0.1);
}It works pretty well, but the problem is it rotates to the object once, then it doesn't rotate again. Do you have any idea of how to make it keep rotating to the object through an update callback or something?
What I have in mind is in a complex level for my platformer, the waypoint will rotate towards an invisible object which marks the way out so the player will know which way to go. ANY help is appreciated. Thanks!
#2
04/04/2013 (10:31 am)
Thanks so much! Okay, I realized what is wrong. The problem is that when it's mounted to the player, it won't rotate. Is there a way to make it so that when it's mounted to the player it can still preform its function?
#3
remove the mount, and add something like this to the BehaviorTemplate
and also add something like this to the bottom of FaceObjectBehavior::onUpdate(%this)
This is off the top of my head, so no promises that the syntax is correct :)
04/04/2013 (4:31 pm)
Hmm, well I'm not all that familiar with the Mount function, But you could simply update its position instead of mounting it.remove the mount, and add something like this to the BehaviorTemplate
%template.addBehaviorField(mountobject, "The object to mount", object, "", t2dSceneObject);
and also add something like this to the bottom of FaceObjectBehavior::onUpdate(%this)
%this.owner.setPositionX(%this.mountobject.position.x); %this.owner.setPositionY(%this.mountobject.position.y);
This is off the top of my head, so no promises that the syntax is correct :)
#4
Here's documentation on mounting: tdn.garagegames.com/wiki/TGB/Reference:_t2dSceneObject_Mounting_Methods
04/04/2013 (5:22 pm)
You should be able to mount to the player, just need to make sure that the %trackRotation is set to false.Here's documentation on mounting: tdn.garagegames.com/wiki/TGB/Reference:_t2dSceneObject_Mounting_Methods
#6
%object being your player, %offset probably 0, %force 0, %trackRotation 0, %sendToMount 1, %ownedByMount (depends.. but most likely 1), %inheritAttributes 0.. So in the end..
If your player "dies" and is respawned, the owned by Mount attribute will force your object to "die" as well, so it will need re-mounting.
04/05/2013 (3:21 pm)
Or...mount(%object, [%offset, [%force, [%trackRotation, [%sendToMount, [%ownedByMount, [%inheritAttributes]]]]]])
%object being your player, %offset probably 0, %force 0, %trackRotation 0, %sendToMount 1, %ownedByMount (depends.. but most likely 1), %inheritAttributes 0.. So in the end..
mount($Player, [0, [0, [0, [1, [1, [0]]]]]])
If your player "dies" and is respawned, the owned by Mount attribute will force your object to "die" as well, so it will need re-mounting.
#7
04/06/2013 (1:04 pm)
Sounds good! I'm planning on having a trigger with enter/leave callbacks so when you enter it will mount but when you leave it will be deleted. I'll post my code when I'm done.
#8
04/06/2013 (2:45 pm)
Great! It's working very well. Here's what I've got: //-----------------------------------------------------------------------------
// Platformer Starter Kit
// Copyright (C) Phillip O'Shea
//
// Area Damage - This behavior can be owned by either a Trigger or a Scene
// Object. If a player enters the trigger it will take damage,
// and will continue to do so each interval. If an actor
// collides with a scene object, then it will take damage upon
// collision.
//-----------------------------------------------------------------------------
if ( !isObject( CompassBehavior ) )
{
%template = new BehaviorTemplate( CompassBehavior );
%template.friendlyName = "Compass Area";
%template.behaviorType = "Miscellaneous";
%template.description = "Attach a compass to the player to point to a waypoint.";
%template.addBehaviorField( PlayerOnly, "Don't effect the other actors.", BOOL, true );
}
/// Set up the object
function CompassBehavior::onAddToScene( %this, %scenegraph )
{
if ( %this.PlayerOnly )
{
%this.Owner.setObjectType( "PlayerTrigger" );
%this.Owner.setCollidesWith( "PlayerObject" );
}
else
{
%this.Owner.setObjectType( "ActorTrigger" );
%this.Owner.setCollidesWith( "ActorObject" );
}
}
/// Attach compass!
function CompassBehavior::onEnter( %this, %theirObject )
{
%Compass = new t2dStaticSprite(Compass)
{
SceneGraph = Scenewindow2D.getSceneGraph();
imageMap = "CompasImageMap";
frame = "0";
sourceRect = "0 0 0 0";
canSaveDynamicFields = "1";
Position = %theirObject.position;
size = "50.000 50.000";
mountID = "332";
_behavior0 = "FaceObjectBehavior object Waypoint turnSpeed 90";
};
Compass.mount(%theirObject, 0, 0, 0, 1, 1, 0);
Compass.setMountTrackRotation(false);
}
function CompassBehavior::onLeave( %this, %theirObject )
{
Compass.safeDelete();
} You can get the FaceObjectBehavior off of TDN. One question--why is it that in my platform game when there's a trigger over all the platform objects, the walls don't receive collision?
#9
I'm not sure if there is a solution to your problem that would be easy.. Try adjustments, tinker around with settings, maybe there's a reason.
Hopefully you can find a way to make it work tho, I know it's frustating to make so much progress and then run up to a brick wall so to speak.
04/06/2013 (6:13 pm)
There have been several posts regarding problematic triggers that can sometimes interfere with Collision, or cause erratic effects. I'm not sure if there's any way to diagnose the problem, and with TGB not being supported, it's rather unlikely anyone other than those still developing with TGB will be able to help. I'm not sure if there is a solution to your problem that would be easy.. Try adjustments, tinker around with settings, maybe there's a reason.
Hopefully you can find a way to make it work tho, I know it's frustating to make so much progress and then run up to a brick wall so to speak.
#10
04/08/2013 (9:04 am)
Not a problem. I figured it out--I just had to have two triggers. One of them has it's leave callback disabled and the other one, the enter. When you enter, it attaches the compass if you don't already have one. When you leave, it deletes it. In the event of dying where it might not give you the compass back if you spawn between the two triggers, it was simple to add a function in the "onAddToScene" method of the Actor. Now, every time you respawn, it should check to see if you need a compass.
Torque Owner kc_0045
Default Studio Name
if I were to guess I would say somehow the
%this.owner.enableUpdateCallback();
is being disabled, or the
if (!isObject(%this.object))
return;
is stopping it from running?
Drag a sprite over to your scene and attach the Behavior to it, see if that works first.