Chute physique

Cliquez pour lâcher un RigidBody3D sur le sol et le voir s'empiler. Ceux que vous créez se nettoient seuls au bout de dix secondes.

L'éditeur ci-dessus fait tourner ce projet. Changez une ligne et il se recharge.

Classes Godot utilisées

RigidBody3DStaticBody3DCollisionShape3D

Le code

scripts/main.gd
extends Node

const GROUND_SIZE: Vector3 = Vector3(14, 1, 14)
const BODY_LIFETIME: float = 10.0


func _ready() -> void:
	_add_environment()
	_add_light()
	_add_ground()

	var camera: Camera3D = Camera3D.new()
	camera.position = Vector3(0, 7, 12)
	add_child(camera)
	camera.look_at(Vector3(0, 1, 0), Vector3.UP)
	camera.current = true

	for i in 3:
		_drop_body(false)

	print('click anywhere to drop a body')


func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouseButton and event.pressed:
		_drop_body()


func _drop_body(temporary: bool = true) -> void:
	var visual: MeshInstance3D = MeshInstance3D.new()
	var collision: CollisionShape3D = CollisionShape3D.new()

	if randf() > 0.5:
		var sphere: SphereMesh = SphereMesh.new()
		sphere.radius = 0.5
		sphere.height = 1.0
		visual.mesh = sphere

		var sphere_shape: SphereShape3D = SphereShape3D.new()
		sphere_shape.radius = 0.5
		collision.shape = sphere_shape
	else:
		visual.mesh = BoxMesh.new()
		collision.shape = BoxShape3D.new()

	var material: StandardMaterial3D = StandardMaterial3D.new()
	material.albedo_color = Color.from_hsv(randf(), 0.5, 0.95)
	material.roughness = 0.4
	visual.material_override = material

	var body: RigidBody3D = RigidBody3D.new()
	body.position = Vector3(randf_range(-2.5, 2.5), 9, randf_range(-2.5, 2.5))
	body.add_child(visual)
	body.add_child(collision)
	add_child(body)

	if not temporary:
		return

	await get_tree().create_timer(BODY_LIFETIME).timeout

	if is_instance_valid(body):
		body.queue_free()


func _add_ground() -> void:
	var mesh: BoxMesh = BoxMesh.new()
	mesh.size = GROUND_SIZE

	var material: StandardMaterial3D = StandardMaterial3D.new()
	material.albedo_color = Color('#2a2f3d')

	var visual: MeshInstance3D = MeshInstance3D.new()
	visual.mesh = mesh
	visual.material_override = material

	var shape: BoxShape3D = BoxShape3D.new()
	shape.size = GROUND_SIZE

	var collision: CollisionShape3D = CollisionShape3D.new()
	collision.shape = shape

	var ground: StaticBody3D = StaticBody3D.new()
	ground.position = Vector3(0, -0.5, 0)
	ground.add_child(visual)
	ground.add_child(collision)
	add_child(ground)


func _add_environment() -> void:
	var sky_material: ProceduralSkyMaterial = ProceduralSkyMaterial.new()
	sky_material.sky_top_color = Color(0.05, 0.06, 0.12)
	sky_material.sky_horizon_color = Color(0.14, 0.16, 0.24)
	sky_material.ground_bottom_color = Color(0.02, 0.02, 0.04)
	sky_material.ground_horizon_color = Color(0.1, 0.11, 0.16)

	var sky: Sky = Sky.new()
	sky.sky_material = sky_material

	var env: Environment = Environment.new()
	env.background_mode = Environment.BG_SKY
	env.sky = sky
	env.ambient_light_source = Environment.AMBIENT_SOURCE_SKY
	env.ambient_light_energy = 0.7

	var world: WorldEnvironment = WorldEnvironment.new()
	world.environment = env
	add_child(world)


func _add_light() -> void:
	var light: DirectionalLight3D = DirectionalLight3D.new()
	light.rotation_degrees = Vector3(-55, -35, 0)
	light.light_energy = 1.2
	light.shadow_enabled = true
	add_child(light)

Plus d'exemples de Physique