Trouble with the rotation again..[SOLVED]

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Trouble with the rotation again..[SOLVED]

Post by FuzzYspo0N »

Hi :)
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=25288

this is what im working on but, i have a problem that others have had and their solutions havent seemed to work for my game.

Problem : I need the camera to be a chase camera that follows the trolley, its simple. the code is even easy to get the position as explained in this wonderful tutorial

http://www.gamedev.net/reference/articl ... le1591.asp

The Physics is a different turning type (as in radians or degrees, polar, euler or quats whatever thats about)
Problem : The camera rotates only between 270 and 90, and when it reaches each number it goes backward, giving the illusion your trolley has lost control, lol.

Ideas : Thought maybe this helps, but it didnt yet (cos i didnt change the physics internally)
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=25409

Or this explains it well with a picture even,
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=24934

Below is the code im using (cut out the bold section its identical (as my logic tells me,even))

Code: Select all

void g_game::updatecam()
{
	f32 yrot = g_trolley->getRotation().Y;
	f32 xpos = g_trolley->getPosition().X;
	f32 zpos = g_trolley->getPosition().Z;
[b]
	core::matrix4 mat; 
	mat.setRotationDegrees(vector3df(0,yrot,0)); 
	core::vector3df yrot1 = mat.getRotationDegrees();[/b]

	f32 camerax = camDist* sin((yrot1.Y) * 3.142 / 180) + xpos;
	f32 cameraz = camDist* cos((yrot1.Y) * 3.142 / 180) + zpos;

	cam->setPosition(core::vector3df(camerax,30,cameraz));
	cam->setTarget(g_trolley->getPosition());
}

Its not the game code,its the test code but i need to understand why the camera wont follow a 360 degree oath behind the object...

FOR REFERENCE {}
http://irrlicht.sourceforge.net/phpBB2/ ... ee2#138995
I am using Tumle. The code is free and small, have a look at the

CControllablePhysicsNode For where it rotates the object, but please,

i am not asking for working code (tho, that is great ) i want to understand, not copy paste as much. i think i understand why its not working, but how do i fix it. I cant upload a demo atm, ill try lower the file size first to test. so you can see i f i cant be helped...

Thanks for the help :)
Last edited by FuzzYspo0N on Wed Jan 02, 2008 11:10 pm, edited 1 time in total.
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

In my last demo - driving/game engine - I have a chase camera that follows the vehicle. It was trivial to implement but took some time to enhance. The user can switch views from rear/front/left/right, alter view distance and the camera smoothly interpolates between views. I also use the physics system to clip the chase camera against world geometry.

I initially had an issue with "rotation wrapping" and "getting things to point in the right direction". I solved it by going through the maths of the misaligned systems to understand what was happening and how to solve it.

Take a look at my driving demos and see if that's the kind of thing you're looking for. If so, I may be able to help you out with some snippets.
evo
Posts: 96
Joined: Mon Jun 27, 2005 6:46 pm
Location: The Netherlands

Post by evo »

I am doing something similar in my app. I have adapted it a bit to suit your purpose. (Haven't tested it in c++, I am using squirrel scripting for these tasks). This code should follow the target at a certain speed and maintain a minimum distance. In order to keep the speed constant you should call the updatecam function at constant time intervals.

Code: Select all

//camDist = constant: minimum distance between trolley and camera
//CamSpeed = constant: speed of the camera
void g_game::updatecam() 
{ 
   core::vector3df Direction = g_trolley->getPosition() - cam->getPosition();
   if ((Direction.getLength() - camDist) > CamSpeed)
   {
      Direction.normalize();
      core::vector3df NewPosition = core::vector3df(0,0,0);
      NewPosition.X = cam->getPosition().X + Direction.X * CamSpeed;
      NewPosition.Y = cam->getPosition().Y + Direction.Y * CamSpeed;
      NewPosition.Z = cam->getPosition().Z + Direction.Z * CamSpeed;
      
      cam->setPosition(NewPosition);
   }
   else
   {  // last step
      Direction.normalize();
      core::vector3df NewPosition = core::vector3df(0,0,0);
      NewPosition.X = cam->getPosition().X + Direction.X * (Direction.getLength() - camDist);
      NewPosition.Y = cam->getPosition().Y + Direction.Y * (Direction.getLength() - camDist);
      NewPosition.Z = cam->getPosition().Z + Direction.Z * (Direction.getLength() - camDist);
      
      cam->setPosition(NewPosition);
   }
   cam->setTarget(g_trolley->getPosition());
}
You will propably need more advanced code than this (pathfinding). Since there is no collision checking done.
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Post by FuzzYspo0N »

Hey si02 thanks for the reply,

I see your demo, i must jsut find it or redownload it , read the reply to evo, and it shud make more sense what im after :

@evo

The problem with your solution is the camera rotation is not so hot...what i need, basically, is the camera to face the same direction as the trolley at all times. Moving to get there is ok, but if its not facing the direction constantly it gets troublesome to race (seeing as youre moving fast)

Your above code, it works to an extent. A few things that didnt go well were crazy jerking , i dunno why, i tried setting the variables to all sorts and with minimal jerk i realised the camera moves down , below 0 if i go "down lower then 0" like, the map is at (0,0,0) but has pieces (underground parking) , when i go under it tends to go below 0. the other thing is it moves closer and closer (reaching camDist away) and when it hits 0 (the (Direction.getLength() - camDist) line makes it to 0, it resets the camera back to -50 or whatever and it jerks again. I am unable to upload a demo on this connection but i think its just being weird...

Until then, sio2 if you wud be willing to explain more about the camera code you have, ill be willing to learn...

Thanks for the replies
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Post by FuzzYspo0N »

CuteAlien helped me some, with a simple example, and it works great.

This was the simplistic camera i need for this game, but ill fix the movement and smooth stuff later, for now, follow!

Thanks cuteAlien. For reference :

Code: Select all

void g_game::updatecam() 
{ 
	core::vector3df camVector(0,0,1);
	const core::matrix4 & targetMatrix = g_trolley->getAbsoluteTransformation ();
	targetMatrix.rotateVect(camVector);
	cam->setTarget( g_trolley->getAbsolutePosition() );
	const float behind = -60.0f;
	const float up = 40.0f;
	camVector *= behind;
	camVector.Y += up; 
	cam->setPosition( g_trolley->getAbsolutePosition() + camVector );
}
Post Reply