Menu
CoddyTech

Drifting Rocks

Part of the Projects section of Coddy's Godot journey. Lesson 16 of 36.

The rocks need to move. rock.gd drifts each rock at its velocity, spins it slowly and wraps it like the ship.

A rock is big, so it wraps at -30 and 670 instead of 0 and 640. That way it leaves the screen fully before it comes back. The jump is 700, the width of that wider strip:

if position.x > 670:
	position.x -= 700

The left edge and the top and bottom work the same way, with -30, 390 and a jump of 420.

challenge icon

Challenge

Hard

Make the rocks drift, spin and wrap.

  1. In rock.gd, add _physics_process: move by velocity * delta and add 0.5 * delta to rotation.
  2. Past -30 or 670 on x, move it 700 the other way.
  3. Past -30 or 390 on y, move it 420 the other way.

Press Play: the rocks tumble across the screen and come back. Then Check.

Try it yourself

extends Area2D

var velocity = Vector2.ZERO

# Bursts into dust where the rock is. Call it when the rock breaks.
func burst():
	var dust = CPUParticles2D.new()
	dust.position = position
	dust.one_shot = true
	dust.explosiveness = 1.0
	dust.amount = 20
	dust.lifetime = 0.7
	dust.spread = 180.0
	dust.gravity = Vector2.ZERO
	dust.initial_velocity_min = 30.0
	dust.initial_velocity_max = 120.0 * scale.x
	dust.scale_amount_min = 2.0
	dust.scale_amount_max = 5.0
	dust.color = Color(0.75, 0.82, 0.95)
	dust.finished.connect(dust.queue_free)
	get_parent().add_child(dust)
	dust.emitting = true

All lessons in Projects

Practice on your own: Godot playground