0 registered members (),
1,094
guests, and 1
spider. |
Key:
Admin,
Global Mod,
Mod
|
|
|
player walking!! Help
#444021
07/29/14 04:41
07/29/14 04:41
|
Joined: Jul 2012
Posts: 45
Gino_Fabrizio
OP
Newbie
|
OP
Newbie
Joined: Jul 2012
Posts: 45
|
I need help improving my newly project. While the character walking (in first person), he gets stuck sometimes in the ground, a litle. How I can fix that? here is my code:
#include <acknex.h>
#include <default.c>
STRING* key_forward = "w";
STRING* key_backward = "s";
STRING* key_left = "a";
STRING* key_right = "d";
STRING* key_jump = "space";
STRING* key_run = "shiftl"; // left shift key
var t_player_speed = 25; // player's movement speed
var t_fwdbk_accel = 7; // player's forward / backward acceleration
var t_side_accel = 3; // player's sideway (strafe) acceleration
var t_friction = 1.5; // player's friction - this value should always be greater than 1
var t_fallspeed = 1; // player's fall down speed
var t_jump = 20; // player's jumping force
var t_resilience = 4; // pushes the player upwards a bit if it sinks too much in the floor
var t_disttoground = 64; // distance between the origin of player's model and the ground (used by the camera as well)
var t_camera_h = 12; // horizontal camera acceleration
var t_camera_h_frict = 0.95; // always use a value below 1 here
var t_camera_v = 8; // vertical camera acceleration
var t_camera_v_frict = 0.8; // always use a value below 1 here
var t_players_health = 100; // the player starts with 100 health points
var camera_h_speed = 0;
var camera_v_speed = 0;
VECTOR dist;
VECTOR absdist;
function first_person_camera()
{
vec_set(camera.x, my.x);
// accelerate camera's pan and tilt angles depending on mouse_force.x and mouse_force.y in order to create a smooth camera
camera.pan -= accelerate (camera_h_speed, t_camera_h * (mouse_force.x), t_camera_h_frict);
camera.tilt += accelerate (camera_v_speed, t_camera_v * (mouse_force.y), t_camera_v_frict);
my.pan = camera.pan;
}
function handle_camera()
{
camera.genius = my;
camera.arc = 80;
vec_set(camera.x,vector(my.x,my.y,my.z+30));
my.pan = camera.pan;
camera.pan -= 10 * mouse_force.x * time_step;
camera.tilt += 10 * mouse_force.y * time_step;
camera.tilt = clamp(camera.tilt,-90,90);
}
function handle_movement()
{
if(key_shift)
{
dist.x = 15 * (key_w - key_s) * time_step;
dist.y = 15 * (key_a - key_d) * time_step;
}
else
{
dist.x = 10 * (key_w - key_s) * time_step;
dist.y = 10 * (key_a - key_d) * time_step;
}
}
function handle_gravity()
{
result = c_trace(my.x,vector(my.x,my.y,my.z-1000),IGNORE_PASSABLE|IGNORE_MODELS|USE_BOX);
if(result > 5)
{
absdist.z -= 2 * time_step;
}
else
{
absdist.z = 0;
}
}
#define health skill1
#define state skill2
action player_code()
{
set (my, INVISIBLE); // using a 1st person player
set (my, FLAG2); // will be used by enemies' c_scan instructions to detect the player
var forward_on, backward_on, right_on, left_on, jump_on, run_on;
var current_height = 0, jump_handle;
VECTOR horizontal_speed, vertical_speed, temp;
vec_set(horizontal_speed.x, nullvector); // initialize this vector
vec_set(vertical_speed.x, nullvector); // initialize this vector
player = me;
my.eflags |= FAT | NARROW;
wait(1);
my.health = 400;
while(my.health > 0) // while I'm alive
{
handle_gravity();
handle_movement();
c_move(my,dist,absdist,IGNORE_PASSABLE|GLIDE);
handle_camera();
wait(1);
}
{
// key input start
forward_on = 0; // reset all the key values at the beginning of each frame
backward_on = 0;
right_on = 0;
left_on = 0;
jump_on = 0;
run_on = 0;
// set the proper variables if their corresponding keys are pressed
if(key_pressed(key_for_str(key_forward)))
forward_on = 1;
if(key_pressed(key_for_str(key_backward)))
backward_on = 1;
if(key_pressed(key_for_str(key_left)))
left_on = 1;
if(key_pressed(key_for_str(key_right)))
right_on = 1;
if(key_pressed(key_for_str(key_jump)))
jump_on = 1;
if(key_pressed(key_for_str(key_run)))
run_on = 1;
// key input end
first_person_camera(); // calls the shooter camera function
// player movement start
// player's horizontal speed (forward / backward / sideways) movement uses acceleration and friction as well
horizontal_speed.x = (horizontal_speed.x > 0) * maxv(horizontal_speed.x - time_step * t_friction, 0) + (horizontal_speed.x < 0) * minv(horizontal_speed.x + time_step * t_friction, 0);
if(forward_on)
{
horizontal_speed.x += time_step * t_fwdbk_accel;
horizontal_speed.x = minv(horizontal_speed.x, time_step * t_player_speed * (1 + run_on));
}
if(backward_on)
{
horizontal_speed.x -= time_step * t_fwdbk_accel;
horizontal_speed.x = maxv(horizontal_speed.x, -(time_step * t_player_speed * (1 + run_on)));
}
horizontal_speed.y = (horizontal_speed.y > 0) * maxv(horizontal_speed.y - time_step * t_friction, 0) + (horizontal_speed.y < 0) * minv(horizontal_speed.y + time_step * t_friction, 0);
if(left_on)
{
horizontal_speed.y += time_step * t_side_accel;
horizontal_speed.y = minv(horizontal_speed.y, time_step * t_player_speed * (1 + run_on));
}
if(right_on)
{
horizontal_speed.y -= time_step * t_side_accel;
horizontal_speed.y = maxv(horizontal_speed.y, -(time_step * t_player_speed * (1 + run_on)));
}
// disable the friction, allow smooth gliding along the surfaces
move_friction = 0;
vec_set(temp.x, my.x);
temp.z -= 10000;
current_height = c_trace(my.x, temp.x, IGNORE_ME | IGNORE_PASSABLE | USE_BOX | GLIDE); // compute the distance between player's origin and the floor below its feet
if(current_height < (t_disttoground * 0.97)) // if it is smaller than the default value (64 quants) push the player upwards a bit (resilience) - 0.97 gives the hysteresis
{
vertical_speed.z = t_resilience * time_step;
}
else
{
if(current_height > t_disttoground) // if it is bigger than the default value (64 quants), move the player downwards
{
vertical_speed.z -= t_fallspeed * time_step;
}
else // sitting properly on the floor?
{
vertical_speed.z = 0; // then don't do anything on the z axis
}
}
if((jump_on) && (current_height < t_disttoground)) // jumping while player's feet are placed on the floor?
{
vertical_speed.z = t_jump * 0.25; // push the player upwards
}
// this c_move instruction does all the job, moving the player in the direction given by horizontal_speed (forward, backward, sideways) and vertical_speed (on the z axis)
c_move (my, horizontal_speed.x , vertical_speed.x, IGNORE_PASSABLE | USE_BOX | GLIDE);
// player movement end
wait(1);
}
}
function main()
{
video_set(sys_metrics(0),sys_metrics(1),0,1);
fps_max = 70;
level_load("small.hmp");
wait(2);
ent_create("player.mdl", vector(0,0,100),player_code);
}
Thanks.
|
|
|
Re: player walking!! Help
[Re: Gino_Fabrizio]
#444024
07/29/14 07:35
07/29/14 07:35
|
Joined: Jun 2014
Posts: 97 Lagos, Nigeria
tolu619
Junior Member
|
Junior Member
Joined: Jun 2014
Posts: 97
Lagos, Nigeria
|
This was copied and pasted from the movement code in my own project which doesn't have the problem you complained of. You'll have to change the variable names to yours of course. I tried to delete the parts of my code that aren't directly relevant to the walking but I may have accidentally left some of it in there. If you spot any bits of code that are useless to you, ignore them.
#define speed skill2
#define animation skill3
VECTOR feet;
var distancedown;
var spin;
var mygravity = 0;
if(c_trace(my.x, vector(my.x,my.y,my.z-10000), IGNORE_ME|IGNORE_PASSENTS |USE_BOX) > 0)
distancedown = my.z + feet.z - target.z;
else
distancedown = 0;
if(distancedown > 0)
distancedown = clamp(distancedown, 0, accelerate(mygravity, 9.8, 0.1)); //0.1 is friction, accelerate "mygravity" by 9.8
//clamp limits distance down to lower limit 0 and upper limit accelerate(mygravity, 9.8, 0.1)
else
{mygravity = 0;}
spin = (key_a - key_d);
if((!key_a) && (!key_d))
//spin = (joy_rot.x/10);
spin = -(joy_force.x/4);
my.pan += spin * my.speed/2 * time_step;
distance = my.speed * (key_w - key_s) * time_step;
if ((!key_w) && (!key_s))
distance = my.speed * (joy_force.y) * time_step;
//distance = my.speed * (joy_rot.y/10) * time_step;
distance = sign(distance)*(abs(distance) + 0.5*distancedown); //adapt the speed on slopes
//sign(var X) returns -1 if X <0, 1 if X > 0, and o if X = 0
c_move(me, vector(distance, 0, 0), vector(0, 0, -distancedown), GLIDE | USE_POLYGON |IGNORE_PASSABLE);
my.animation += (distance + spin)/2;
ent_animate(me, "run", my.animation, ANM_CYCLE);
if(distance == 0 && spin == 0) //player didn't move
{
my.animation += 5*time_step;
ent_animate(me, "stand", my.animation, ANM_CYCLE);
}
PUT THESE IN THE PLAYER ACTION BEFORE CALLING THE MOVEMENT FUNCTION
vec_for_min(feet, me); //assigning the lowest position of the model to the vector named "feet"
if((my.eflags&FAT) && (my.eflags&NARROW)) //if both the fat and narrow flags are set
my.z *= 0.5;
|
|
|
|