Give me gravity please

Posted By: croman

Give me gravity please - 12/17/08 14:34

Hi,

Does anyone have a gravity script that is working? It doesnt matter if it's lite-c or c-script(i'll convert it) just that it's a working gravity script.
Please...:)

Thnx
Posted By: Michael_Schwarz

Re: Give me gravity please - 12/17/08 15:58

c_move(me, vector(0,0,-9), nullvector, IGNORE_ME);
Posted By: Joozey

Re: Give me gravity please - 12/17/08 16:26

Mathematically it should be:
Quote:
y(t) == y(0) + v(0) t + (1/2) g t2

where:
* y(t), height depending on time;
* y(0), starting height;
* v(0), starting speed;
* g, acceleration ( gravitational acceleration g is 9.98 on earth, 3.73 on mars);
* t, time.


In the example of a falling panel:
Code:
if (falling == true) {
  panel.pos_y = fallheight + (0 * falltime) + 0.5 * 9.98 * (falltime*falltime); //gravity formula, change (0 * falltime) for giving an initial speed
  falltime += time_step; //falltime is counting
}

if (falling == false) {
  falltime = 0; //reset falltime
  fallheight = panel.pos_y; //record height as long as we're on ground, can then be used as initial height when in the air
}

Posted By: croman

Re: Give me gravity please - 12/17/08 16:53

thnx. i'll try to implement that method smile but i hoped that someone has a piece of code with c_trace and c_move and everything else for gravity
Posted By: Joozey

Re: Give me gravity please - 12/17/08 17:10

Then use Michael's approach? smile
Posted By: croman

Re: Give me gravity please - 12/17/08 17:13

i tried :P
i stuck quite often.
i even tried to use that khmovement gravity but it has some major problems with lite-c - dont know why

well it seems i'll use yours approach smile
Posted By: Quad

Re: Give me gravity please - 12/17/08 17:29

this was from Davids khmovement tut i guess(only the gravity part)

Code:
#define jumping_height 20
		vec_set(vec1_from,my.x);
		vec_set(vec1_to,my.x);
		vec1_to.z -= 1000;
	height = c_trace(vec1_from, vec1_to,IGNORE_ME|IGNORE_PASSABLE|IGNORE_SPRITES|USE_BOX); 
		
		if (key_space)
		{
			if (height < 2)
			{
				height = 1;
				move_vec.z = jumping_height*time_step;
			}
		}
		
		if (height > 0)
		{
			move_vec.z-=3 * time_step;
		  	if (move_vec.z < (height * -1))
			{
				move_vec.z = height * -1;
			}
		}
		else
		{
			move_vec.z = -1 * height;
		}
		
		c_move(me, move_vec, nullvector,GLIDE|IGNORE_SPRITES|IGNORE_PASSABLE|USE_BOX);


you have to definfe move_vec,height and such ofc.
Posted By: Xarthor

Re: Give me gravity please - 12/17/08 17:34

Hint:
use two c_move instructions.
One for gravity, the other one for movement in x and y direction.
This hint is also given in the manual by the way.
Posted By: croman

Re: Give me gravity please - 12/17/08 17:53

thnx Quadraxas

Xarthot, i will. can you explain why exactly?
Posted By: Xarthor

Re: Give me gravity please - 12/17/08 18:25

The manual states that it might come to collision problems (getting stuck in the ground e.g.) if you use it in one instruction.
I currently have no further information available.
Posted By: croman

Re: Give me gravity please - 12/17/08 18:26

yup i've seen it by myself moment ago. thnx
Posted By: testDummy

Re: Give me gravity please - 12/17/08 21:05

Quoting jcl.
Quote:
And anyway, a moving actor's bounding box should never touch the ground - at least that's what we're preaching since A4 days. Otherwise you'll get collision with the ground when moving that entity.

Uh huh!?!.

Quoting Xarthor.
Quote:
Hint:
use two c_move instructions.
One for gravity, the other one for movement in x and y direction.
This hint is also given in the manual by the way.

It seemed here, for simply avoiding stuck conditions, 2 c_move invocations were unnecessary.

In A6, it almost seems that c_move / c_trace instructions are ridiculously expensive in terms of performance.
Maybe those instructions were optimized in A7.

Posted By: Pappenheimer

Re: Give me gravity please - 12/17/08 22:16

And now, me with a quote from the online manual, a walk action with in-build gravity wink :
Click to reveal..
Example (lite-C):

// simple function for walking over ground
// control the player with the WASD keys
// player origin must be at the model center
// bounding box must be smaller than the player!
action player_walk()
{
// if necessary, adjust the bounding box to give the entity 'floor room' (see remarks)
// my.min_z *= 0.5;

var speed_down = 0; // downward speed by gravity
var anim_percent = 0; // animation percentage
VECTOR vFeet;
vec_for_min(vFeet,me); // vFeet.z = distance from player origin to lowest vertex

while (1)
{
// rotate the player using the [A] and [D] keys
my.pan += 5*(key_a-key_d)*time_step;

// determine the ground distance by a downwards trace
var dist_down;
if (c_trace(my.x,vector(my.x,my.y,my.z-5000),IGNORE_ME | IGNORE_PASSABLE | USE_BOX) > 0)
dist_down = my.z + vFeet.z - target.z; // get distance between player's feet and the ground
else
dist_down = 0;

// apply gravity when the player is in the air
if (dist_down > 0) // above floor, fall down with increasing speed
dist_down = clamp(dist_down,0,accelerate(speed_down,5,0.1));
else // on or below floor, set downward speed to zero
speed_down = 0;

// move the player using the [W] and [S] keys
var dist_ahead = 5*(key_w-key_s)*time_step;
dist_ahead = sign(dist_ahead)*(abs(dist_ahead) + 0.5*dist_down); // adapt the speed on slopes
c_move(me,vector(dist_ahead,0,0),vector(0,0,-dist_down),IGNORE_PASSABLE | GLIDE); // move the player

// animate the player according to its moved distance
if (dist_ahead != 0) // player is moving ahead
{
anim_percent += 1.3*dist_ahead; // 1.3 = walk cycle percentage per quant
ent_animate(me,"walk",anim_percent,ANM_CYCLE); // play the "walk" animation
}
else // player stands still
{
anim_percent += 5*time_step;
ent_animate(me,"stand",anim_percent,ANM_CYCLE); // play the "stand" animation
}
wait(1);
}
}


Hope, you don't mind.
© 2024 lite-C Forums