Need Help with Shaking an Object
by Chad Kilgore · in Torque Game Builder · 04/19/2009 (6:59 am) · 1 replies
I am trying to create a vertical space shooter that allows the player's ship to loose their engines. When the player looses his engines, the ship jiggles around the player's location.
How I got this effect to work was to have a PlayerDummy (a t2dSceneObject representing the player's game location) and a PlayerImage (a t2dStaticSprite representing the player's image). The PlayerDummy has the ShooterControlsBehavior attached to it; while the PlayerImage has the TakesDamageBehavior and ShootsBehavior attached to it. Additionally, the PlayerImage has the following SoftMountObjectBehavior attached to it:
This behavior allows the PlayerImage to move the PlayerDummy. For the most part this system works pretty well. The ship jiggles when I increase the behaviors intensity. However, any object mounted to the PlayerImage seems to take an additional frame to update to their appropriate position, and it looks really bad. Does anybody have any suggestions on how to create this same effect without this issue?
How I got this effect to work was to have a PlayerDummy (a t2dSceneObject representing the player's game location) and a PlayerImage (a t2dStaticSprite representing the player's image). The PlayerDummy has the ShooterControlsBehavior attached to it; while the PlayerImage has the TakesDamageBehavior and ShootsBehavior attached to it. Additionally, the PlayerImage has the following SoftMountObjectBehavior attached to it:
if (!isObject(SoftMountObjectBehavior))
{
%template = new BehaviorTemplate(SoftMountObjectBehavior);
%template.friendlyName = "Soft Mount Object";
%template.behaviorType = "Game";
%template.description = "Soft mounts an object";
%template.addBehaviorField(object, "The object to soft mount", "", t2dSceneObject);
%template.addBehaviorField(intensity, "The intensity of the shake (unit)", float, 0.0);
}
function SoftMountObjectBehavior::onBehaviorAdd(%this)
{
%this.owner.enableUpdateCallback();
}
function SoftMountObjectBehavior::onUpdate(%this)
{
%this.owner.setPosition(%this.object.getPosition());
if (0.0 == %this.intensity)
return;
%distance = getRandom() * %this.intensity;
%vector = ((getRandom() * 2) - 1) SPC ((getRandom() * 2) - 1);
%vector = VectorScale(VectorNormalize(%vector), %distance);
%this.owner.setPositionX(
%this.owner.getPositionX() + getWords(%vector, 0, 1)
);
%this.owner.setPositionY(
%this.owner.getPositionY() + getWords(%vector, 1, 1)
);
}This behavior allows the PlayerImage to move the PlayerDummy. For the most part this system works pretty well. The ship jiggles when I increase the behaviors intensity. However, any object mounted to the PlayerImage seems to take an additional frame to update to their appropriate position, and it looks really bad. Does anybody have any suggestions on how to create this same effect without this issue?
Torque 3D Owner Chad Kilgore
//----------------------------------------------------------------------------- // Torque Game Builder // Copyright (C) GarageGames.com, Inc. //----------------------------------------------------------------------------- if (!isObject(SoftMountObjectBehavior)) { %template = new BehaviorTemplate(SoftMountObjectBehavior); %template.friendlyName = "Soft Mount Object"; %template.behaviorType = "Game"; %template.description = "Soft mounts an object"; %template.addBehaviorField(object, "The object to soft mount", "", t2dSceneObject); %template.addBehaviorField(intensity, "The intensity of the shake (unit)", float, 0.0); } function SoftMountObjectBehavior::onBehaviorAdd(%this) { %this.owner.enableUpdateCallback(); } function SoftMountObjectBehavior::onUpdate(%this) { %position = %this.owner.getPosition(); %this.owner.setPosition(%this.object.getPosition()); if (0.0 != %this.intensity) { %distance = getRandom() * %this.intensity; %vector = ((getRandom() * 2) - 1) SPC ((getRandom() * 2) - 1); %vector = VectorScale(VectorNormalize(%vector), %distance); %this.owner.setPositionX( %this.owner.getPositionX() + getWord(%vector, 0) ); %this.owner.setPositionY( %this.owner.getPositionY() + getWord(%vector, 1) ); } %position = VectorSub(%this.owner.getPosition(), %position); %list = %this.owner.getAllMountedChildren(); %count = getWordCount(%list); for (%i = 0; %i < %count; %i++) { %child = getWord(%list, %i); %vector = VectorAdd(%child.getPosition(), %position); %child.setPosition( getWord(%vector, 0), getWord(%vector, 1) ); } }I would love to hear what people have to say about this solution. But at least it works. :)