How do I make screen shake, move etc?

I was trying using render.set_projection for that, but it is throwing errors such as “bad argument #2 to ‘get_width’ (RenderScriptInstance expected, got userdata)” or “bad argument #3 to ‘set_projection’ (RenderScriptInstance expected, got userdata)” even if I use examples from the manual or fragments from older topics.

Or should I use camera instead for that now?

You can only use render api in your render script. You can send a message to your render script to activate a screen shake.

1 Like

Animating/moving a camera object is the easiest way to produce a screen shake or any kind of movement needed to pan the scene.

This is a gist containing the camera shake script used in Hammerwatch Coliseum:

Attach the script to a go containing the camera and send a message to that script with four parameters:
x = amount of shake in x-direction
y = amount of shake in y-direction
time = duration of shake in seconds
speed = shakes per second - lower number means faster shaking

To try it out you can simply hot reload the script and it will initiate a shake immediately.

12 Likes

Thank you for sharing Johan! I created an example using your screen shake code:

And HTML example: http://britzl.github.io/publicexamples/screen_shake/index.html

5 Likes

I use something a littel bit less complex and it works for my needs.

go.property("shake", 0)

function init(self)
	msg.post("#camera", "acquire_camera_focus")
	self.pos = go.get_world_position()
	self.look_at = self.pos
end


function update(self, dt)
	if self.shake > 0 then
		go.set_position(self.pos + vmath.vector3(math.random() * self.shake, math.random() * self.shake, 0))
		self.shake = self.shake * 0.9 - 0.1 
	end
end

function on_message(self, message_id, message, sender)
	if message_id == hash("shake") then
		self.shake = 8
	end
end
6 Likes

Thank you for sharing @baturinsky. I updated my example to include your script as well. Space to toggle camera shake algorithm between Johan and Baturinsky.

5 Likes