Camera rotation

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
cdesseno
Posts: 10
Joined: Sun Dec 14, 2008 8:03 pm
Location: Buenos Aires, Argentina
Contact:

Camera rotation

Post by cdesseno »

I searched over the forums but I only found something about the bindTargetAndRotation() function.

I move a car with the keys, so I want a camera following it.

In the loop:

Code: Select all

cam->setTarget(carPosition);
cam->setPosition(core::vector3df(carPosition.X - 10.0f, carPosition.Y + 10.0f, carPosition.Z));
cam->setRotation(car->getRotation());
The problem is that the camera follows the car but it doesn't rotate. What can I do?

Thanks.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

by default a camera doesn't act on setRotation(...) !!!
that's why the new function bindTargetAndRotation() was added, so a camera does act on setRotation() now if target and rotation are bind... ;)

but you also use setTarget() and setRotation() on the camera, so the question is what function should the camera react on !?!?!
use either one of them and not both !!!
and of course use bindTargetAndRotation() first if you want to use setRotation() !!! :lol:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
cdesseno
Posts: 10
Joined: Sun Dec 14, 2008 8:03 pm
Location: Buenos Aires, Argentina
Contact:

Post by cdesseno »

Thanks:D
Now I have this, but it is not working as I expected...

Code: Select all

scene::ICameraSceneNode * cam = smgr->addCameraSceneNode(0, core::vector3df(0, 0, 0), core::vector3df(0, 0, 0));
cam->bindTargetAndRotation(1);
In the loop:

Code: Select all

cam->setPosition(core::vector3df(carPosition.X, carPosition.Y + 5.0f, carPosition.Z -10));
cam->setRotation(car->getRotation());
Now the problem is other, the car sometimes looses the focus. I mean I don't see all the car. So, do I need the setTarget() function to avoid this? But how I implement it with the setRotation also?

I think I'm confused :?:
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

cdesseno wrote:Now the problem is other, the car sometimes looses the focus. I mean I don't see all the car.
what do you mean ???
does it vanish ???
maybe it's a culling problem, try to set another culling mode for the car (look for setAutomaticCulling(...) )... ;)
cdesseno wrote:So, do I need the setTarget() function to avoid this? But how I implement it with the setRotation also?
you can use either methode you want, there should be no difference in the result, just the handling differs... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
cdesseno
Posts: 10
Joined: Sun Dec 14, 2008 8:03 pm
Location: Buenos Aires, Argentina
Contact:

Post by cdesseno »

Testing the code:

Code: Select all

scene::ICameraSceneNode * cam = smgr->addCameraSceneNode(0, core::vector3df(0, 0, 0), core::vector3df(0, 0, 0));
cam->bindTargetAndRotation(1);
cam->setAutomaticCulling(scene::EAC_FRUSTUM_BOX);

Code: Select all

cam->setRotation(car->getRotation());
cam->setPosition(core::vector3df(carPosition.X, carPosition.Y + 5.0f, carPosition.Z -10));
This is Ok:
Image

This happened when I try to steer.
Image

I think it's because of
cam->setPosition(core::vector3df(carPosition.X, carPosition.Y + 5.0f, carPosition.Z - 10));

When I go forward it's ok, buy when I steer instead of Z I have to change X and Z.
[/list][/list]
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I'm pretty sure that the problem that you're having is because the camera is rotating around it's Y axis. What you probably want is for the camera to rotate around the vehicle. i.e., you want the vehicle to stay in the same spot on the screen and the camera should stay 'behind' it, right?

If so, you're going to need some more logic to put the camera in the right spot with the right rotation. There was some code that was posted in the past for a fancy chase camera. You might want to use the search feature to find it.

Travis
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

what about setting the camera as a child of the car ???
then you don't need to set it's rotation/target and position, it always will follow your car... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Sort of. I really doubt that he wants the camera to follow the car as if it were mounted rigidly from a transparent boom sticking out of the rear of the car.

He probably wants some smoothing so that if the user makes a hard turn you would see the side of the car for a few seconds, and then the camera would slowly rotate into position behind the car again, based on the speed of the car. This is how all modern driving/riding games that I've seen behave.

Travis
cdesseno
Posts: 10
Joined: Sun Dec 14, 2008 8:03 pm
Location: Buenos Aires, Argentina
Contact:

Post by cdesseno »

vitek wrote:Sort of. I really doubt that he wants the camera to follow the car as if it were mounted rigidly from a transparent boom sticking out of the rear of the car.

He probably wants some smoothing so that if the user makes a hard turn you would see the side of the car for a few seconds, and then the camera would slowly rotate into position behind the car again, based on the speed of the car. This is how all modern driving/riding games that I've seen behave.

Travis
That's would be nice!
cdesseno
Posts: 10
Joined: Sun Dec 14, 2008 8:03 pm
Location: Buenos Aires, Argentina
Contact:

Post by cdesseno »

If I can't I will prefer the first option :lol:
cdesseno
Posts: 10
Joined: Sun Dec 14, 2008 8:03 pm
Location: Buenos Aires, Argentina
Contact:

Post by cdesseno »

Searching I found this and works!

Code: Select all

core::vector3df RelativeToCar(0, 6, -10);
car->getAbsoluteTransformation().transformVect(RelativeToCar);
cam->setPosition(RelativeToCar);
cam->setTarget(car->getAbsolutePosition()); 
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Well to solve this u can make several approches

1:
attach a empty scenenode as a child behind the car. Then every frame u set the Target of ur camera to your car and u move the camera itself towards the attached scenenode. either with a constant speed or depending to the distance (ofcourse it should be always time depended movement...never frame based)

2:
U could make the nessesary calculations yourself without using an extra scenenode. that pretty easy as well. You define a vector as pointing back in reference to the center of the car. then u apply the rotation of the car to that vector. after that u have the same position as if u would attach a scenenode. then simply do as in 1.

These two methods might look a little strange bc sometime the camera will move through the car when turning hard and the cam speed isn't high enough.
To solve that i have suggestion number 3

3:
Because ur car will only rotate around the Y-Axis u can define a rotation ring ring using polar cordinates with y=0 or any value u want. Then simply set the Car Y rotation to that rotation ring with smoth interpolation. Thats probably the nicest method.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Post Reply