Almost finished!!! Spherical Planet Gravity Test

Posted By: Carlos3DGS

Almost finished!!! Spherical Planet Gravity Test - 05/15/09 03:30

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)
Posted By: Felixsg

Re: Almost finished!!! Spherical Planet Gravity Test - 05/15/09 04:35

probably but I can be wrong
the solution not are to move the player (person)
is move the world

with the cam fixed
Posted By: MMike

Re: Almost finished!!! Spherical Planet Gravity Test - 05/15/09 04:45

this can work for Atomic level molecules simulation? at least the atraction?
Posted By: KiwiBoy

Re: Almost finished!!! Spherical Planet Gravity Test - 05/15/09 05:36

Simply place a model in centre then vec_dist it and let .z do all the work.
You will always be attracted to where ever the model is smile

I think the tilt thing is due to flipping the model upside down where you then encounter gimbol lock probs.
Posted By: Carlos3DGS

Re: Almost finished!!! Spherical Planet Gravity Test - 05/15/09 11:38

@ Felixsg: That is what I was doing at first, but then I realized that would only work if I was planning to not have anything more than just the ball (an empty planet) in my game forever.
When I start placing people, vegetation, animals, etc... well then each time I take one one step in any direction I would have to start rotating tons of stuff, that would be crazy for the processor, it really is better to just calculate one model's rotation than hundreds of modle's rotations. Besides, if I were to find a way to correclty rotate hundreds of them then i would be able to do just one correctly also (and easier) smile
Nice idea though! I started with that idea also!

@ MMike: If I get it to work correctly... Yes, it will work perfectly for your work. Feel free to copy the code and play around with it. But i'm kind of stuck now, hope I get some help to finish those problems im having.

