Posted By: CodeMaster
Pushing object with PhysX - 05/20/20 18:01
I need help how to implement physics in my code, so the player can push objects, nothing more. I found 3run have a great example (59_rigid_template) but I want try implement this in my player code. When I register my player into physX, after that he can't move anymore, because now physics has primacy over my movement code. Is there a some easy way to make this? If I understand good, it has to do with pXent_getvelocity? Here is my code:
Code
#include <acknex.h> #include <default.c> #include <windows.h> #include <strio.c> #include <ackphysX.h> var ph_gravity = 9.81; VECTOR camera_move_to; var camera_distance = 700; var camera_tilt; #define movement_speed skill2 #define rot_pan_to skill15 #define rot_force_pan skill16 #define rot_velocity_pan skill19 #define move_x skill22 #define move_y skill23 #define move_z skill24 #define force_x skill25 #define force_y skill26 #define force_z skill27 #define velocity_x skill28 #define velocity_y skill29 #define velocity_z skill30 #define foot_height skill38 #define soil_contact skill39 #define soil_height skill40 #define store_movement_speed skill56 var result_local; var result2; var pkey_up;var pkey_down;var pkey_left;var pkey_right; var up_press;var down_press;var left_press;var right_press; STRING* key__up = "cuu"; STRING* key__down = "cud"; STRING* key__left = "cul"; STRING* key__right = "cur"; VECTOR temp; VECTOR temp_local; ANGLE temp2; ANGLE temp3; VECTOR temp4; function handle_input_startup (){ while (1) { pkey_up = 0; pkey_down = 0; pkey_left = 0;pkey_right = 0; if (key_pressed(key_for_str(key__up)) == 1) { pkey_up = 1; } if (key_pressed(key_for_str(key__down)) == 1) { pkey_down = 1; } if (key_pressed(key_for_str(key__left)) == 1) { pkey_left = 1; } if (key_pressed(key_for_str(key__right)) == 1) { pkey_right = 1; } if (pkey_up == 0 && up_press == 1) { up_press = 0; } if (pkey_down == 0 && down_press == 1) { down_press = 0; } if (pkey_left == 0 && left_press == 1) { left_press = 0; } if (pkey_right == 0 && right_press == 1) { right_press = 0; } camera_distance -= mickey.z * time_step; camera_distance = clamp(camera_distance, 200, 1000); wait(1); } } function rotate_entity(ANGLE* rotate_angle,var rotate_speed) { if (my.pan == rotate_angle.pan) { return; } temp.x = ang(rotate_angle.pan - my.pan); if (temp.x > 0) { result_local = 1; } if (temp.x < 0) { result_local = -1; } my.rot_force_pan = rotate_speed * 0.3 * ang(rotate_angle.pan - my.pan); my.rot_velocity_pan += (time_step * my.rot_force_pan) - (minv(time_step*0.7,1) * my.rot_velocity_pan); temp.x = ang(rotate_angle.pan - (my.pan + my.rot_velocity_pan)); if ((temp.x > 0 && result_local == -1) || (temp.x < 0 && result_local == 1)) { my.rot_velocity_pan = ang(rotate_angle.pan - my.pan); } c_rotate(my,vector(my.rot_velocity_pan * time_step,0,0),IGNORE_PASSABLE|IGNORE_YOU|USE_AXIS|USE_POLYGON|GLIDE); } function move_rotate_variable(variable_to_move,rotate_to_angle,rotate_speed) { if (variable_to_move == rotate_to_angle) { return(variable_to_move); } temp_local.x = ang(rotate_to_angle - variable_to_move); if (temp_local.x > 0) { result_local = 1; } if (temp_local.x < 0) { result_local = -1; } temp_local.y = 0.3 * rotate_speed * result_local; temp_local.x = ang(rotate_to_angle - (variable_to_move + temp_local.y)); if ((temp_local.x > 0 && result_local == -1) || (temp_local.x < 0 && result_local == 1)) { temp_local.y = ang(rotate_to_angle - variable_to_move); } return(variable_to_move + temp_local.y); } function player_gravity(){ if(my.force_z <= 0){ vec_set(temp,vector(my.x,my.y,my.z-150)); c_trace(my.x,temp,IGNORE_PASSABLE | IGNORE_ME | USE_BOX); if(!trace_hit) vec_set(target,temp); my.soil_height = target.z; } if(my.z > my.soil_height+(5+20*my.soil_contact)*time_step || my.force_z > 0) { my.soil_contact = 0; my.force_z = maxv(my.force_z-9*time_step,-90); } else{ c_move(me,nullvector,vector(0,0,my.soil_height-my.z),IGNORE_PASSABLE); my.soil_contact = 1; my.force_z = 0; // jump code } if(my.force_z) c_move(me,nullvector,vector(0,0,maxv(my.force_z*time_step,my.soil_height-my.z)),IGNORE_PASSABLE); if(HIT_TARGET && normal.z < -0.5) my.force_z = minv(my.force_z,0); } function handle_player_input() { my.velocity_x = 0; my.velocity_y = 0; my.move_x = 0; my.move_y = 0; temp2.pan = -1000; temp3.pan = 0; if (pkey_up == 1 && pkey_down == 0 && pkey_left == 0 && pkey_right == 0) { temp2.pan = camera.pan; } if (pkey_down == 1 && pkey_up == 0 && pkey_left == 0 && pkey_right == 0) { temp2.pan = camera.pan + 180; } if (pkey_left == 1 && pkey_down == 0 && pkey_up == 0 && pkey_right == 0) { temp2.pan = camera.pan + 90; temp3.pan += 5 * time_step; } if (pkey_right == 1 && pkey_down == 0 && pkey_left == 0 && pkey_up == 0) { temp2.pan = camera.pan - 90; temp3.pan -= 5 * time_step; } if (pkey_up == 1 && pkey_left == 1 && pkey_right == 0 && pkey_down == 0) { temp2.pan = camera.pan + 45; temp3.pan += 2.5 * time_step; } if (pkey_up == 1 && pkey_right == 1 && pkey_left == 0 && pkey_down == 0) { temp2.pan = camera.pan - 45; temp3.pan -= 2.5 * time_step; } if (pkey_down == 1 && pkey_left == 1 && pkey_right == 0 && pkey_up == 0) { temp2.pan = camera.pan + 135; temp3.pan += 6 * time_step; } if (pkey_down == 1 && pkey_right == 1 && pkey_left == 0 && pkey_up == 0) { temp2.pan = camera.pan - 135; temp3.pan -= 6 * time_step; } //move player my.store_movement_speed = my.movement_speed; if (temp2.pan != -1000) { my.rot_pan_to = temp2.pan; my.velocity_x = fcos(temp2.pan,my.store_movement_speed * time_step); my.velocity_y = fsin(temp2.pan,my.store_movement_speed * time_step); } else { my.rot_pan_to = my.pan; } } function process_movement() { result2 = my.movement_speed; accelerate(my.force_x,my.velocity_x*0.85,0.85); accelerate(my.force_y,my.velocity_y*0.85,0.85); rotate_entity(my.rot_pan_to,1); c_move(my,my.move_x,vector(my.force_x,my.force_y,0),IGNORE_PASSABLE | GLIDE); } function handle_camera() { camera_tilt = clamp(camera_tilt,-54,-54);//50 temp.x = fcos(camera_tilt,-camera_distance); vec_set(camera_move_to.x,vector(my.x + fcos(camera.pan,temp.x),my.y + fsin(camera.pan,temp.x),my.z + 20 + fsin(camera_tilt,-camera_distance))); temp.x = minv(1,0.5 * time_step); temp4.x = minv(1,0.1 * time_step); camera.x += temp4.x*(camera_move_to.x - camera.x); camera.y += temp.x*(camera_move_to.y - camera.y); camera.z = player.z + 60; } action ph_ball(){ wait(1); c_setminmax(my); set(my, POLYGON); pXent_settype(my, PH_RIGID, PH_SPHERE); } action player_act() { player = my; my.emask |= (ENABLE_SCAN|ENABLE_DETECT|ENABLE_ENTITY); my.rot_pan_to = my.pan; while (1){ //the main loop player_gravity(); handle_player_input(); process_movement(); handle_camera(); wait(1); } } void level_init_physics(){ physX_open(); pX_setunit(0.02); ph_fps_max_lock = 120; ph_check_distance = 2; pX_setccd(1); pX_setgravity(vector(0, 0, -ph_gravity)); } void main (){ video_set(1920, 1045, 32, 2); collision_mode = 2; level_init_physics(); wait(3); level_load("level.wmb"); }