Camera tracking a non-player object
by David Horn · in Torque Game Engine · 02/04/2008 (6:08 am) · 2 replies
I have to fighters in a ring. Both are AIPlayers.
I'm using the track camera resource in order to have a 3rd person camera - which is working fine. I can easily track either fighter.
However, what I really need is for the camera to be pointed directly in-between the two fighters.
Here was my attempted solution and the ensuing problem...
I created a torque logo object (a placeholder just so I had something I could see) called "ctarget" and made that the camera's target. I then change the position of the logo to be the average of the two fighters' positions.
Then I call the following function that calls itself to update - i assume - every 2 milliseconds...
The problem is that it's incredibly choppy. The position of the object is definitely lagging to the point of the camera jumping and it's incredibly choppy. When I track a fighterwhile calling the setMoveDestination function, it's incredibly smooth.
My question is, is there a better way to do this? should I use something other than the scheduling of the camTarget function that would make it as smooth as the aiPlayer?
FYI - I also attempted to add the setDestination method to the staticshape c and h files, but my C++ chops weren't up to the task. But I figured that way, at least I could call the setMoveDestination with the average mid-point of the 2 fighters instead of directly making it equal to it.
I'm using the track camera resource in order to have a 3rd person camera - which is working fine. I can easily track either fighter.
However, what I really need is for the camera to be pointed directly in-between the two fighters.
Here was my attempted solution and the ensuing problem...
I created a torque logo object (a placeholder just so I had something I could see) called "ctarget" and made that the camera's target. I then change the position of the logo to be the average of the two fighters' positions.
Then I call the following function that calls itself to update - i assume - every 2 milliseconds...
// ========================================================================
// camTarget.cs
//
// This module contains a function for moving the the camera's target
// ========================================================================
function camTarget(){
schedule(2, 0, camTarget);
if ($prevmode==1 ||$prevmode==2){
ctarget.setPosition("25.5293 -415.609 0.59669");
ctarget.setScale(ctarget.getScale());
}
if ($prevmode==0){
%xfrm = ctarget.getTransform();
%lx = getword(%xfrm,0); // first, get the current transform values
%ly = getword(%xfrm,1);
%lz = getword(%xfrm,2);
%rx = getword(%xfrm,3);
%ry = getword(%xfrm,4);
%rz = getword(%xfrm,5);
%rd = getword(%xfrm,6);
%w1 = $wlist[0].id.getTransform(); // $wlist[0].id is fighter number 1
%w1x = getword(%w1,0);
%w1y = getword(%w1,1);
%w1z = getword(%w1,2);
%w2 = $wlist[0].pfocus.getTransform(); // $wlist[1].id is fighter number 2
%w2x = getword(%w2,0);
%w2y = getword(%w2,1);
%w2z = getword(%w1,2);
%lx = (%w1x + %w2x)/2;
%ly = (%w1y + %w2y)/2;
%lz = ((%w1z + %w2z)/2) - 2;
ctarget.setTransform(%lx SPC %ly SPC %lz SPC %rx SPC %ry SPC %rz SPC %rd);
ctarget.setScale(ctarget.getScale());
//the camera is set with...
//$mycam.setTrackObject(ctarget);
}
}The problem is that it's incredibly choppy. The position of the object is definitely lagging to the point of the camera jumping and it's incredibly choppy. When I track a fighterwhile calling the setMoveDestination function, it's incredibly smooth.
My question is, is there a better way to do this? should I use something other than the scheduling of the camTarget function that would make it as smooth as the aiPlayer?
FYI - I also attempted to add the setDestination method to the staticshape c and h files, but my C++ chops weren't up to the task. But I figured that way, at least I could call the setMoveDestination with the average mid-point of the 2 fighters instead of directly making it equal to it.
About the author
#2
Remember torquescript doesn't get processed every frame, it's only done once every 40ms or so. no matter how many calls you do, it'll only update once every 40ms. maybe even more.
I did something with a null object setup as a pathshape i think to move to stay between the objects, i'd have to double check my code to be sure.
02/10/2008 (12:46 am)
Have you looked into the camera resources? i've used two different types and had a non player object be the camera target like you're attempting. Was no big deal. Perhaps you need to do that.Remember torquescript doesn't get processed every frame, it's only done once every 40ms or so. no matter how many calls you do, it'll only update once every 40ms. maybe even more.
I did something with a null object setup as a pathshape i think to move to stay between the objects, i'd have to double check my code to be sure.
Torque Owner Lee Latham
Default Studio Name
My first guess is that you need some kind of interpolation. But then again, you're sample size is quite high.
So my second thought is that something about "every two milliseconds" makes me nervous. I mean, that's 500 times a second, right? It's late and I've had a few beers, so feel free to correct me anywhere along the way, here :-) But seeing as how you only need a total framerate of, say, 50fps to get really smooth real time rendering, that seems excessive in the extreme, and may be creating some type of artifact causing the choppiness you're seeing.
What happens if you slow it down to say, 4 times a second?