Game Development Community

My camera is jittery.

by Skebrew · in Torque Game Builder · 12/01/2011 (8:02 am) · 5 replies

Hi. Does anyone know how to make a camera rigid when it's not mounted? Because the camera keeps shaking each
time it has to jump to another position. I tried using startCameraMove to move it incrementally, but my program calls for an update every 32 milliseconds so it moves just as fast. Also I tried moving the camera 1 extra space forward and that didn't work either because it still had to jump to far. So if anyone could tell me how to make a camera move parallel to an object rigidly without mounting it, it would save me SOOOOOOOOOO much trouble.

P.S. Any command that does that? Or script sequence?


Thanks in advance
Skebrew

By the way my code is:
if (!isObject(CameraFollowBehavior))
{
   %template = new BehaviorTemplate(CameraFollowBehavior);
   
   %template.friendlyName = "Camera follow";
   %template.behaviorType = "Camera";
   %template.description  = "Makes the camera follow a object, it also mounts the object. Also set's view limit off.";

    %template.addBehaviorField(objectToFollow, "The object that the camera follows", object, "", t2dSceneObject);
	%template.addBehaviorField(trackingForce, "The tracking force of the camera to the mount point. A value of 0 makes the mount instant.", Int, "0");
	%template.addBehaviorField(cameraWidth, "The Camera's width of viewing", Float, "100.00");
	%template.addBehaviorField(cameraHieght, "The Camera's cameraHieght of viewing.", Float, "75.00");
	//%template.addBehaviorField(cameraSpeed, "The time it takes the camera to update. In milliseconds (1000 = 1 second)", Integer, "1000");
	%template.addBehaviorField(cameraDistanceY, "The distance it takes the character to make the camera move on the Y axis(Up and down)", Float, "20.0");
	%template.addBehaviorField(cameraDistanceX, "The distance it takes the character to make the camera move on the X axis(left and right) Also you have to put it larger than Y for the same effect like 9 more", Float, "29.0");
	%template.addBehaviorField(cameraSpeed, "The speed the camera moves(Camera updates every 0.032 seconds)", Float, "0.3");
	%template.addBehaviorField(updateSpeed, "The speed that the camera updates(in miliseconds)", Integer, "500");
}

function CameraFollowBehavior::onBehaviorAdd(%this)
{
   sceneWindow2D.startCameraMove();
   //echo ("Behavior add finished");
   //%this.owner.enableUpdateCallback();
   sceneWindow2D.setTargetCameraPosition(0, 0, %this.cameraWidth, %this.cameraHieght);
   sceneWindow2D.startCameraMove();
}

function CameraFollowBehavior::onAddToScene(%this)
{
 
//echo ("onAddToScene finished");
%this.camPosY = 0.0;
%this.objPosY = 0.0;
%this.distanceY = 0.0;
%this.camPosX = 0.0;
%this.objPosX = 0.0;
%this.distanceX = 0.0;
%this.onUpdaten();
}

function CameraFollowBehavior::onUpdaten(%this, %scenegraph)
{
 %this.newY = 0.0;
 %this.newX = 0.0;
 %this.UpdateCameraY();
 %this.UpdateCameraX();
 //%this.CameraPanSpeed();
    
	/*if(%this.boul == 1)
	{
	echo("hi");
    echo("guy");
    echo("");
	%this.boul = 0;
	}
	else
	{
    echo("56");
    echo("42");
    echo("");
	%this.boul = 1;
	}*/
	
 sceneWindow2d.completeCameraMove();//Makes sure the move is complete before doing another.
 //sceneWindow2d.setCurrentCameraPosition(%this.newX, %this.newY, %this.cameraWidth, %this.cameraHieght);//Instant
 //sceneWindow2d.MoveCamera();
	
 sceneWindow2d.setTargetCameraPosition(%this.newX, %this.newY, %this.cameraWidth, %this.cameraHieght);
 sceneWindow2D.startCameraMove(%this.cameraSpeed);
 %this.schedule(%this.updateSpeed, "onUpdaten");
}

