I want the box to move in accordance to the height of the terrain. This is my code.
-- Use this function to perform your initial setup
displayMode(FULLSCREEN)
function setup()
stick = Stick()
pylon = Wall("Cargo Bot:Crate Red 2")
wall = Wall("Cargo Bot:Crate Yellow 2")
hero = Hero(1,0,0)
TO_DEG = 180/math.pi
sin,cos,rad=math.sin,math.cos,math.rad
--UserDefined vars
TerrainRes=64 --Resolution of terrain
TerrainSize=9--Size in x and z
TerrainHeight=4
--Movement
Angle=vec2(0,0)
parameter.number("X",-1,2,1)
parameter.number("Y",-1,2,1)
parameter.number("Z",-1,2,0)
--Variables
Ires=TerrainSize/TerrainRes
EpicTerrain=image(TerrainRes/2,TerrainRes/2)
setContext(EpicTerrain)
sprite("Documents:height amp") --IMPORTANT! Your heightmap here!!
setContext()
--Meshing
local X,Index,px,py,y1,y2,y3,y4
m=mesh()
verts={}
for x=1,TerrainRes-1 do
X=(x-1)*(TerrainRes-1)*6+1
px=(x-1)/TerrainRes px=px*TerrainSize
for y=1,TerrainRes-1 do
Index=X+(y-1)*6
py=(y-1)/TerrainRes py=py*TerrainSize
--All the heights
y1=color(EpicTerrain:rawGet(x,y)).x/255*TerrainHeight
y2=color(EpicTerrain:rawGet(x+1,y)).x/255*TerrainHeight
y3=color(EpicTerrain:rawGet(x,y+1)).x/255*TerrainHeight
y4=color(EpicTerrain:rawGet(x+1,y+1)).x/255*TerrainHeight
--Constructing the vertices
verts[Index]=vec3(px,y1,py)
verts[Index+1]=vec3(px+Ires,y2,py)
verts[Index+2]=vec3(px+Ires,y4,py+Ires)
verts[Index+3]=vec3(px+Ires,y4,py+Ires)
verts[Index+4]=vec3(px,y3,py+Ires)
verts[Index+5]=vec3(px,y1,py)
end
end
m.vertices=verts
m.shader=shader(MSV,MSF)
m.shader.TerrainSize=TerrainSize*1.5
end
-- This function gets called once every frame
function draw()
background(0)
local TO_DEG = TO_DEG
local hero = hero
perspective(60)
camera(hero.x, 2, 1+hero.z, hero.x, 1, hero.z, 0, 1, 0)
m:draw()
-- Draw world
-- Draw hero
translate(hero.x, hero.y, hero.z)
rotate(stick.direction*TO_DEG, 0, 1, 0)
-- roll animation
if stick.active then
rotate(-ElapsedTime*10*TO_DEG, 0, 0, 1)
end
scale(.25, .25, .25)
hero:draw()
-- Restore orthographic projection
-- fade out overlay
if stick.active then
local ceil = math.ceil
-- move hero based on stick direction
local mvtx = math.cos(stick.direction)/50*stick.dist
local mvtz = -math.sin(stick.direction)/50*stick.dist
hero.x = hero.x + mvtx
hero.z = hero.z + mvtz
hero.y = 1
-- convert to table coordinates
hero.px = ceil(hero.x - .5)
hero.py = ceil(hero.z - .5)
-- lazy collision check
end
ortho()
resetMatrix()
viewMatrix(matrix())
sprite(EpicTerrain,0,0,300,300)
end
function touched(touch)
stick:touched(touch)
end
--# World
--# Hero
Hero = class()
function Hero:init(x,y,z)
self.x, self.y, self.z = x,y,z
self.px, self.py = math.ceil(.5+x), math.ceil(.5+z)
self.mdl = Wall("Cargo Bot:Crate Blue 2")
end
function Hero:draw()
self.mdl:draw()
end
--# Wall
Wall = class()
function Wall:init(tex)
-- all the unique vertices that make up a cube
local vertices =
{
vec3(-0.5, -0.5, 0.5), -- Left bottom front
vec3( 0.5, -0.5, 0.5), -- Right bottom front
vec3( 0.5, 0.5, 0.5), -- Right top front
vec3(-0.5, 0.5, 0.5), -- Left top front
vec3(-0.5, -0.5, -0.5), -- Left bottom back
vec3( 0.5, -0.5, -0.5), -- Right bottom back
vec3( 0.5, 0.5, -0.5), -- Right top back
vec3(-0.5, 0.5, -0.5), -- Left top back
}
-- now construct a cube out of the vertices above
local verts =
{
-- Front
vertices[1], vertices[2], vertices[3],
vertices[1], vertices[3], vertices[4],
-- Right
vertices[2], vertices[6], vertices[7],
vertices[2], vertices[7], vertices[3],
-- Back
vertices[6], vertices[5], vertices[8],
vertices[6], vertices[8], vertices[7],
-- Left
vertices[5], vertices[1], vertices[4],
vertices[5], vertices[4], vertices[8],
-- Top
vertices[4], vertices[3], vertices[7],
vertices[4], vertices[7], vertices[8],
-- Bottom
vertices[5], vertices[6], vertices[2],
vertices[5], vertices[2], vertices[1],
}
-- all the unique texture positions needed
local texvertices =
{
vec2(0,0),
vec2(1,0),
vec2(0,1),
vec2(1,1)
}
-- apply the texture coordinates to each triangle
local texCoords =
{
-- Front
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Right
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Back
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Left
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Top
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Bottom
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
}
self.model = mesh()
self.model.vertices = verts
self.model.texture = tex
self.model.texCoords = texCoords
self.model:setColors(255,255,255,255)
end
function Wall:draw()
self.model:draw()
end
--# Floor
--# Stick
Stick = class()
function Stick:init()
self.direction = 0
self.dist = 0
self.active = false
self.origin = vec2(150, 150)
self.center = self.origin
self.pos = self.origin
end
function Stick:touched(touch)
if touch.state == BEGAN then
self.center = vec2(touch.x, touch.y)
self.active = true
end
self.pos = vec2(touch.x, touch.y)
self.direction = math.atan2(self.pos.y - self.center.y, self.pos.x - self.center.x)
self.dist = math.min(2, self.pos:dist(self.center)/32)
if touch.state == ENDED then
self.center = self.origin
self.pos = self.center
self.active = false
end
end
MSV=[[
uniform mat4 modelViewProjection;
attribute vec4 position;
varying vec3 Pos;
void main() {
Pos=position.xyz;
gl_Position=modelViewProjection*position;
}
]]
MSF=[[
precision highp float;
varying vec3 Pos;
uniform vec3 Player;
uniform float TerrainSize;
void main() {
gl_FragColor=vec4(pow(vec3(length(Player-Pos)/TerrainSize),vec3(0.45)),1.);
}
]]