Hey It worked so thank you but before I put all my code on this how do you move the mesh around like if you needed to move it 100 down?
Okay I found a way that will work by doing that addRect again and changing the size. If this is the wrong way to do it please tell me but for know I will use this.
Thanks again for answering all the questions I have had so far.
Look at the translate command under Graphics.
Okay will do but thank youuuuuuuu soooooooo much when I ran my code In with the FPS in there I was trying to draw so many sprites I had I down to 3FPS now it runs at 59FPS thank you so much.
Pac
Hey Dave I am having a problem. I need to get a camera in craft to follow an entity but my problem is that I don’t know how and that the reference doesn’t make any sense some please help me.
Instead of moving the camera and the entity, move everything else so it looks like the camera and entity are moving. I don’t know what you’re doing, so I can’t give you too much help.
I am trying to make a space video game but the problem is that when you move the ship the ship flys off the screen
Here’s something I wrote a while ago. Run in landscape position. This might be like what you’re after. Slide your finger up or down the screen to increase or decrease your speed. Tilt the iPad left or right to turn. Fly thru the green balls but avoid the red balls.
displayMode(FULLSCREEN)
function setup()
assert(craft, "Please include Craft as a dependency")
parameter.number("maxSpeed",.1,1.2,.5)
parameter.watch("Gravity.x")
parameter.watch("fps")
setup1()
end
function setup1()
mx,mz=0,0
gameOver=false
hgx=Gravity.x
st=os.time()
speed,ey,ang=0,45,0
cameraX,cameraZ=-205,-205
count=200
yellow=0
scene = craft.scene()
scene.camera.position = vec3(cameraX,0,cameraZ)
scene.camera.eulerAngles=vec3(ex,ey,ez)
scene.sun.rotation = quat.eulerAngles(45,0,45)
scene.ambientColor = color(90,90,90)
skyMaterial = scene.sky.material
skyMaterial.sky = color(252, 255, 0, 255)
skyMaterial.horizon = color(0, 203, 255, 255)
tab={}
for z=1,count do -- 1=red
createSphere(math.random(-200,200),math.random(-200,200),2,1,255,0,0,0,0)
end
for z=1,count do -- 2=green
createSphere(math.random(-200,200),math.random(-200,200),.25,2,0,255,0,0,0)
end
createFloor()
end
function update(dt)
scene:update(dt)
scene.camera.position = vec3(cameraX,1,cameraZ)
scene.camera.eulerAngles=vec3(0,ey,0)
end
function draw()
fps=1/DeltaTime//1
background(0)
if gameOver then
sprite("Tyrian Remastered:Explosion Huge",WIDTH/2,HEIGHT/2,500,500)
fill(255,0,0)
text("Hold the ipad level then",WIDTH/2,HEIGHT/2+50)
text("Double tap the screen to restart",WIDTH/2,HEIGHT/2)
text(200-count.." hits in "..en-st.." seconds",WIDTH/2,HEIGHT/2-50)
val=string.format("%5.2f",(200-count)/(en-st))
text(val.." hits per second",WIDTH/2,HEIGHT/2-100)
return
end
en=os.time()
update(DeltaTime)
scene:draw()
if speed>maxSpeed then
speed=maxSpeed
elseif speed<=0 then
speed=0
end
ey=ey-ang
x=speed*math.sin(math.rad(ey))
z=speed*math.cos(math.rad(ey))
cameraX=cameraX+x
cameraZ=cameraZ+z
for a,b in pairs(tab) do
if b.type==1 then
if cameraX>=b.ent.position.x-b.size and cameraX<=b.ent.position.x+b.size and
cameraZ>=b.ent.position.z-b.size and cameraZ<=b.ent.position.z+b.size then
gameOver=true
sound(SOUND_EXPLODE, 27037)
end
end
if b.type==2 then
if cameraX>b.ent.position.x-b.size and cameraX<b.ent.position.x+b.size and
cameraZ>b.ent.position.z-b.size and cameraZ<b.ent.position.z+b.size then
b.ent:destroy()
table.remove(tab,a)
count=count-1
sound(SOUND_HIT, 19423)
createSphere(math.random(-200,200),math.random(-200,200),.5,3,255,255,0,.2,.2)
yellow=yellow+1
end
end
if b.type==3 then
if cameraX>=b.ent.position.x-b.size and cameraX<=b.ent.position.x+b.size and
cameraZ>=b.ent.position.z-b.size and cameraZ<=b.ent.position.z+b.size then
b.ent:destroy()
table.remove(tab,a)
yellow=yellow-1
sound(SOUND_POWERUP, 19422)
else
xx=b.ent.position.x
zz=b.ent.position.z
xx=xx+b.xv
zz=zz+b.zv
if xx<-200 or xx>200 then
b.xv=-b.xv
end
if zz<-200 or zz>200 then
b.zv=-b.zv
end
b.ent.position=vec3(xx,1,zz)
end
end
end
gx=Gravity.x
ang=ang+(gx-hgx)*4
hgx=gx
pushMatrix()
translate(WIDTH/2,HEIGHT/2-100)
rotate(ang*-30)
sprite("Tyrian Remastered:Boss A",0,0,300)
fill(255)
text(count.." ".." "..yellow.." "..string.format("%2.1f",speed),0,0)
translate()
popMatrix()
if speed==0 then
text("Slide your finger up/down to + or - the speed",WIDTH/2,HEIGHT-50)
end
end
function touched(t)
if t.state==BEGAN then
if t.tapCount==2 and gameOver then
setup1()
return
else
ang=0
end
elseif t.state==MOVING then
speed=speed+t.deltaY/80
end
end
function createFloor(x,z)
c1=scene:entity()
w=c1:add(craft.rigidbody,STATIC)
c1.model = craft.model.cube(vec3(400,1,400))
c1.position=vec3(x,-.5,z)
c1.material = craft.material("Materials:Standard")
c1.material.map = readImage("Blocks:Cactus Top")
c1.material.offsetRepeat=vec4(0,0,50,50)
end
function createSphere(x,z,size,type,r,g,b,xv,zv)
sphere1=scene:entity()
s1=sphere1:add(craft.rigidbody,KINEMATIC)
sphere1.position=vec3(x,1,z)
sphere1:add(craft.shape.sphere,size)
sphere1.model = craft.model.icosphere(size,1)
sphere1.material = craft.material("Materials:Specular")
sphere1.material.diffuse=color(r,g,b)
table.insert(tab,{ent=sphere1,xv=xv,zv=zv,size=size,type=type})
end
@dave1707 - yeah, great demo you posted before. Just made a few changes - not polished and needs a few obvious mods but changed the createSphere() mod to add a little variety:
function createSphere(x,z,size,type,r,g,b,xv,zv)
sphere1=scene:entity()
s1=sphere1:add(craft.rigidbody,KINEMATIC)
sphere1.position=vec3(x,0,z)
mat = tostring(math.floor(math.random(20)+40))
sphere1.model = craft.model("Nature:naturePack_0"..mat)
table.insert(tab,{ent=sphere1,xv=xv,zv=zv,size=size,type=type})
end
Anyone else want to tidy/add anything?
@Bri_G That makes it different and hard to know what to hit or avoid. It takes a really long time to load on my iPad Air. When I first posted this it was a starter game for anyone to add whatever they wanted to it. I still play with it every now and then. I got the green balls down in the teens, but the yellow balls are hard to get. I was thinking of adding more tries so the game didn’t end after one bad collision but then I like just flying around hitting the green balls and chasing the yellow balls. Here’s the original link from about a year ago.
https://codea.io/talk/discussion/8932/starter-game-22#latest
Yes that is something like what I need but I am can’t see where you move the camera / object around. Please point it out.
@pac Look thru the code for all occurances where I use camera. The camera gets moved in the draw function just before the large for loop where I modify cameraX and cameraZ. In the setup function I define the camera using the variables cameraX and cameraZ.
@dave1707 - yes, does take a long time to initialise. I just like flying around objects trying to avoid all of them. Will play around with it, but later. See if I can improve the startup and avoid having to restart. Could make a few games out of this. Thanks for reposting it.
@pac I stripped out all of the code except for the code moving the camera and ship. See if that helps any.
displayMode(FULLSCREEN)
function setup()
assert(craft, "Please include Craft as a dependency")
hgx=Gravity.x
speed,ey,ang=.1,45,0
cameraX,cameraZ=-205,-205
scene = craft.scene()
scene.camera.position = vec3(cameraX,0,cameraZ)
skyMaterial = scene.sky.material
skyMaterial.sky = color(252, 255, 0, 255)
skyMaterial.horizon = color(0, 203, 255, 255)
createFloor()
end
function update(dt)
scene:update(dt)
scene.camera.position = vec3(cameraX,1,cameraZ)
scene.camera.eulerAngles=vec3(0,ey,0)
end
function draw()
background(0)
update(DeltaTime)
scene:draw()
ey=ey-ang
x=speed*math.sin(math.rad(ey))
z=speed*math.cos(math.rad(ey))
cameraX=cameraX+x
cameraZ=cameraZ+z
gx=Gravity.x
ang=ang+(gx-hgx)*4
hgx=gx
pushMatrix()
translate(WIDTH/2,HEIGHT/2-100)
rotate(ang*-30)
sprite("Tyrian Remastered:Boss A",0,0,300)
translate()
popMatrix()
end
function createFloor(x,z)
c1=scene:entity()
w=c1:add(craft.rigidbody,STATIC)
c1.model = craft.model.cube(vec3(400,1,400))
c1.position=vec3(x,-.5,z)
c1.material = craft.material("Materials:Standard")
c1.material.map = readImage("Blocks:Cactus Top")
c1.material.offsetRepeat=vec4(0,0,50,50)
end
@Bri_G It is kind of fun just flying around. The part I like best is chasing the yellow balls. Maybe I’ll remove the red balls so I don’t have to run into them.
Okay thanks that really helped
Is there an app that lets me make 3D models on my iPad?
If you don’t know by the morning/don’t respond I am going to make new discussion tomorrow.
Thanks pac
So I am trying to make a game that uses the water craft stuff the problem is that I can find an water so could you please tell me how I could change a plane to have a blue color or tell me who to put a sprite on a plane both are good but the second on is better.
Thanks Pac
@pac Here’s another version where I had more time to play around with it. It takes about 5 seconds to load because of the models.
displayMode(FULLSCREEN)
function setup()
assert(craft, "Please include Craft as a dependency")
hgx=Gravity.x
speed,ey,ang=.1,45,0
cameraX,cameraZ=-200,-200
scene = craft.scene()
scene.camera.position = vec3(cameraX,0,cameraZ)
skyMaterial = scene.sky.material
skyMaterial.sky = color(252, 255, 0, 255)
skyMaterial.horizon = color(0, 156, 255, 255)
createShip(50)
createSea()
end
function update(dt)
scene:update(dt)
scene.camera.position = vec3(cameraX,1,cameraZ)
scene.camera.eulerAngles=vec3(0,ey,0)
end
function draw()
background(0)
update(DeltaTime)
scene:draw()
ey=ey-ang
x=speed*math.sin(math.rad(ey))
z=speed*math.cos(math.rad(ey))
cameraX=cameraX+x
cameraZ=cameraZ+z
gx=Gravity.x
ang=ang+(gx-hgx)*4
hgx=gx
end
function createShip(nbr)
for z=1,nbr do
sh=scene:entity()
sh.position=vec3(math.random(-200,200),0,math.random(-200,200))
mr=sh:add(craft.renderer)
mr.model=craft.model("Watercraft:watercraftPack_"..string.format("%03d",math.random(29)))
end
end
function createSea(nbr)
c1=scene:entity()
c1.model = craft.model.cube(vec3(400,1,400))
c1.position=vec3(0,-.15,0)
c1.material = craft.material("Materials:Standard")
c1.material.map = readImage("Surfaces:Desert Cliff Normal")
c1.material.offsetRepeat=vec4(0,0,20,20)
c1.material.diffuse=color(0, 183, 255, 255)
end
Thanks