diff options
Diffstat (limited to 'demos/3d/kinematic_char')
-rw-r--r-- | demos/3d/kinematic_char/cubelib.res | bin | 0 -> 11431 bytes | |||
-rw-r--r-- | demos/3d/kinematic_char/cubio.gd | 96 | ||||
-rw-r--r-- | demos/3d/kinematic_char/engine.cfg | 17 | ||||
-rw-r--r-- | demos/3d/kinematic_char/follow_camera.gd | 92 | ||||
-rw-r--r-- | demos/3d/kinematic_char/kinebody3d.png | bin | 0 -> 6078 bytes | |||
-rw-r--r-- | demos/3d/kinematic_char/level.scn | bin | 0 -> 15257 bytes | |||
-rw-r--r-- | demos/3d/kinematic_char/purple_wood.tex | bin | 0 -> 173520 bytes | |||
-rw-r--r-- | demos/3d/kinematic_char/purplecube.scn | bin | 0 -> 9808 bytes | |||
-rw-r--r-- | demos/3d/kinematic_char/twood.tex | bin | 0 -> 168054 bytes | |||
-rw-r--r-- | demos/3d/kinematic_char/white_wood.tex | bin | 0 -> 169001 bytes |
10 files changed, 205 insertions, 0 deletions
diff --git a/demos/3d/kinematic_char/cubelib.res b/demos/3d/kinematic_char/cubelib.res Binary files differnew file mode 100644 index 0000000000..66b999d78d --- /dev/null +++ b/demos/3d/kinematic_char/cubelib.res diff --git a/demos/3d/kinematic_char/cubio.gd b/demos/3d/kinematic_char/cubio.gd new file mode 100644 index 0000000000..6f12e39db7 --- /dev/null +++ b/demos/3d/kinematic_char/cubio.gd @@ -0,0 +1,96 @@ + +extends KinematicBody + +# member variables here, example: +# var a=2 +# var b="textvar" + +var g = -9.8 +var vel = Vector3() +const MAX_SPEED = 5 +const JUMP_SPEED = 7 +const ACCEL= 2 +const DEACCEL= 4 +const MAX_SLOPE_ANGLE = 30 + +func _fixed_process(delta): + + var dir = Vector3() #where does the player intend to walk to + var cam_xform = get_node("target/camera").get_global_transform() + + if (Input.is_action_pressed("move_forward")): + dir+=-cam_xform.basis[2] + if (Input.is_action_pressed("move_backwards")): + dir+=cam_xform.basis[2] + if (Input.is_action_pressed("move_left")): + dir+=-cam_xform.basis[0] + if (Input.is_action_pressed("move_right")): + dir+=cam_xform.basis[0] + + dir.y=0 + dir=dir.normalized() + + vel.y+=delta*g + + var hvel = vel + hvel.y=0 + + var target = dir*MAX_SPEED + var accel + if (dir.dot(hvel) >0): + accel=ACCEL + else: + accel=DEACCEL + + hvel = hvel.linear_interpolate(target,accel*delta) + + vel.x=hvel.x; + vel.z=hvel.z + + var motion = vel*delta + motion=move(vel*delta) + + var on_floor = false + var original_vel = vel + + + var floor_velocity=Vector2() + + var attempts=4 + + while(is_colliding() and attempts): + var n=get_collision_normal() + + if ( rad2deg(acos(n.dot( Vector3(0,1,0)))) < MAX_SLOPE_ANGLE ): + #if angle to the "up" vectors is < angle tolerance + #char is on floor + floor_velocity=get_collider_velocity() + on_floor=true + + motion = n.slide(motion) + vel = n.slide(vel) + if (original_vel.dot(vel) > 0): + #do not allow to slide towads the opposite direction we were coming from + motion=move(motion) + if (motion.length()<0.001): + break + attempts-=1 + + if (on_floor and floor_velocity!=Vector3()): + move(floor_velocity*delta) + + if (on_floor and Input.is_action_pressed("jump")): + vel.y=JUMP_SPEED + + var crid = get_node("../elevator1").get_rid() +# print(crid," : ",PS.body_get_state(crid,PS.BODY_STATE_TRANSFORM)) + +func _ready(): + # Initalization here + set_fixed_process(true) + pass + + +func _on_tcube_body_enter( body ): + get_node("../ty").show() + pass # replace with function body diff --git a/demos/3d/kinematic_char/engine.cfg b/demos/3d/kinematic_char/engine.cfg new file mode 100644 index 0000000000..b3060b65e0 --- /dev/null +++ b/demos/3d/kinematic_char/engine.cfg @@ -0,0 +1,17 @@ +[application] + +name="Kinematic Character 3D" +main_scene="res://level.scn" +icon="res://kinebody3d.png" + +[input] + +move_forward=[key(Up)] +move_left=[key(Left)] +move_right=[key(Right)] +move_backwards=[key(Down)] +jump=[key(Space)] + +[rasterizer] + +shadow_filter=3 diff --git a/demos/3d/kinematic_char/follow_camera.gd b/demos/3d/kinematic_char/follow_camera.gd new file mode 100644 index 0000000000..0b9ff9bbb2 --- /dev/null +++ b/demos/3d/kinematic_char/follow_camera.gd @@ -0,0 +1,92 @@ + +extends Camera + +# member variables here, example: +# var a=2 +# var b="textvar" + +var collision_exception=[] +export var min_distance=0.5 +export var max_distance=4.0 +export var angle_v_adjust=0.0 +export var autoturn_ray_aperture=25 +export var autoturn_speed=50 +var max_height = 2.0 +var min_height = 0 + +func _fixed_process(dt): + var target = get_parent().get_global_transform().origin + var pos = get_global_transform().origin + var up = Vector3(0,1,0) + + var delta = pos - target + + #regular delta follow + + #check ranges + + if (delta.length() < min_distance): + delta = delta.normalized() * min_distance + elif (delta.length() > max_distance): + delta = delta.normalized() * max_distance + + #check upper and lower height + if ( delta.y > max_height): + delta.y = max_height + if ( delta.y < min_height): + delta.y = min_height + + #check autoturn + + var ds = PhysicsServer.space_get_direct_state( get_world().get_space() ) + + + var col_left = ds.intersect_ray(target,target+Matrix3(up,deg2rad(autoturn_ray_aperture)).xform(delta),collision_exception) + var col = ds.intersect_ray(target,target,collision_exception) + var col_right = ds.intersect_ray(target,target+Matrix3(up,deg2rad(-autoturn_ray_aperture)).xform(delta),collision_exception) + + if (col!=null): + #if main ray was occluded, get camera closer, this is the worst case scenario + delta = col.position - target + elif (col_left!=null and col_right==null): + #if only left ray is occluded, turn the camera around to the right + delta = Matrix3(up,deg2rad(-dt*autoturn_speed)).xform(delta) + elif (col_left==null and col_right!=null): + #if only right ray is occluded, turn the camera around to the left + delta = Matrix3(up,deg2rad(dt*autoturn_speed)).xform(delta) + else: + #do nothing otherwise, left and right are occluded but center is not, so do not autoturn + pass + + #apply lookat + pos = target + delta + + look_at_from_pos(pos,target,up) + + #turn a little up or down + var t = get_transform() + t.basis = Matrix3(t.basis[0],deg2rad(angle_v_adjust)) * t.basis + set_transform(t) + + + +func _ready(): + +#find collision exceptions for ray + var node = self + while(node): + if (node extends RigidBody): + collision_exception.append(node.get_rid()) + break + else: + node=node.get_parent() + # Initalization here + set_fixed_process(true) + #this detaches the camera transform from the parent spatial node + set_as_toplevel(true) + + + + + + diff --git a/demos/3d/kinematic_char/kinebody3d.png b/demos/3d/kinematic_char/kinebody3d.png Binary files differnew file mode 100644 index 0000000000..41f0edb246 --- /dev/null +++ b/demos/3d/kinematic_char/kinebody3d.png diff --git a/demos/3d/kinematic_char/level.scn b/demos/3d/kinematic_char/level.scn Binary files differnew file mode 100644 index 0000000000..785db19adc --- /dev/null +++ b/demos/3d/kinematic_char/level.scn diff --git a/demos/3d/kinematic_char/purple_wood.tex b/demos/3d/kinematic_char/purple_wood.tex Binary files differnew file mode 100644 index 0000000000..cdf0f810f1 --- /dev/null +++ b/demos/3d/kinematic_char/purple_wood.tex diff --git a/demos/3d/kinematic_char/purplecube.scn b/demos/3d/kinematic_char/purplecube.scn Binary files differnew file mode 100644 index 0000000000..ab758366fd --- /dev/null +++ b/demos/3d/kinematic_char/purplecube.scn diff --git a/demos/3d/kinematic_char/twood.tex b/demos/3d/kinematic_char/twood.tex Binary files differnew file mode 100644 index 0000000000..65c1bd043c --- /dev/null +++ b/demos/3d/kinematic_char/twood.tex diff --git a/demos/3d/kinematic_char/white_wood.tex b/demos/3d/kinematic_char/white_wood.tex Binary files differnew file mode 100644 index 0000000000..e003442e70 --- /dev/null +++ b/demos/3d/kinematic_char/white_wood.tex |