Movement around planet works
This commit is contained in:
parent
987955704b
commit
926684ca7a
7
Planets.gd
Normal file
7
Planets.gd
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
extends Spatial
|
||||||
|
class_name SolarSystem
|
||||||
|
|
||||||
|
|
||||||
|
func get_gravitation_acceleration(position: Vector3) -> Vector3:
|
||||||
|
# TODO: Take all planets and their mass into account
|
||||||
|
return ($Earth.transform.origin - position).normalized()
|
59
Player.gd
Normal file
59
Player.gd
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
extends KinematicBody
|
||||||
|
|
||||||
|
|
||||||
|
var acceleration := Vector3(0.0, -9.81, 0.0)
|
||||||
|
var velocity := Vector3(0.0, 0.0, 0.0)
|
||||||
|
|
||||||
|
var move_speed = 5.0
|
||||||
|
var jump_accel = 300.0
|
||||||
|
|
||||||
|
export(NodePath) var solar_system
|
||||||
|
|
||||||
|
|
||||||
|
# Called when the node enters the scene tree for the first time.
|
||||||
|
func _ready():
|
||||||
|
pass # Replace with function body.
|
||||||
|
|
||||||
|
|
||||||
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
|
func _physics_process(delta):
|
||||||
|
var move_velocity := Vector3.ZERO
|
||||||
|
var move_acceleration := Vector3.ZERO
|
||||||
|
|
||||||
|
# Movement and rotation
|
||||||
|
if Input.is_action_pressed("ui_up"):
|
||||||
|
move_velocity.z -= move_speed
|
||||||
|
if Input.is_action_pressed("ui_down"):
|
||||||
|
move_velocity.z += move_speed
|
||||||
|
if Input.is_action_pressed("ui_left"):
|
||||||
|
rotate(transform.basis.y, delta)
|
||||||
|
if Input.is_action_pressed("ui_right"):
|
||||||
|
rotate(transform.basis.y, -delta)
|
||||||
|
|
||||||
|
# Make movement local
|
||||||
|
move_velocity = transform.basis * move_velocity
|
||||||
|
|
||||||
|
# Jumping
|
||||||
|
if Input.is_action_just_pressed("ui_select"):
|
||||||
|
move_acceleration.y += jump_accel
|
||||||
|
|
||||||
|
# Gravity
|
||||||
|
var gravity_acceleration = get_node(solar_system).get_gravitation_acceleration(transform.origin)
|
||||||
|
velocity += (gravity_acceleration * 9.81 - gravity_acceleration * move_acceleration.y) * delta
|
||||||
|
var movement = (move_velocity + velocity) * delta
|
||||||
|
|
||||||
|
# Apply movement to position
|
||||||
|
var collision = move_and_collide(movement)
|
||||||
|
|
||||||
|
# Reset the velocity if the body is colliding
|
||||||
|
if collision:
|
||||||
|
velocity = Vector3.ZERO
|
||||||
|
|
||||||
|
# Rotate down vector to face center of gravity
|
||||||
|
var down = gravity_acceleration
|
||||||
|
var local_down = transform.basis * Vector3.DOWN
|
||||||
|
var angle = local_down.angle_to(down)
|
||||||
|
var axis = local_down.cross(down).normalized()
|
||||||
|
|
||||||
|
if axis != Vector3.ZERO: # Happens if we're perfectly aligned already (local_down and down are equal)
|
||||||
|
rotate(axis, angle)
|
50
World.tscn
Normal file
50
World.tscn
Normal file
File diff suppressed because one or more lines are too long
@ -8,15 +8,20 @@
|
|||||||
|
|
||||||
config_version=4
|
config_version=4
|
||||||
|
|
||||||
_global_script_classes=[ ]
|
_global_script_classes=[ {
|
||||||
|
"base": "Spatial",
|
||||||
|
"class": "SolarSystem",
|
||||||
|
"language": "GDScript",
|
||||||
|
"path": "res://Planets.gd"
|
||||||
|
} ]
|
||||||
_global_script_class_icons={
|
_global_script_class_icons={
|
||||||
|
"SolarSystem": ""
|
||||||
}
|
}
|
||||||
|
|
||||||
[application]
|
[application]
|
||||||
|
|
||||||
config/name="Physics Project"
|
config/name="Physics Project"
|
||||||
run/main_scene="res://InputConverter.tscn"
|
run/main_scene="res://World.tscn"
|
||||||
config/icon="res://icon.png"
|
config/icon="res://icon.png"
|
||||||
|
|
||||||
[input]
|
[input]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user