本文整理汇总了C++中idVec3::ToVec2方法的典型用法代码示例。如果您正苦于以下问题:C++ idVec3::ToVec2方法的具体用法?C++ idVec3::ToVec2怎么用?C++ idVec3::ToVec2使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类idVec3
的用法示例。
在下文中一共展示了idVec3::ToVec2方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Ballistics
static int Ballistics( const idVec3 &start, const idVec3 &end, float speed, float gravity, ballistics_t bal[2] ) {
int n, i;
float x, y, a, b, c, d, sqrtd, inva, p[2];
x = ( end.ToVec2() - start.ToVec2() ).Length();
y = end[2] - start[2];
a = 4.0f * y * y + 4.0f * x * x;
b = -4.0f * speed * speed - 4.0f * y * gravity;
c = gravity * gravity;
d = b * b - 4.0f * a * c;
if ( d <= 0.0f || a == 0.0f ) {
return 0;
}
sqrtd = idMath::Sqrt( d );
inva = 0.5f / a;
p[0] = ( - b + sqrtd ) * inva;
p[1] = ( - b - sqrtd ) * inva;
n = 0;
for ( i = 0; i < 2; i++ ) {
if ( p[i] <= 0.0f ) {
continue;
}
d = idMath::Sqrt( p[i] );
bal[n].angle = atan2( 0.5f * ( 2.0f * y * p[i] - gravity ) / d, d * x );
bal[n].time = x / ( cos( bal[n].angle ) * speed );
bal[n].angle = idMath::AngleNormalize180( RAD2DEG( bal[n].angle ) );
n++;
}
return n;
}
示例2: FindPathAroundObstacles
/*
============
idAI::FindPathAroundObstacles
Finds a path around dynamic obstacles using a path tree with clockwise and counter clockwise edge walks.
============
*/
bool idAI::FindPathAroundObstacles( const idPhysics *physics, const idAAS *aas, const idEntity *ignore, const idVec3 &startPos, const idVec3 &seekPos, obstaclePath_t &path ) {
int numObstacles, areaNum, insideObstacle;
obstacle_t obstacles[MAX_OBSTACLES];
idBounds clipBounds;
idBounds bounds;
pathNode_t *root;
bool pathToGoalExists;
path.seekPos = seekPos;
path.firstObstacle = NULL;
path.startPosOutsideObstacles = startPos;
path.startPosObstacle = NULL;
path.seekPosOutsideObstacles = seekPos;
path.seekPosObstacle = NULL;
if( !aas ) {
return true;
}
bounds[1] = aas->GetSettings()->boundingBoxes[0][1];
bounds[0] = -bounds[1];
bounds[1].z = 32.0f;
// get the AAS area number and a valid point inside that area
areaNum = aas->PointReachableAreaNum( path.startPosOutsideObstacles, bounds, ( AREA_REACHABLE_WALK | AREA_REACHABLE_FLY ) );
aas->PushPointIntoAreaNum( areaNum, path.startPosOutsideObstacles );
// get all the nearby obstacles
numObstacles = GetObstacles( physics, aas, ignore, areaNum, path.startPosOutsideObstacles, path.seekPosOutsideObstacles, obstacles, MAX_OBSTACLES, clipBounds );
// get a source position outside the obstacles
GetPointOutsideObstacles( obstacles, numObstacles, path.startPosOutsideObstacles.ToVec2(), &insideObstacle, NULL );
if( insideObstacle != -1 ) {
path.startPosObstacle = obstacles[insideObstacle].entity;
}
// get a goal position outside the obstacles
GetPointOutsideObstacles( obstacles, numObstacles, path.seekPosOutsideObstacles.ToVec2(), &insideObstacle, NULL );
if( insideObstacle != -1 ) {
path.seekPosObstacle = obstacles[insideObstacle].entity;
}
// if start and destination are pushed to the same point, we don't have a path around the obstacle
if( ( path.seekPosOutsideObstacles.ToVec2() - path.startPosOutsideObstacles.ToVec2() ).LengthSqr() < Square( 1.0f ) ) {
if( ( seekPos.ToVec2() - startPos.ToVec2() ).LengthSqr() > Square( 2.0f ) ) {
return false;
}
}
// build a path tree
root = BuildPathTree( obstacles, numObstacles, clipBounds, path.startPosOutsideObstacles.ToVec2(), path.seekPosOutsideObstacles.ToVec2(), path );
// draw the path tree
if( ai_showObstacleAvoidance.GetBool() ) {
DrawPathTree( root, physics->GetOrigin().z );
}
// prune the tree
PrunePathTree( root, path.seekPosOutsideObstacles.ToVec2() );
// find the optimal path
pathToGoalExists = FindOptimalPath( root, obstacles, numObstacles, physics->GetOrigin().z, physics->GetLinearVelocity(), path.seekPos );
// free the tree
FreePathTree_r( root );
return pathToGoalExists;
}
示例3: FindOptimalPath
/*
============
FindOptimalPath
Returns true if there is a path all the way to the goal.
============
*/
bool FindOptimalPath( const pathNode_t *root, const obstacle_t *obstacles, int numObstacles, const float height, const idVec3 &curDir, idVec3 &seekPos ) {
int i, numPathPoints, bestNumPathPoints;
const pathNode_t *node, *lastNode, *bestNode;
idVec2 optimizedPath[MAX_OBSTACLE_PATH];
float pathLength, bestPathLength;
bool pathToGoalExists, optimizedPathCalculated;
seekPos.Zero();
seekPos.z = height;
pathToGoalExists = false;
optimizedPathCalculated = false;
bestNode = root;
bestNumPathPoints = 0;
bestPathLength = idMath::INFINITY;
node = root;
while( node ) {
pathToGoalExists |= ( node->dist < 0.1f );
if ( node->dist <= bestNode->dist ) {
if ( idMath::Fabs( node->dist - bestNode->dist ) < 0.1f ) {
if ( !optimizedPathCalculated ) {
bestNumPathPoints = OptimizePath( root, bestNode, obstacles, numObstacles, optimizedPath );
bestPathLength = PathLength( optimizedPath, bestNumPathPoints, curDir.ToVec2() );
seekPos.ToVec2() = optimizedPath[1];
}
numPathPoints = OptimizePath( root, node, obstacles, numObstacles, optimizedPath );
pathLength = PathLength( optimizedPath, numPathPoints, curDir.ToVec2() );
if ( pathLength < bestPathLength ) {
bestNode = node;
bestNumPathPoints = numPathPoints;
bestPathLength = pathLength;
seekPos.ToVec2() = optimizedPath[1];
}
optimizedPathCalculated = true;
} else {
bestNode = node;
optimizedPathCalculated = false;
}
}
if ( node->children[0] ) {
node = node->children[0];
} else if ( node->children[1] ) {
node = node->children[1];
} else {
for ( lastNode = node, node = node->parent; node; lastNode = node, node = node->parent ) {
if ( node->children[1] && node->children[1] != lastNode ) {
node = node->children[1];
break;
}
}
}
}
if ( !pathToGoalExists ) {
seekPos.ToVec2() = root->children[0]->pos;
} else if ( !optimizedPathCalculated ) {
OptimizePath( root, bestNode, obstacles, numObstacles, optimizedPath );
seekPos.ToVec2() = optimizedPath[1];
}
if ( ai_showObstacleAvoidance.GetBool() ) {
idVec3 start, end;
start.z = end.z = height + 4.0f;
numPathPoints = OptimizePath( root, bestNode, obstacles, numObstacles, optimizedPath );
for ( i = 0; i < numPathPoints-1; i++ ) {
start.ToVec2() = optimizedPath[i];
end.ToVec2() = optimizedPath[i+1];
gameRenderWorld->DebugArrow( colorCyan, start, end, 1 );
}
}
return pathToGoalExists;
}
示例4: GetObstacles
/*
============
GetObstacles
============
*/
int GetObstacles( const idPhysics *physics, const idAAS *aas, const idEntity *ignore, int areaNum, const idVec3 &startPos, const idVec3 &seekPos, obstacle_t *obstacles, int maxObstacles, idBounds &clipBounds ) {
int i, j, numListedClipModels, numObstacles, numVerts, clipMask, blockingObstacle, blockingEdgeNum;
int wallEdges[MAX_AAS_WALL_EDGES], numWallEdges, verts[2], lastVerts[2], nextVerts[2];
float stepHeight, headHeight, blockingScale, min, max;
idVec3 seekDelta, silVerts[32], start, end, nextStart, nextEnd;
idVec2 expBounds[2], edgeDir, edgeNormal, nextEdgeDir, nextEdgeNormal, lastEdgeNormal;
idVec2 obDelta;
idPhysics *obPhys;
idBox box;
idEntity *obEnt;
idClipModel *clipModel;
idClipModel *clipModelList[ MAX_GENTITIES ];
numObstacles = 0;
seekDelta = seekPos - startPos;
expBounds[0] = physics->GetBounds()[0].ToVec2() - idVec2( CM_BOX_EPSILON, CM_BOX_EPSILON );
expBounds[1] = physics->GetBounds()[1].ToVec2() + idVec2( CM_BOX_EPSILON, CM_BOX_EPSILON );
physics->GetAbsBounds().AxisProjection( -physics->GetGravityNormal(), stepHeight, headHeight );
stepHeight += aas->GetSettings()->maxStepHeight;
// clip bounds for the obstacle search space
clipBounds[0] = clipBounds[1] = startPos;
clipBounds.AddPoint( seekPos );
clipBounds.ExpandSelf( MAX_OBSTACLE_RADIUS );
clipMask = physics->GetClipMask();
// find all obstacles touching the clip bounds
numListedClipModels = gameLocal.clip.ClipModelsTouchingBounds( clipBounds, clipMask, clipModelList, MAX_GENTITIES );
for ( i = 0; i < numListedClipModels && numObstacles < MAX_OBSTACLES; i++ ) {
clipModel = clipModelList[i];
obEnt = clipModel->GetEntity();
if ( !clipModel->IsTraceModel() ) {
continue;
}
if ( obEnt->IsType( idActor::Type ) ) {
obPhys = obEnt->GetPhysics();
// ignore myself, my enemy, and dead bodies
if ( ( obPhys == physics ) || ( obEnt == ignore ) || ( obEnt->health <= 0 ) ) {
continue;
}
// if the actor is moving
idVec3 v1 = obPhys->GetLinearVelocity();
if ( v1.LengthSqr() > Square( 10.0f ) ) {
idVec3 v2 = physics->GetLinearVelocity();
if ( v2.LengthSqr() > Square( 10.0f ) ) {
// if moving in about the same direction
if ( v1 * v2 > 0.0f ) {
continue;
}
}
}
} else if ( obEnt->IsType( idMoveable::Type ) ) {
// moveables are considered obstacles
} else {
// ignore everything else
continue;
}
// check if we can step over the object
clipModel->GetAbsBounds().AxisProjection( -physics->GetGravityNormal(), min, max );
if ( max < stepHeight || min > headHeight ) {
// can step over this one
continue;
}
// project a box containing the obstacle onto the floor plane
box = idBox( clipModel->GetBounds(), clipModel->GetOrigin(), clipModel->GetAxis() );
numVerts = box.GetParallelProjectionSilhouetteVerts( physics->GetGravityNormal(), silVerts );
// create a 2D winding for the obstacle;
obstacle_t &obstacle = obstacles[numObstacles++];
obstacle.winding.Clear();
for ( j = 0; j < numVerts; j++ ) {
obstacle.winding.AddPoint( silVerts[j].ToVec2() );
}
if ( ai_showObstacleAvoidance.GetBool() ) {
for ( j = 0; j < numVerts; j++ ) {
silVerts[j].z = startPos.z;
}
for ( j = 0; j < numVerts; j++ ) {
gameRenderWorld->DebugArrow( colorWhite, silVerts[j], silVerts[(j+1)%numVerts], 4 );
}
}
// expand the 2D winding for collision with a 2D box
obstacle.winding.ExpandForAxialBox( expBounds );
obstacle.winding.GetBounds( obstacle.bounds );
obstacle.entity = obEnt;
}
//.........这里部分代码省略.........
示例5: TestTrajectory
/*
=====================
idAI::TestTrajectory
=====================
*/
bool idAI::TestTrajectory( const idVec3 &start, const idVec3 &end, float zVel, float gravity, float time, float max_height, const idClipModel *clip, int clipmask, const idEntity *ignore, const idEntity *targetEntity, int drawtime ) {
int i, numSegments;
float maxHeight, t, t2;
idVec3 points[5];
trace_t trace;
bool result;
t = zVel / gravity;
// maximum height of projectile
maxHeight = start.z - 0.5f * gravity * ( t * t );
// time it takes to fall from the top to the end height
t = idMath::Sqrt( ( maxHeight - end.z ) / ( 0.5f * -gravity ) );
// start of parabolic
points[0] = start;
if ( t < time ) {
numSegments = 4;
// point in the middle between top and start
t2 = ( time - t ) * 0.5f;
points[1].ToVec2() = start.ToVec2() + (end.ToVec2() - start.ToVec2()) * ( t2 / time );
points[1].z = start.z + t2 * zVel + 0.5f * gravity * t2 * t2;
// top of parabolic
t2 = time - t;
points[2].ToVec2() = start.ToVec2() + (end.ToVec2() - start.ToVec2()) * ( t2 / time );
points[2].z = start.z + t2 * zVel + 0.5f * gravity * t2 * t2;
// point in the middel between top and end
t2 = time - t * 0.5f;
points[3].ToVec2() = start.ToVec2() + (end.ToVec2() - start.ToVec2()) * ( t2 / time );
points[3].z = start.z + t2 * zVel + 0.5f * gravity * t2 * t2;
} else {
numSegments = 2;
// point halfway through
t2 = time * 0.5f;
points[1].ToVec2() = start.ToVec2() + ( end.ToVec2() - start.ToVec2() ) * 0.5f;
points[1].z = start.z + t2 * zVel + 0.5f * gravity * t2 * t2;
}
// end of parabolic
points[numSegments] = end;
if ( drawtime ) {
for ( i = 0; i < numSegments; i++ ) {
gameRenderWorld->DebugLine( colorRed, points[i], points[i+1], drawtime );
}
}
// make sure projectile doesn't go higher than we want it to go
for ( i = 0; i < numSegments; i++ ) {
if ( points[i].z > max_height ) {
// goes higher than we want to allow
return false;
}
}
result = true;
for ( i = 0; i < numSegments; i++ ) {
gameLocal.clip.Translation( trace, points[i], points[i+1], clip, mat3_identity, clipmask, ignore );
if ( trace.fraction < 1.0f ) {
if ( gameLocal.GetTraceEntity( trace ) == targetEntity ) {
result = true;
} else {
result = false;
}
break;
}
}
if ( drawtime ) {
if ( clip ) {
gameRenderWorld->DebugBounds( result ? colorGreen : colorYellow, clip->GetBounds().Expand( 1.0f ), trace.endpos, drawtime );
} else {
idBounds bnds( trace.endpos );
bnds.ExpandSelf( 1.0f );
gameRenderWorld->DebugBounds( result ? colorGreen : colorYellow, bnds, vec3_zero, drawtime );
}
}
return result;
}
示例6: CartesianToCylindrical
void hhDeathWraithEnergy::CartesianToCylindrical(idVec3 &cartesian, float &radius, float &theta, float &z) {
radius = cartesian.ToVec2().Length();
theta = idMath::ATan(cartesian.y, cartesian.x);
z = cartesian.z;
}