[SOLVED] Camera Collision Issue

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
Aelis440
Posts: 52
Joined: Sun Oct 05, 2008 8:45 pm

[SOLVED] Camera Collision Issue

Post by Aelis440 »

I have an issue with my camera and I cannot figure it out. The following link has both the executable and extremely stripped down source code.

http://www.sagescholars4scholars.com/Game.rar

First run the executable. When it loads, zoom your camera out with your mouse scroller. Then, hold down either mouse button (left or right) and move your camera around so it collides with the building. You'll notice that it freaks out and goes crazy. However, based on what I'm doing, I cannot figure out why this is happening. It has something to do (I think) with the updateCameraPosition() function within the Camera class as it gets called every loop. Here is what it looks like:

Code: Select all

void Cam::updateCameraPosition(Character* target)
{
	vector3df targetLoc = target->getCenter();
	// Calculate our new positions to move to.
	double xMove = targetLoc.X + (globalDistance * sin(angleToTarget * DEGTORAD));
	double zMove = targetLoc.Z + (globalDistance * cos(angleToTarget * DEGTORAD));
	// Keep our camera above or below the target at the specified height.
	double yMove = targetLoc.Y + heightModifier;

	// Finally, set the position.
	position = vector3df(xMove, yMove, zMove);
   // If there is an object in between the character and the camera
   // let's move the camera to just in front (0.90) of that object.
	line3d<f32> line(targetLoc, position);
	triangle3df someTri; 
	vector3df collisionPoint;
	if (cmgr->getCollisionPoint(line, cameraSelector, collisionPoint, someTri))
	{
		position = (collisionPoint + line.start) * f32(0.90);
	}

	cam->setPosition(position);
	cam->setTarget(targetLoc);
}
the line that says "position = (collisionPoint + line.start) * f32(0.90);" is the trick. If you make the 0.90 a 0.50, it works "ok." But, this distance is undesirable. Essentially, my goal is to make it so when there is an object in between the character and the camera, we move the camera to the point right in front of that object (so we can still see the character). This is why I desire a number such as 0.90 instead of 0.50. In addition, when you make the camera freak out at certain angles, it stops looking directly at the target, how is that possible since I call cam->setTarget(targetLoc) every loop? Any advice/insight would be greatly appreciated. Thanks!
Last edited by Aelis440 on Thu Jan 22, 2009 9:54 pm, edited 1 time in total.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Code: Select all

position = (collisionPoint + line.start) * f32(0.90); 
I haven't tested it, but I'd have thought that should be:

Code: Select all

position = line.start + ((collisionPoint - line.start) * 0.9f); 
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Pyritie
Posts: 120
Joined: Fri Jan 16, 2009 12:59 pm
Contact:

Post by Pyritie »

Have you tried looking at that demo that comes with irrlicht? The one about triangle selectors. You could use the ray thingy (i.e. what the demo does with the billboarded plane and the camera) to find where the ray intersects with the object you want the camera to collide with.
Hive Workshop | deviantART

I've moved to Ogre.
Aelis440
Posts: 52
Joined: Sun Oct 05, 2008 8:45 pm

Post by Aelis440 »

Once again Rogerborg, you're spot on. I pulled my math from the lind3d<T>::getMiddle() function, I don't know if the math there is wrong (or just works for that situation) but you may want to give it a glance if you ever have some free time.

It's a shame I can't send a beer through the internet, I probably owe you a 6-pack by now. : )
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Aelis440 wrote:I pulled my math from the lind3d<T>::getMiddle() function, I don't know if the math there is wrong (or just works for that situation)
Yup, that maths only works for 0.5f.

There's actually a vector3d method that would do the job:

Code: Select all

position = collisionPoint.getInterpolated(line.start, 0.9); 
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Post Reply