@ KiwiBoy: If you use the world coordinates its much more complicated than just modifying z. If you are thinking of z relative to the player model (model's axis) then that is exactly what im doing. But for that to work the player's rotation must be calculated correctly so his z axis is always pointing to the center of the planet (actualy i rotate him so his z axis is towards the center, then I trace towards the core to get the distance to the surface of the planet so he can actually even cimb mountains ans stuff). Currently it is working perfectly for walking forwards/back and strafing side to side, the problem is when I want the player to turn to his left/right (for some reason he likes to face the north pole, and if I try to make him turn away from that either nothing happens, or strange crazy rotations occur).

Please someone help me cry
Posted By: atari98

Re: Almost finished!!! Spherical Planet Gravity Test - 05/15/09 11:45

PLease write in German!!
I hate English!!
Posted By: Cowabanga

Re: Almost finished!!! Spherical Planet Gravity Test - 05/15/09 11:47

Originally Posted By: atari98
PLease write in German!!
I hate English!!

Can't you understand something simple like this?
Posted By: Carlos3DGS

Re: Almost finished!!! Spherical Planet Gravity Test - 05/15/09 12:23

I am not german, and I do not know german, sorry.

It wouldnt hurt to work on your manners, I have never barged in any german conversation to shout "I hate German!!!", LoL...
Posted By: MMike

Re: Almost finished!!! Spherical Planet Gravity Test - 05/15/09 13:42

WTF.. english is internacional, learn it , or you left behind.. its your choice. Sorry to disapoint you but german is not necessary like english...
Posted By: Carlos3DGS

Re: Almost finished!!! Spherical Planet Gravity Test - 05/15/09 14:04

Added the download to my main post.
It contains my project files and also a compiled version.

Tweaked code a little but still dosent work (also changed this in main post)
Posted By: Jaxas

Re: Almost finished!!! Spherical Planet Gravity Test - 05/15/09 19:22

not bad, i like it smile
Posted By: Carlos3DGS

Re: Almost finished!!! Spherical Planet Gravity Test - 05/16/09 03:05

Thanks smile

Not finished though, there are still some flaws I hope I can fix.
Posted By: Carlos3DGS

Re: Almost finished!!! Spherical Planet Gravity Test - 05/16/09 15:07

by the way, I still need some help. If anyone could take a look at my code I would apreciate any help
Posted By: VeT

Re: Almost finished!!! Spherical Planet Gravity Test - 05/19/09 18:45

Nice contribution, but there is still Gimbal lock on the top of the sphere
Is player uses normal to surface, or he is using the center of the sphere?
Posted By: Carlos3DGS

Re: Almost finished!!! Spherical Planet Gravity Test - 05/21/09 20:07

It uses the center of the sphere. I dont want to use the surface's normal because then, when he is on a steep slope he will not be at a strange angle. (would get a wierd effect looking like matrix wall-walking)
Posted By: Grafton

Re: Almost finished!!! Spherical Planet Gravity Test - 05/24/09 18:20

Yep, you are experiencing gimbal lock.
when the game "prey" came out, I did a spherical world demo
and an "any orientation" wall walking demo that dosent
experience gimbal lock . Check those out to see my solution.
Posted By: Carlos3DGS

Re: Almost finished!!! Spherical Planet Gravity Test - 05/25/09 00:08

I have downloaded several times the "prey_.zip" file in the link "Wall And Ceiling Walking Demo" in your signature, but when I try to unzip it with winrar or 7zip I always get an error.
Posted By: EvilSOB

Re: Almost finished!!! Spherical Planet Gravity Test - 05/25/09 00:14

Same here. "Unexpected end fof archive"
And theres no source in there. (due to the error?)
Any chance of seeing the source code?
Posted By: Carlos3DGS

Re: Almost finished!!! Spherical Planet Gravity Test - 05/25/09 11:51

in the past I had this same problem with another file. and when it was re-compressed and re-uploaded it worked.
Posted By: Grafton

Re: Almost finished!!! Spherical Planet Gravity Test - 05/25/09 17:42

Thanks for letting me know, the zip was indeed bad. I managed to find the original source, recompile it, and upload it. Should be fine now.
Posted By: Carlos3DGS

Re: Almost finished!!! Spherical Planet Gravity Test - 05/27/09 00:15

the download works fine now, thaks.
Cool, something like that is what I am trying to do. Any chance I could get a look at the source code for that?
Posted By: Grafton

Re: Almost finished!!! Spherical Planet Gravity Test - 05/27/09 08:39

I will probably just submit it to the AUM or Acknex resources so it dosent get
lost.

Basically, just use c_trace aligned with your player to get the normal of the
polygon directly beneath, then align your character to it. For convex surface,
you really need to interpolate with something like vec_lerp, especially if the
surface is low poly.
Posted By: KiwiBoy

Re: Almost finished!!! Spherical Planet Gravity Test - 05/27/09 08:45

I love that demo, play it quite often mindlessly laugh and try and make him drop in funny places looking for the 'forever drop' smile

I dont know if your portal one is the one I pkayed with too but it was another goody.
Posted By: EvilSOB

Re: Almost finished!!! Spherical Planet Gravity Test - 05/27/09 09:16

Originally Posted By: Grafton
... then align your character to it. ...
This is the part I have the problem with.
I keep getting gimbal lock near the equator. And I have tried dozens of way around it but no luck.
I'd like to see the code of one that works, so I can understand what Im doing stupid wrong.
(BTW, I'm running around the inside of a sphere, not the outside)
Posted By: Grafton

Re: Almost finished!!! Spherical Planet Gravity Test - 05/27/09 21:57

Here is one from 2006, "walking inside a sphere".
The camera stinks, and the sphere is low poly, but it uses the same idea as the prey demo.
Posted By: EvilSOB

Re: Almost finished!!! Spherical Planet Gravity Test - 05/27/09 22:06

Ooooh. Yummy! Thanks Grafton. Very generous.
I dont have time to look at it right now, maybe later today.

WARNING :: I may have questions.

Once again, many thanks...
Posted By: KiwiBoy

Re: Almost finished!!! Spherical Planet Gravity Test - 05/28/09 00:33

Impressive! Be great to have it do it on a larger model sphere but then you wouldnt need wall walking, just move the world smile
© 2024 lite-C Forums