Gravity and A7...

Posted By: Helghast

Gravity and A7... - 06/02/08 09:11

Ok, so i have been working a lot lately on creating an RPG template code... all aspects work good, except gravity...

I had implemented kingdom hearts tutorial, and that worked, to some extent. What happened that when running up hills (my levels are made from models with polygon collision) at low FPS, he wouldnt climb the hill at all, and just stand at the bottom of it doing nothing.

So i decided i would simply use single point collision and snap the player to that target.z value (as i have done forever in A6 and proved working!)

great, so i take this (A6) code:
Code:
function set_gravity
{
	var target_trace[3]; // variable for temp storage of the trace

	vec_set(target_trace, my.x);
	target_trace.z -= 5000;
	trace_mode = ignore_me + ignore_sprites + ignore_models + use_box + ignore_passable;
	my.z = -trace(my.x, target_trace);
}


and convert that to A7:
Code:
function handle_gravity() {// pulls objects to the ground ;) *hence the name "gravity":P*
	VECTOR target_trace; // variable for temp storage of the trace

	vec_set(target_trace, my.x);
	target_trace.z -= 5000;
	result = c_trace(my.x, target_trace, IGNORE_ME | IGNORE_PASSABLE | USE_BOX | USE_POLYGON);
	
	vec_set(my.z, target.z);
	my.z += 25;
}


Guess what... it doesnt work, at all!
can anyone here share a code that does work with single point "gravity" ? I'm really desperate for this!

thanks in advance,

Dennis
Posted By: Spirit

Re: Gravity and A7... - 06/02/08 09:34

Well I would say neither of those codes could have worked.

In the A6 code youre setting the z position to the result of a trace, this does not make sense to me unless by some chance the floor is exactly at 0 position.

The A7 code will even crash, youre using vec_set with a z value, this will write past the target vector and trash the memory. You probably meant to change the z value only but can not use vec_set then.

For gravity theres no difference between A6 and A7 so you can use the same function. This is from the online manual:

Code:
// determine the ground distance by a downwards trace
		var dist_to_ground; 
		VECTOR vFeet;
		vec_for_min(vFeet,me); // vFeet.z = distance between player origin and lowest player vertex
		result = c_trace(my.x, vector(my.x,my.y,my.z-5000),IGNORE_ME | IGNORE_PASSABLE | USE_BOX);
		if (result > 0)
			dist_to_ground = my.z + vFeet.z - target.z; // the distance between player's feet and the ground
		else
			dist_to_ground = 0;


and then:

m.z -= dist_to_ground;

or:

my.z = target.z - vFeet.z; (dont need dist_to_ground in that case)

I hope this helps.
Posted By: Helghast

Re: Gravity and A7... - 06/02/08 11:07

awh! you are the man!! this works exactly as intended laugh

and yeah, i've never been too good at writing gravity codes...
I never actually had any more problems with something else come to think of it XD anyway, thanks!

however, where did you find this in the manual ? i looked, but couldnt find it (local manual though...).

kind regards and thanks for the help,
Posted By: WINBERRY

Re: Gravity and A7... - 06/19/08 15:37

Spirit,
Thanks for the code!
Posted By: NL_3DGS_n00b

Re: Gravity and A7... - 06/19/08 19:11

This code works very nice, I was looking for it too.
I have a question regarding to this code.
How can I make the player jump.
(By space key for example)

Right now I have this in my player action:

Code:
VECTOR vFeet;
var dist_to_ground; 
vec_for_min(vFeet,me);


And this above in in the WHILE loop which the player movement is in.

Code:
result = c_trace(my.x, vector(my.x,my.y,my.z-5000),IGNORE_ME | IGNORE_PASSABLE | USE_BOX);
if (result > 0)
	dist_to_ground = my.z + vFeet.z - target.z;
else
	dist_to_ground = 0;


So how can I make this work with jumping?
© 2024 lite-C Forums