Page 1 of 1

Camera Parent Doesn't Follow Correctly.

Posted: Tue Aug 10, 2004 7:31 pm
by Atlantis
I'm working on a remake of asteroids.. ( for fun and to learn from ).

And for some reason when I parent a camera to my ship.. It doesn't follow it properly.. I mean it is right on start but if I move my ship the camera goes off in some strange direction...

Then I decided to use this code to move the camera around.. But it is so jerky..

Can anyone help me with this problem?

Code: Select all

void CPlayer::Update(long cTime) {
	float elapsedTime = cTime * 0.001f;

	//Move ship forward
	vector3df pos = node->getPosition();
	vector3df dir = forward->getAbsolutePosition() - pos;
	dir.normalize();
	pos += dir * (moveSpeed * elapsedTime);
	node->setPosition(pos);

	//Update camera
	matrix4 m; 
	m.setRotationDegrees( node->getRotation() ); 

	vector3df target = vector3df(0, 0, 1.0f); 
	vector3df upVec = vector3df(0, 1.0f, 0);
	m.transformVect(target); 
	target += forward->getAbsolutePosition(); 
	m.transformVect(upVec);

	camera->GetCamera()->setUpVector(upVec); 
	camera->GetCamera()->setTarget(target);
	camera->GetCamera()->setPosition(cameraPosition->getAbsolutePosition());
}
forward and cameraPosition are dummy objects parented to the ship.

Posted: Tue Aug 10, 2004 8:17 pm
by cinek
Hehehe, wait till you add a plane with background, it becomes unplayable ;)

Or rather don't wait, I've found a solution to this same problem just yesterday.
I have to call updateAbsolutePosition() on both the camera and object after changing their positions (I call it for all objects after changing their positions, maybe that's not necessary, I don't know).

AFAIK updateAbsolutePosition() is called on all scene nodes AFTER the rendering (in onPostRender()), so the current frame isn't rendered with correct positions. Don't ask me why it is so, that's what I found somewhere on the forum, tried it, it worked.

Anyway, setting the camera as a child of the starship should also work (but you have to change the "look at" position with setTarget() AFAIR), though I haven't tried it myself.

Oh BTW: I'm making a multiplayer asteroids-like game myself :)

Posted: Tue Aug 10, 2004 8:32 pm
by Guest
Hey thanks it worked :)

And you got any tips on how to make more realistic ship movement?
It can only change pitch and yaw.. But I want it to bank when you turn..
But can't seem to find a way to do it.

Its just a perfect flat turn on yaw right now.. no banking which would make it more realistic..

Posted: Tue Aug 10, 2004 8:45 pm
by cinek
Heh, good idea, I'll do that too :)

I think one could just make the bank rotation to be a simple function of angular velocity, even linear might be good for start.

I'll post, when I have something that looks good.

Posted: Wed Aug 11, 2004 9:05 pm
by cinek
Yes, linear function is enough, all I had to do was adding rotation on X axis based on angular speed:

Code: Select all

node->setRotation(w*0.4, angle/GRAD_PI2, 0);
where w (its omega, heh) is angular speed, 0.4 is just a coefficient, I chose it by trying different values till it looked good, and angle is the orientation (in radians).

Here's the result: ftp://studio4plus.com/pub/test-040811.zip (see the README and remember! it's just a messy test program ;)).

Posted: Thu Aug 12, 2004 1:27 am
by Atlantis
How do you find the objects Angular Velocity? If I can ask...

Posted: Thu Aug 12, 2004 1:38 am
by Guest
And if you know how I can make it work with my camera.. :(

My ship movement and camera suck... The camera is fixed and is in perfect sync with the ship.. It rolls the camera rolls so the ship looks stationary..

Image

I was rolling while destroying that asteroid.. and the camera is still stationary.. :(

Posted: Thu Aug 12, 2004 10:10 am
by cinek
I store position, linear velocity and acceleration and orientation, angular velocity and acceleration in my own class. I set accelerations when player presses buttons, speeds and postion are computed from it (it's very simple).

So as you see it's a bit different approach. If you don't want to change your code, you could find the angular speed simply by derivating the rotation:

w = (lastAnlge - currentAngle)/elapsedTime

You could then use it to rotate your ship or whatever.
Now, the camera: I guess that you'll have to change the up vector to make it bank left or right, that would be:

target.rotateXZBy(angleInDegrees, vector3df(0, 0, 0)); // but I haven't used it, may not be what I think it is.

Posted: Thu Aug 12, 2004 7:43 pm
by Atlantis

Code: Select all

w = (lastAnlge - currentAngle)/elapsedTime
is w a vector?
or just a float.

Same for current angle and lastangle..

are they vectors or just a float with the current Yaw Rotation in euler angles?

Posted: Thu Aug 12, 2004 8:53 pm
by cinek
My game allows only 2D movement, so they are scalars. If they were vectors representing Euler angles, you'd end up with angular speeds on every axis respectively (but Euler angles have some problems with representing 3D orientation, you might consider using matrices or quaternions. Then the basic rules still apply (speed is a derivative of position etc.) but the computations look a bit different).

Check Chris Hecker's site on dynamics for excellent articles focusing on using phisics in games.

Posted: Thu Aug 12, 2004 8:57 pm
by Atlantis
thanks.. Well thats one of the only things thats stopping me from continuing development.. is the crappy camera and movement.. But thanks I"ll check out that site..

Posted: Fri Aug 13, 2004 12:25 pm
by Electron
which camera are you using?

Your problem might be that you need to set the camera's target every fram, if it is the child of something else. The camera node will move with its parent, but it will always remain looking at the same spot otherwise