I was working through the Lite-C workshop tutorials and I got to workshop 19: Physics
I guess the tutorials were written for A7 because they it didn't work at first, but I got that part sorted out pretty quickly when I found the docs about the pX_ methods.
I wanted to extend the marble game a little bit by adding a jumping feature. The idea is that if the player presses the space bar, the ball will jump.
Well, I got the jumping part working pretty quickly, but I want to make sure the user can only jump if the ball is actually touching the ground (no jumping forever in midair).
I can't figure out how to detect if the ball is at rest on the ground.
Can anybody tell me how I can tell if the ball entity is on the ground or if it is in "free-fall"?
Thanks in advance.
Here is the script file so far (slightly edited version of Lite-C workshop 19: Physics
///////////////////////////////////////////////////////////////
// ODE version of script19
// Only for use with A7
#include <acknex.h>
#include <default.c>
#include <ackphysX.h>
///////////////////////////////////////////////////////////////
ENTITY* ball;
VECTOR* hit_distance;
var is_touching;
function EventTrigger()
{
is_touching = false;
switch(event_type)
{
case (EVENT_FRICTION):
{
is_touching = true;
}
break;
}
}
function phys_Controller()
{
VECTOR torque;
VECTOR force;
vec_zero(torque);
vec_zero(force);
pXent_settype (my, PH_RIGID, PH_SPHERE); // set the physics entity type
pXent_setmass (my, 3); // and its mass
pXent_setfriction (my, 500); // set the friction
pXent_setdamping (my, 40, 40); // set the damping
pXent_setelasticity (my, 50); // set the elasticity
// my.emask |= ENABLE_FRICTION | ENABLE_BLOCK | ENABLE_ENTITY | ENABLE_IMPACT;
my.event = EventTrigger;
pXent_settriggerflag(my, NX_TRIGGER_ENABLE, 1);
var space_pressed = false;
is_touching = true;
while (true)
{
torque.x = 150 * time_step * (key_cur - key_cul); // move the ball using the cursor keys
torque.y = 150 * time_step * (key_cuu - key_cud); // 25 sets the x / y movement speeds
torque.z = 0; // no need to move on the vertical axis
force.z = 0;
if ( key_space && !space_pressed && is_touching )
{
force.z = 1000;
space_pressed = true;
is_touching = false;
}
space_pressed = key_space;
pXent_addtorqueglobal (my, torque); // add a torque (an angular force) to the ball
pXent_addforcecentral (my, force);
camera.x = my.x - 300; // keep the camera 300 quants behind the ball
camera.y = my.y; // using the same y with the ball
camera.z = 1000; // and place it at z = 1000 quants
camera.tilt = -60; // make it look downwards
wait (1);
}
}
function main()
{
physX_open();
level_load("roller.wmb"); // load the level
ball = ent_create ("ball.mdl", vector(-400, 0, 100), phys_Controller); // create the ball
pX_setgravity (vector(0, 0, -9.81)); // set the gravity
}
You'll notice from the code sample, that I tried to set the emask to some events and I set the event property of the entity, but that didn't work... The event never got called.
So I commented that out.
I then tried to use the "pXent_settriggerflag" method, but that turned my entity into a "trigger" object (I guess) and it just falls through the floor... BUT, my EventTrigger method did get called when it hit the floor...
So how can I correctly check if the ball is actually touching the ground? This should be a common feature in most games.
Thanks in advance!