0 registered members (),
677
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Almost finished!!! Spherical Planet Gravity Test
#265913
05/15/09 03:30
05/15/09 03:30
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
OP
User
|
OP
User
Joined: Oct 2008
Posts: 513
|
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) #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=R5U67BBLKeys: (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
|
|
|
Re: Almost finished!!! Spherical Planet Gravity Test
[Re: MMike]
#265920
05/15/09 05:36
05/15/09 05:36
|
Joined: Apr 2009
Posts: 298 Southern Oceans
KiwiBoy
Member
|
Member
Joined: Apr 2009
Posts: 298
Southern Oceans
|
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 I think the tilt thing is due to flipping the model upside down where you then encounter gimbol lock probs.
Use the 'manual' Luke, the manual is your friend. 'Self reminder' My WebPage
|
|
|
Re: Almost finished!!! Spherical Planet Gravity Test
[Re: KiwiBoy]
#265972
05/15/09 11:38
05/15/09 11:38
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
OP
User
|
OP
User
Joined: Oct 2008
Posts: 513
|
@ 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) 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
|
|
|
|