Jumping & stairs code required

Posted By: DonaldThief

Jumping & stairs code required - 07/25/13 13:51

Hello, everyone

I am using the usual gravity code from the AUM ,with some modification to make something like acceleration:

I put the following in a while loop:

Code:
VECTOR temp, gravity_speed;
var distance_to_ground;
var max_gravity = -10*time_step;
vec_set(temp.x,my.x);
      temp.z -= 5000;
 
      distance_to_ground = c_trace(my.x,temp.x,IGNORE_ME | USE_BOX | IGNORE_PASSABLE);
      gravity_speed.z = -(distance_to_ground - 17);
      gravity_speed.z = clamp(gravity_speed.z,max_gravity,5);
      max_gravity += -5*time_step;
      max_gravity = clamp(max_gravity,-35*time_step,-10*time_step);
      gravity_speed.x = 0;
      gravity_speed.y = 0;
      
      c_move(my,nullvector,gravity_speed.x,GLIDE | IGNORE_PASSABLE);
      
      if(distance_to_ground <= 0.1) max_gravity = -10*time_step;





1) First, I want to increase the efficiency of the code and I mean the value 17 (in seventh line) ,how to set a proper value that suits the model (distance between the origin and feet) 'cause I tried that a lot using vec_for_min and stuff ,but it didn't work and the model moves upwards

I added this before the code above:

Code:
vec_for_min(temp2.x,my);
leg_height = my.z - temp2.z;



then I replaced "17" with "leg_height"


1) I'd like to have a functional jumping code that suits the code above

2) There's a problem with this code which is going upstairs. How can I make it walk on the stairs instead of considering it as an obstacle?



ThanQ for reading

Posted By: Superku

Re: Jumping & stairs code required - 07/25/13 21:04

Have you seen the following article and code already: http://opserver.de/mwik4/index.php?title=Gravity ?

I don't think I've tested it with stairs but it could work already.
Posted By: DonaldThief

Re: Jumping & stairs code required - 07/26/13 13:45