/*function CameraFollowBehavior::MoveCamera(%this)//Doesn't make smooth AT ALL!!!
{
%playerVelY = %this.objectToFollow.getLinearVelocityY();
%playerVelX = %this.objectToFollow.getLinearVelocityY();
%corectionAmntY = %this.distanceY - %this.cameraDistanceY;
%corectionAmntX = %this.distanceX - %this.cameraDistanceX;

%timeY = %playerVelY / %corectionAmntY;
%timeX = %playerVelX / %corectionAmntX;

   if(%timeX < %timeY)
   {
   %time = %timeY;
   }
   else
   {
   %time = %timeX;
   }
   
Positize(%time);
   
sceneWindow2d.setTargetCameraPosition(%this.newX, %this.newY, %this.cameraWidth, %this.cameraHieght);

sceneWindow2D.startCameraMove(%time);
}*/

function CameraFollowBehavior::UpdateCameraY(%this)//____________________________________________________START OF UPDATE Y_________________________________________________________________________
{
//%this.camPos = sceneWindow2d.getCurrentCameraPosition();//Incase you want a vector
%this.camPosY = GetCamPosY();//see CamFunctions gamescript
%this.camPosYOrg = GetCamPosY();//see CamFunctions gamescript
%this.objPosY = %this.owner.getPositionY();
%this.objPosYOrg = %this.owner.getPositionY();
%corectionAmnt = 0.0;
		
%this.distanceY = %this.camPosY - %this.objPosY;

	if(%this.distanceY < 0)//if negative make it not negative
	{
	%this.distanceY = %this.distanceY * -1;
	}
	
    if(%this.distanceY > %this.cameraDistanceY)//cameraDistanceY is normally 20
	{
	%corectionAmnt = %this.distanceY - %this.cameraDistanceY;
	
	   if(%this.camPosYOrg > %this.objPosYOrg)//Cam needs to go up. because up is negative on the Y axis on torque for some reason.
	   {
	      //echo(%this.camPosYOrg);
		  //echo(%this.distanceY);
          if(%this.camPosYOrg < 0 && %this.distanceY > 0)
          {          
		  %this.newY = %this.camPosYOrg - %corectionAmnt;
		  //echo(%this.newY);
          }
		  else if(%this.camPosYOrg >= 0 && %this.distanceY < 0)
		  {
		  %this.newY = %this.camPosYOrg + %corectionAmnt;
		  //echo(%this.newY);
		  }
          else if(%this.camPosYOrg >= 0 && %this.distanceY > 0)
          {		  
	      %this.newY = %this.camPosYOrg - %corectionAmnt;
		  //echo(%this.newY);
          }
          else if(%this.camPosYOrg < 0 && %this.distanceY < 0)//all these ifs makes the camera always on track no mater if it's positive or negative
          {		  
	      %this.newY = %this.camPosYOrg + %corectionAmnt;
		  //echo(%this.newY);
          }	
          else if(%this.distanceY == 0)
          {		  
	      %this.newY = %this.camPosYOrg;
		  //echo(%this.newY);
          } 		  
	   }
	   else//cam needs to go down
	   {
	      if(%this.camPosYOrg < 0 && %this.distanceY > 0)
          {          
		  %this.newY = %this.camPosYOrg + %corectionAmnt;
		  //echo(%this.newY);
          }
		  else if(%this.camPosYOrg >= 0 && %this.distanceY < 0)
		  {
		  %this.newY = %this.camPosYOrg - %corectionAmnt;
		  //echo(%this.newY);
		  }
          else if(%this.camPosYOrg >= 0 && %this.distanceY > 0)
          {		  
	      %this.newY = %this.camPosYOrg + %corectionAmnt;
		  //echo(%this.newY);
          }
          else if(%this.camPosYOrg < 0 && %this.distanceY < 0)
          {		  
	      %this.newY = %this.camPosYOrg - %corectionAmnt;
		  //echo(%this.newY);
          }	 
	   }
	}
	else
	{
	%this.newY = %this.camPosYOrg;//if it don't need to move
	}	
	
}//___________________________________________________________________________________________________________END OF UPDATE Y_________________________________________________________________________


