making player jump with physics engine [SED]

Posted By: hungryhobo

making player jump with physics engine [SED] - 11/24/09 04:58

So, I have a ball and I want the player to be able to make the ball jump. using the physics engine in the tutorial, how can i make my ball jump?

here is what i have so far. not using the physics i was able to make a character jump, which is why there is "fall_handling" and the trace. it can be ignored, i just kept it incase i need it.

///////////////////////////////////////////////////////////////////////////////
//jump_handling_phys()
///////////////////////////////////////////////////////////////////////////////
//uses: move_vec_x, move_vec_y, move_vec_z, fall_distance, jump_distance, fall, jump, player_force_z
function jump_handling_phys()
{
if(my.jump_distance > jump_height && my.jump == 1)
{
my.jump = 0;
my.player_force_z = 0;
my.jump_distance = 0;
}
else
{
if(my.jump == 1 && my.fall == 0)
{
if(my.player_force_z > jump_speed*time)
{
my.player_force_z -= jump_accel*time;
my.jump_distance += my.player_force_z;
}
else
{
my.player_force_z = jump_speed*time;
my.jump_distance += my.player_force_z;
}
}
}

}



//AND MY MOVEMENT ACTION
///////////////////////////////////////////////////////////////////////////////
//ACTION phys_move
///////////////////////////////////////////////////////////////////////////////
//uses: player_force_x, player_force_y, player_force_z, //uses: move_vec_x, move_vec_y, move_vec_z, fall_distance, jump_distance, fall, jump
action phys_move
{

player = me;
my.fall = 0;
my.jump = 0;

///////////////////////////////////////////////////////////////////////////////
//PHYSICS
///////////////////////////////////////////////////////////////////////////////
ph_setgravity(vector(0, 0, -386)); // set the gravity
phent_settype(player, PH_RIGID, PH_SPHERE); // set the physics entity type
phent_setmass(player, 3, PH_SPHERE); // and its mass
phent_setfriction (player, 80); // set the friction
phent_setdamping (player, 40, 40); // set the damping
phent_setelasticity (player, 50, 20); // set the elasticity
///////////////////////////////////////////////////////////////////////////////
while (1)
{

my.player_force_x = 180 * time * (key_cur - key_cul); // move the ball using the cursor keys
my.player_force_y = 180 * time * (key_cuu - key_cud); // 180 sets the x / y movement speeds
my.player_force_z = 0; // no need to move on the vertical axis
phent_addtorqueglobal (player, my.player_force_x); // add a torque (an angular force) to the ball

//FIND FALL DISTANCE
var trace_distance;
vec_set(temp,my.x);
temp.z -= 4000;
trace_mode = IGNORE_SPRITES + ACTIVATE_SONAR + USE_BOX; //set up trace()
my.fall_distance = trace(my.z, temp); //check how high i am and return the number

if(key_space)
{
my.jump = 1;
}
jump_handling_phys();





camera.x = player.x - 200; // keep the camera 300 quants behind the ball
camera.y = player.y; // using the same y with the ball
camera.z = player.z + 300; // and place it at z = 1000 quants
camera.tilt = -50; // make it look downwards



wait (1);
}



}
Posted By: Tobias_Runde

Re: making player jump with physics engine [SED] - 12/03/09 08:21

don't mix old and new Code and don't mix Physik with direct moves.
Here the easy way:

erase the part between

//FIND FALL DISTANCE
and
jump_handling_phys();

and enter this code:

if(key_space)
phent_addcentralforce(my, vector(0,0,xxx));

xxx is the force to jump!
Posted By: Jaeger

Re: making player jump with physics engine [SED] - 12/04/09 18:36

If you know C/C++, I'd suggest you write your own physics from scratch. It's been a long time since I used 3DGS, but I remember how disappointed I was with ODD physics. I should have listened to the 3DGS-experienced guys like darkinferno, when they told me not to use it in the context of my testing project. It worked fine for things like bullets, crates, and simple moving objects. It was absolutely a nightmare for vehicles, avatars/characters and the likes. If you use a physics-object for a player, it's most likely he's just going to fall over a lot. Trying to implement complex vehicle physics and flight physics was even worse. I soon discovered, there was no existing physics API which could do what I needed it to, except proprietary ones that the developers would not sell (like IL-2 Sturmovik's engine).

There was also no way of just "not using" physics in my project. It's a basic requirement of a simulation. Mimicking physics in Lite-C also was a poor solution for our project. It's a beautiful and elegant scripting language, but simply lacked the raw, low-level, turbo-charged power (and speed) we needed. That's when I had to face it; "I've got to create a new engine, oh boy!". laugh So, I did. It's not 100% finished, but for a realistic war simulation like ours, I can't think of anything on the market which will hold a candle to it on completion. And the cool part is, if you know C++ or C#, you can probably implement physics every bit as good for YOUR game (provided you know what you're doing).

So, if physics are as important to YOU as they are to ME, you should probably do the same thing as me. I program in C# 99% of the time, and the code runs every bit as fast (and sometimes faster) than my first mirror implementation in C++. If your heart is set on 3DGS, which there is NOTHING wrong with (it's a great engine), you can use C++, or get the C# wrapper and make a new physics API. You will be happy if you do. It's NO fault of Conitec's devs, but ODD was just unsatisfactory for us. And I think you're in for a LOT of disappointment and headache trying to make physics based players or anything beyond simple physics. You can't even mix direct movement with physics in ODD. smirk In my implementation, direct movement simply overrides the physics if you set a bitflag, OR has no effect if it's == 0x0. So I can force particular types of complex motion when it is so desired, with no need to disable/re-enable physics (which is slooooowwww). You can also mix bitflags (^= or |= operators) to blend direct and PE motion for crazy effects, like pseudo-magnetism! laugh

If you have questions about doing it yourself, you can always pm. I come around at least once every couple days, since I still love the community! <3 laugh
© 2023 lite-C Forums