Camera Parent Doesn't Follow Correctly.

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Atlantis

Camera Parent Doesn't Follow Correctly.

Post 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.
cinek
Posts: 13
Joined: Sat Aug 07, 2004 3:00 pm
Location: Poland

Post 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 :)
Cinek
Guest

Post 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..
cinek
Posts: 13
Joined: Sat Aug 07, 2004 3:00 pm
Location: Poland

Post 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.
Cinek
cinek
Posts: 13
Joined: Sat Aug 07, 2004 3:00 pm
Location: Poland

Post 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 ;)).
Cinek
Atlantis

Post by Atlantis »

How do you find the objects Angular Velocity? If I can ask...
Guest

Post 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.. :(
cinek
Posts: 13
Joined: Sat Aug 07, 2004 3:00 pm
Location: Poland

Post 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.
Cinek
Atlantis
Posts: 8
Joined: Thu Aug 12, 2004 6:31 am
Location: Florida

Post 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?
-Atlantis

Well Then we'll just have to get to know each other.. Me umm I like uh, farris wheels and college football, anything that goes faster than 200 miles per hour. Mjr. John Sheppard ( Stargate: Atlantis )
cinek
Posts: 13
Joined: Sat Aug 07, 2004 3:00 pm
Location: Poland

Post 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.
Cinek
Atlantis
Posts: 8
Joined: Thu Aug 12, 2004 6:31 am
Location: Florida

Post 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..
-Atlantis

Well Then we'll just have to get to know each other.. Me umm I like uh, farris wheels and college football, anything that goes faster than 200 miles per hour. Mjr. John Sheppard ( Stargate: Atlantis )
Electron
Posts: 874
Joined: Sun Mar 14, 2004 12:05 am
Location: Massachusetts USA

Post 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
You do a lot of programming? Really? I try to get some in, but the debugging keeps me pretty busy.

Crucible of Stars
Post Reply