function CameraFollowBehavior::UpdateCameraX(%this)//____________________________________________________START OF UPDATE X_________________________________________________________________________
{
//%this.camPos = sceneWindow2d.getCurrentCameraPosition();
%this.camPosX = GetCamPosX();//see CamFunctions gamescript because you can't acces this variable from a behavior
%this.camPosXOrg = GetCamPosX();//see CamFunctions gamescript because you can't acces this variable from a behavior
%this.objPosX = %this.owner.getPositionX();
%this.objPosXOrg = %this.owner.getPositionX();
%corectionAmnt = 0.0;
		

%this.distanceX = %this.camPosX - %this.objPosX;

	if(%this.distanceX < 0)//The absolute distance from the object and the camera.
	{
	%this.distanceX = %this.distanceX * -1;
	}
	//echo(%this.distanceX);
    if(%this.distanceX > %this.cameraDistanceX)//cameraDistanceX is normally 20
	{
	%corectionAmnt = %this.distanceX - %this.cameraDistanceX;
	   if(%this.camPosXOrg > %this.objPosXOrg)//Cam needs to go up. because up is negative on the X axis on torque for some reason.
	   {
	      //echo(%this.camPosXOrg);
		  //echo(%this.distanceX);
          if(%this.camPosXOrg < 0 && %this.distanceX > 0)
          {          
		  %this.newX = %this.camPosXOrg - %corectionAmnt;
		  //echo(%this.newX);
          }
		  else if(%this.camPosXOrg >= 0 && %this.distanceX < 0)
		  {
		  %this.newX = %this.camPosXOrg + %corectionAmnt;
		  //echo(%this.newX);
		  }
          else if(%this.camPosXOrg >= 0 && %this.distanceX > 0)
          {		  
	      %this.newX = %this.camPosXOrg - %corectionAmnt;
		  //echo(%this.newX);
          }
          else if(%this.camPosXOrg < 0 && %this.distanceX < 0)//These are just so that positive and negative numbers don't interfere.
          {		  
	      %this.newX = %this.camPosXOrg + %corectionAmnt;
		  //echo(%this.newX);
          }	
          else if(%this.distanceX == 0)
          {		  
	      %this.newX = %this.camPosXOrg;
		  //echo(%this.newX);
          } 		  
	   }
	   else//cam needs to go down
	   {
	      if(%this.camPosXOrg < 0 && %this.distanceX > 0)
          {          
		  %this.newX = %this.camPosXOrg + %corectionAmnt;
		  //echo(%this.newX);
          }
		  else if(%this.camPosXOrg >= 0 && %this.distanceX < 0)
		  {
		  %this.newX = %this.camPosXOrg - %corectionAmnt;
		  //echo(%this.newX);
		  }
          else if(%this.camPosXOrg >= 0 && %this.distanceX > 0)
          {		  
	      %this.newX = %this.camPosXOrg + %corectionAmnt;
		  //echo(%this.newX);
          }
          else if(%this.camPosXOrg < 0 && %this.distanceX < 0)
          {		  
	      %this.newX = %this.camPosXOrg - %corectionAmnt;
		  //echo(%this.newX);
          }	 
	   }
	}
	else
	{
	%this.newX = %this.camPosXOrg;//if you didn't move on the X axis.
	}	
}//___________________________________________________________________________________________________________END OF UPDATE X_________________________________________________________________________

Is there a more smooth way of implementing this?


#1
12/03/2011 (9:28 pm)
What can I do? Any help would be greatly appreciated.
#2
12/13/2011 (10:26 am)
Nothing huh, Ah well Thanks any way.

Any moderator or admin can go ahead and delete this. If you need to.
#3
12/13/2011 (1:59 pm)
Knowing what engine you were working with would be a big help in allowing others to help you.

#4
12/13/2011 (4:39 pm)
I've gone ahead and moved this thread to the correct forums section for more exposure.
#5
12/21/2011 (9:02 am)
I'm working with torque game builder (Torque 2D vers 1.7.5). Also sorry for posting in the wrong section. Thanks for moving it.

Also here's my Execution code (I couldn't fit my whole script here).

sceneWindow2d.setTargetCameraPosition(%this.newX, %this.newY, %this.cameraWidth, %this.cameraHieght);
 sceneWindow2D.startCameraMove(%this.cameraSpeed);

%this.cameraSpeed is 32 because that's the closest to rigid as I could get.