But how does vec_for_min work? Is it relative to entity origin or world origin? (if the minimum vertex is below the entity's origin by 10 quants, will vec_for_min vector.z = (-10) or (entity position-10)?) 'cause I can't figure out what foot_height skill represents.
Posted By: Superku

Re: Jumping & stairs code required - 07/26/13 13:50

Haha oh well. Did you read the article or only check the code?

Code:
vec_set(temp,vector(my.x,my.y,my.z-150+my.foot_height));
c_trace(my.x,temp,IGNORE_PASSABLE | IGNORE_ME | USE_BOX);
if(!trace_hit) vec_set(target,temp);


The temp vector obviously is set 2 lines before your questioned vec_set instruction. The idea is simple, we declare out trace target "temp", then we c_trace and should we not hit anything the target vector would be the nullvector, thus we overwrite it with our desired position (the content of temp). The reason for this is that we only want to have a vector somewhere below the player's feet, in fact we are normally only interested in the z-component of the target vector.

Yes, target and hit.x are basically the same vector, using target, the normal vector and so on is just the old school way of doing it (which I prefer) because back then in the days there was no hit struct.

vec_for_min is well explained in the manual, as pretty much every other lite-C instruction: http://www.conitec.net/beta/vec_for_min.htm

EDIT: You have changed/ deleted your old post, so my answer may seem somehow off to other users.
Posted By: DonaldThief

Re: Jumping & stairs code required - 07/26/13 13:57

Sorry, but I find this code difficult to understand. And the stuff in the web page is different from the downloadable code......

BTW, if my.z > my.soil_height ,why do we set my.speed_z to zero? We have to perform a vertical motion

EDIT:

Originally Posted By: Superku
Haha oh well. Did you read the article or only check the code?


I didn't understand both confused


Originally Posted By: Superku

Code:
vec_set(temp,vector(my.x,my.y,my.z-150+my.foot_height));
c_trace(my.x,temp,IGNORE_PASSABLE | IGNORE_ME | USE_BOX);
if(!trace_hit) vec_set(target,temp);


The temp vector obviously is set 2 lines before your questioned vec_set instruction. The idea is simple, we declare out trace target "temp", then we c_trace and should we not hit anything the target vector would be the nullvector, thus we overwrite it with our desired position (the content of temp). The reason for this is that we only want to have a vector somewhere below the player's feet, in fact we are normally only interested in the z-component of the target vector.


This was the only thing in the code that I understood. And it was a mistake to post my previous post that I deleted as I was confused about the "target" vector (I didn't know what it is as I'm a B-E-G-I-N-N-E-R)



Originally Posted By: Superku

vec_for_min is well explained in the manual, as pretty much every other lite-C instruction: http://www.conitec.net/beta/vec_for_min.htm



I know it is well-explained but I just want to be more sure


Originally Posted By: Superku


EDIT: You have changed/ deleted your old post, so my answer may seem somehow off to other users.


OK. I DELETED it as ,at that particular point, I realised what is "target".
Posted By: Superku

Re: Jumping & stairs code required - 07/26/13 14:02

The stuff in the wiki article is not different to the downloadable code.

We don't set it to 0, only in the other case.
Posted By: DonaldThief

Re: Jumping & stairs code required - 07/26/13 14:17

Posted By: Superku

Re: Jumping & stairs code required - 07/26/13 14:33

Oh, I see, that's just a typo and it's fixed now. However, you could have guessed that from the following code (and/ or text).
Posted By: DonaldThief

Re: Jumping & stairs code required - 07/27/13 14:17

@Superku your gravity is pretty good and works perfectly in your sample project ,but it doesn't fit the rest of my player's action as I use some dumb way for animating, moving...etc. and I got some problems with animation ,not because of your code ,but because of my player's code and the player's feet kept sinking into the ground

So what did I do? I modified your code and made it more simple (at least for me) ,but I've got less problems smile as I no longer have an animation problem (actually the animation works perfectly) ,but I still have the sinking-feet problem frown


Here's my code inspired from yours:

Code:
gravity_speed.x = 0;
      gravity_speed.y = 0;
      
      vec_for_min(feet_height.x,my);
      feet_height.z *= my.scale_z;
      
      
      vec_set(temp.x,my.x);
      temp.z -= 5000;
      c_ignore(8,0);
      distance_to_ground = c_trace(my.x,temp.x,IGNORE_ME | USE_BOX | IGNORE_PASSABLE) - abs(feet_height.z);
      DEBUG_VAR(distance_to_ground,100); //It is clear that it is negative!
      
      if(distance_to_ground > 0)
      {
      	gravity_speed.z -= 9*time_step;
      	if(gravity_speed.z < -90) gravity_speed.z = -90;
      }
      else
      {
      	jumping = 0;  //"jumping" is for animation
      	gravity_speed.z = 0;
      	if(key_space)
      	{
      		if(key_space_off)
      		{
      			key_space_off = 0;
      			gravity_speed.z = 65*time_step;
      			jumping = 1;
      		}
      	}
      	else
         {
         	key_space_off = 1;
         }
      }
      
      c_move(my,nullvector,gravity_speed.x,GLIDE | IGNORE_PASSABLE);





Bugs:-

* There's only one bug. The feet of the player (from the "templates" folder) sometimes sink into the ground and sometimes not. Also, I think the reason for this is that "distance_to_ground" is negative after putting a "DEBUG_VAR" instruction







Thank you for always introducing help when needed

Best wishes
Posted By: Ch40zzC0d3r

Re: Jumping & stairs code required - 07/27/13 15:41

Why not just use a min/max function?
Posted By: DonaldThief

Re: Jumping & stairs code required - 07/27/13 15:50

Originally Posted By: Ch40zzC0d3r
Why not just use a min/max function?


What do u mean?
Posted By: Ch40zzC0d3r

Re: Jumping & stairs code required - 07/27/13 18:28

If you limit the variable to -16 it could work, I didnt read the code fully...
Just a simple idea
Posted By: DonaldThief

Re: Jumping & stairs code required - 07/27/13 18:54

Don't know but I am sure it won't work....

Every time I modify the code ,it doesn't work as expected. Either the player model floats on air or it sinks into the ground. I tried all values but there always has been a problem (difficulty of motion on slopes, inability to jump after a previous jump is completely finished...etc.). What makes me very upset is that I don't know what the reason for the bug is


If anyone has time and patience to look into my whole code or only the player action ,just ask for it

Thnx
Posted By: DonaldThief

Re: Jumping & stairs code required - 08/03/13 16:10

Ok. I modified the code a little bit and added some trig ,and it worked fine

ThanQ for helping
© 2023 lite-C Forums