Input handling

The user can interact with the engine in three ways:

  • Keyboard
  • Mouse
  • Joystick

We can querry these information by using the input class of luxinia.

A very simple helper function is the Keyboard.setKeyBinding

 Keyboard.setKeyBinding("x",function () print "P pressed" end)

which will bind a certain key calling a given function.

Step 1

We are using now the previously created scene to react on userinput:

view = UtilFunctions.simplerenderqueue()
view.rClear:colorvalue(0.0,0.0,0.0,0) 

actor = actornode.new("actor",0,0,0)
actor.l3d = l3dprimitive.newsphere("box",1,1,1)
actor.l3d:linkinterface(actor)
actor.l3d:rfLitSun(true)

mysun = actornode.new("sun",100,200,600)
mysun.light = l3dlight.new("light")
mysun.light:linkinterface(mysun)
mysun.light:makesun()

cam = actornode.new("camera",3,5,2)
l3dcamera.default():linkinterface(cam)
cam:lookat(0,0,0,0,0,1)

We need now a function that is called every frame and which reads out the required keys and reacts on this:

function think ()
  local left = Keyboard.isKeyDown("LEFT")
  local right = Keyboard.isKeyDown("RIGHT")
  local up = Keyboard.isKeyDown("UP")
  local down = Keyboard.isKeyDown("DOWN")
  local fx = (left and 1 or 0) - (right and 1 or 0)
  local fy = (up and 1 or 0) - (down and 1 or 0)
  -- fx,fy: movement directions
  local x,y,z = actor:pos()
  actor:pos(x+fx*.1,y+fy*.1,z)
end

Timer.set("think",think,20)

Starting the project, we can move now the sphere around using the arrow keys.

Using a physics model, we could use a dbody to move the simulate the object's movement by using the ODE physics simulation.