Code for a person walking around a sphere, always standing with his feet towards the planet and his head away from the planet, and gravity pulls him toward the center of the planet.

I have been working on this very short code. And it is almost finished, mostly it works like it is supposed to but with some small problems.

Even though the code is short, I added lots of comments so it would be easyer to understand. (and hopefully easier to help me)

Code:
#include <acknex.h>
#include <default.c>
#include <windows.h>

VECTOR WorldCenter;
ENTITY* Person;

action Person_Action()
{
	c_setminmax(me);
	Person=me;
	
	while(1)
	{
		//***************PLAYER MOVEMENT*************
		//forward-back
		c_move(Person,vector((5*(key_w-key_s)*time_step),0,0),nullvector,IGNORE_PASSABLE);
		//strafe left-right
		c_move(Person,vector(0,(5*(key_a-key_d)*time_step),0),nullvector,IGNORE_PASSABLE);
		//rotate left-right
		c_rotate(Person,vector((10*(key_q-key_e)*time_step),0,0),USE_AXIS);
		
		//*******ROTATION RELATIVE TO PLANET CORE ***********
		//Rotate Person on WORLD
		VECTOR TempVec;
		vec_set(TempVec,WorldCenter); //temp vector pointing to center of the planet
		vec_sub(TempVec,Person.x); //subtract vectors for planet core (gravity center) and player position to get a direction vetor towards the center
		vec_to_angle(Person.pan,TempVec); //change direction vector to an angle to rotate the model towards the center, almost ok... but my player is looking towards the planet face down, not standing on it!
		Person.tilt += 90; //I did this to fix the person laying face down toward the planet (now it is standing on it correctly)

		//********************GRAVITY*************************
		//check distance to planet surface
		float FloorDist;
		FloorDist=c_trace(Person.x,WorldCenter,IGNORE_ME | IGNORE_PASSABLE);
		if(FloorDist>68)
		{
			c_move(Person,vector(0,0,-5*time_step),nullvector,IGNORE_PASSABLE);//Apply ORIENTED Gravity (relative to player's z axis)
		}
		else if(FloorDist<67)//I had to add this because my test planet is not completely round and has pointy edges where he got stuck
		{
			c_move(Person,vector(0,0,5*time_step),nullvector,IGNORE_PASSABLE);//Push away from planet's pointy edges
		}
		
		wait(1);
	}
}

void main ()
{
	level_load("Luna.wmb");
	vec_set(WorldCenter,vector(0,0,0));

	camera.x=-2000;
	camera.y=0;
	camera.z=100;

	ent_createlocal("Character_M.mdl",vector(0,0,500),Person_Action);
}


What Does Work:
-The level loads (it is a sphere)
-It loads a Model on top of the sphere that is attracted towards it by gravity
-Gravity always pulls the model towards the center of the sphere even if it is on a side or under it.
-The model's pan/tilt/roll angles are modified so the feet are towards the sphere and the head is away from the sphere.
-"w" and "s" keys make him move forward and back
-"a" and "d" keys make him strafe

What Does NOT Work:
-for some reason the model is standing correclty on the planet, but I cannot get him to face where I want (turn left and right with the "q" and "e" keys sometimes dosent work and other times causes random fliping rotations).
-if directly on the north pole he can only move backwards but not forwards (he sometimes spins if i try to move forward)
-if he is standing directly on the south pole he does not move backwards, only forward (he sometimes spins if i try to move back)

I can post the model and level I am using if needed. This is just a little test code and the model/level are very simple.

Any help to get this working would be greatly apreciated
DOWNLOAD:
http://www.megaupload.com/?d=R5U67BBL
Keys: (wasd qe)
w =forward
s =back
a =strafe left
d =strafe right
q =turn left (not working properly)
e =turn right (not working properly)

Last edited by Carlos3DGS; 05/15/09 14:10. Reason: added download and tweaked code

"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1