Camera Collision Into Player First Person
by UCF_0017 · in Torque Game Engine · 09/26/2006 (7:22 am) · 4 replies
I am using the advanced camera resource and would like to get it so when you back into a wall and the camera collides into the player's back, that it'd go into first person mode.
Does anyone have any good suggestions or links to resources which might help me out? I have been working on Torque now for about two months. Thanks.
Does anyone have any good suggestions or links to resources which might help me out? I have been working on Torque now for about two months. Thanks.
About the author
#2
What I would do would be to look at the actual camera position each frame, and compare it to the position for the view node of the first-person player, and if they're within two feet (or some configurable limit), snap the camera to first person.
09/28/2006 (7:58 pm)
I know of no good resources or links that Google wouldn't find much better than me.What I would do would be to look at the actual camera position each frame, and compare it to the position for the view node of the first-person player, and if they're within two feet (or some configurable limit), snap the camera to first person.
#3
I wanted to try to do this with as little processing power as possible, so I started looking at how the advancedCamera.cc file implements its collision checking with walls.
I found:
Now I am thinking that if I can have it check to see if its colliding with both an interior type or terrain type AND the player, then it would flip on the first person camera, until that combination of collisions stops occurring. Unfortunately, I don't think I know how to use the castRay function correctly.
This is where I am so far:
Unforunately, all this does is sets the camera in some location off my map when I collide into terrain or interior type objects. I think I may be using castRay the second time in the wrong way.
09/30/2006 (4:01 pm)
That's not a bad idea, and I appreciate the response JW. I wanted to try to do this with as little processing power as possible, so I started looking at how the advancedCamera.cc file implements its collision checking with walls.
I found:
// See if the camera view is ocluded by interiors or terrain, and move the camera closer to the player in that case
Point3F AdvancedCamera::runCameraCollisionCheck(const Point3F& startpos, const Point3F& endpos) {
U32 mask = InteriorObjectType | TerrainObjectType;
RayInfo collisionInfo;
// Pad the start position with the lookAtOffset, so the camera doesnt move strange when a little bump
// in the terrain is between camera and the player feet
// Cast ray and check for collision with terrain and interiors
if (!gClientContainer.castRay(startpos + mCurrentLookAtOffset, endpos, mask, &collisionInfo)) {
// No collision, so return endpos
return endpos;
} else {
// We collided, so return the point of collision
// subtract a slight offset so we don't show clipping
Point3F LookDir = startpos + mCurrentLookAtOffset - collisionInfo.point;
LookDir.normalize();
Point3F AntiClippingOffset = LookDir * DISTANCE_FROM_COLLISION;
return collisionInfo.point + AntiClippingOffset;
}
}Now I am thinking that if I can have it check to see if its colliding with both an interior type or terrain type AND the player, then it would flip on the first person camera, until that combination of collisions stops occurring. Unfortunately, I don't think I know how to use the castRay function correctly.
This is where I am so far:
// See if the camera view is ocluded by interiors or terrain, and move the camera closer to the player in that case
Point3F AdvancedCamera::runCameraCollisionCheck(const Point3F& startpos, const Point3F& endpos) {
U32 mask = InteriorObjectType | TerrainObjectType;
RayInfo collisionInfo;
// Pad the start position with the lookAtOffset, so the camera doesnt move strange when a little bump
// in the terrain is between camera and the player feet
// Cast ray and check for collision with terrain and interiors
if (!gClientContainer.castRay(startpos + mCurrentLookAtOffset, endpos, mask, &collisionInfo)) {
// No collision, so return endpos
return endpos;
} else { //This branch occurs when the camera collides with terrain and interiors
U32 playerMask = PlayerObjectType;
RayInfo collisionInfo2;
// We collided, so return the point of collision
// subtract a slight offset so we don't show clipping
Point3F LookDir = startpos + mCurrentLookAtOffset - collisionInfo.point;
LookDir.normalize();
Point3F AntiClippingOffset = LookDir * DISTANCE_FROM_COLLISION;
Point3F returnValue = collisionInfo.point + AntiClippingOffset;
if (!gClientContainer.castRay(startpos + mCurrentLookAtOffset, endpos, playerMask, &collisionInfo2))
{
return returnValue;
}
else
{
//do code to flip on first person camera here.
}
}
}Unforunately, all this does is sets the camera in some location off my map when I collide into terrain or interior type objects. I think I may be using castRay the second time in the wrong way.
#4
10/01/2006 (7:57 pm)
CastRay is a LOT more expensive than a simple distance check.
Torque Owner UCF_0017