Sliding character controller

AdrianM
Posts: 2
Joined: Sat May 22, 2010 5:25 pm

Sliding character controller

Post by AdrianM »

I'm trying to implement my own sliding character controller to have more control over it and also to get the most out of speed. I'm running into a few issues though, and i was hoping if someone could help in clarifying some things up. First of all, here's how my algorithm works(i'm using only the collision library): i do a convex sweep and retain only the first intersection point p and normal n. If there is indeed a collision then i decompose the velocity of the object(which is a sphere) into a parallel component pa, and normal component pn, with respect to n. I then reiterate the algorithm with new parameters:
velocityNew = pn * (pn.dot(velocityOld));
startPoint = collisionPoint;
endPoint = startPoint + velocityNew,

until a number of iterations have passed or velocityNew has a small enough magnitude.

The code is very short and can explain better what am i doing:

Code: Select all

	bool collision = false;
	int i;
	for (i=0; i<10; i++)
	{	
		if (vel.length() < 0.00001f)
			break;

		float radius = 150.0f;
		e = s+vel;
	    btCollisionWorld::ClosestConvexResultCallback cb(s, e);				 	 
		btSphereShape sphere(radius);

		btTransform from(btMatrix3x3().getIdentity(), s);
		btTransform to(btMatrix3x3().getIdentity(), e);

		collisionWorld->convexSweepTest(&sphere, from, to, cb);		 
		if (cb.hasHit())
		{				
			point = cb.m_hitPointWorld;
			normal = cb.m_hitNormalWorld;

			btVector3 n = normal;
			n.normalize();
			btVector3 newPos = point + n * radius;
			s = newPos;
			
			btVector3 dir = e - s;
			btVector3 parallel = btDot(n, dir)*n;
			btVector3 perpendicular = dir - parallel;
			perpendicular.normalize();

			vel = perpendicular*((vel).dot(perpendicular));

			collision = true;
		 }else
			 break;
	 }
	if (!collision)	
		s = e;
The problem is that i sometimes run through walls, and at corners the sphere trembles back and forth. What can i do to improve this? I also notice that the character controller provided also has this issue and besides this it's rather slow sometimes.
AdrianM
Posts: 2
Joined: Sat May 22, 2010 5:25 pm

Re: Sliding character controller

Post by AdrianM »

I've managed to minimize the ping-pong behaviour of the charcter(trembling). Here's how:
1.0 Account for velocity "turning around" with respect to the initial velocity, causing jumps back
2.0 Include a discrete collision test, just before the swept one

But i wonder if there's still anything i can do to further refine it and eliminate as much as possible the ping ponging effect. Any ideas?