I guess I’ll share my code based off this example too. I’ve got most of the basic physics down and a lot of comments to explain what I’ve learned.
-- Make sure to enable Craft in the dependency settings
function setup()
-- for fps function
frame = 0
etime = 0
parameter.watch("fps")
-- positions in space
cubexyz = vec3(0,5,0)
platformxyz = vec3(0,-5,0)
-- init two entities, cube and platform
cube = craft.entity()
platform = craft.entity()
-- Create a basic unit cube mesh
local cubeMesh = craft.mesh.cube(vec3(1,1,1))
local platformMesh = craft.mesh.cube(vec3(8,1,8))
-- Add the renderer component, which will actually draw our mesh
renderer = cube:add(craft.renderer, cubeMesh)
renderer2 = platform:add(craft.renderer, platformMesh)
-- move cubes to certain positions and rotate the cube a bit
cube.y = cubexyz.y
platform.y = platformxyz.y
local r = 45
cube.rotation = quat.eulerAngles(r,r,r)
-- add physics collision bodies
cubep = cube:add(craft.rigidbody, DYNAMIC, 0.5)
platform:add(craft.rigidbody, STATIC)
-- give a shape to the collision area
-- for box, first argument is shape, second is dimentions of the bounding box, and third is the offset from your mesh
cube:add(craft.shape.box, vec3(1,1,1), vec3(0,0,0))
platform:add(craft.shape.box, vec3(8,1,8), vec3(0,0,0))
-- Apply a material to the cube
renderer.material = craft.material("Materials:Standard")
renderer2.material = craft.material("Materials:Standard")
-- Use renderer.material.map to apply a texture
renderer.material.map = "Blocks:Greystone"
renderer2.material.map = "Blocks:Wood Red"
-- bounciness doesn't seem to work yet
cubep.restitution = 50
-- Move the camera forwards and rotate 180 degrees
craft.scene.camera.position = vec3(0,0,15)
craft.scene.camera.rotation = quat.eulerAngles(0,0,180)
end
function update()
-- I think this is called every time the physics engine updates
fpsEval()
end
function fpsEval()
-- just counting fps
frame = frame + 1
etime = etime + DeltaTime
fps = math.floor(frame/etime+.5)
end
function draw()
-- Nothing happening here D:
end
function touched(touch)
if touch.state == BEGAN then
-- wake up cube every time a touch registers
cubep.awake = true
if touch.x > WIDTH/3*2 then
-- apply force to cube (up and right)
cubep:applyForce(vec3(75,75,0))
elseif touch.x < WIDTH/3 then
-- apply force to cube (up and left)
cubep:applyForce(vec3(-75,75,0))
else
-- apply force to cube (up)
cubep:applyForce(vec3(0,75,0))
end
end
end