Hi all,
I recently have been looking at tiny penguins code and have made a few adjustments here and there. I am going to try and use part of the code that generates the landscape as I want rounded hills in my game. I however am having a few issues. The first issue is that I cannot find what dictates where the hills are drawn on the Y-axis. Second I noticed that they in tiny penguins had the penguin moving, however I would prefer to have the land moving underneath. Does anyone know if this is possible? Also if anyone knows of a easier way to generate hills and still have them render collisions that would be great. Here is what I have so far from tiny penguins.
thanks,
Chris
[lua]require (“physics”)
physics.start()
local circle = display.newCircle(50, 250, 10)
physics.addBody(circle, “dynamic”, {density = 1.0, friction = 0, bounce = 0, radius = 10, isSensor = false})
local hillYPosition = display.contentHeight - display.contentHeight / 4 – The y position of the bottom of the hill
local sideBuffer = display.contentWidth
– The offset (where the bottom of a hill will be place)
– We want the offset to be a big negative so there are hills to the left of the screen
local hillOffSet = -sideBuffer
local hillHeight = 10
local hillAmpMinValue = .3
local hillAmpRange = .2
local isFingerDown = false
– We need to put everything into a group so we can move it such that the bird is always in the same x coordinate (birdFixedCoordX)
local mainGroup = display.newGroup()
function main()
display.setStatusBar( display.HiddenStatusBar )
initHills()
end
function initHills()
while(hillOffSet < display.contentWidth + sideBuffer) do
createHill()
end
end
– Creates one hill
– Code borrowed from: http://www.cocos2d-iphone.org/forum/topic/14136/page/3
function createHill()
local physicsBodyHeight = 30
local x1 = hillOffSet
local yStaringPosition = display.contentHeight - display.contentHeight / 4
local h_height = hillHeight + math.random(hillHeight)
– Hill amplitude scale
local h_amp = hillAmpMinValue + math.random() * hillAmpRange
local y1 = h_height
local objects = {}
local lineGroup = display.newGroup()
mainGroup:insert(lineGroup, 0)
local angleStep = 20
local angle
for angle = angleStep, 360, angleStep do
local points = {}
local x2 = hillOffSet + angle/h_amp
local y2 = h_height * math.cos(math.rad(angle))
local rect = display.newRect(0,0,1,1)
– rect.isVisible = false
– Going to make a rectangle for each line segment
table.insert(points, x2)
table.insert(points, hillYPosition - h_height + y2 + physicsBodyHeight)
table.insert(points, x1)
table.insert(points, hillYPosition - h_height + y1 +physicsBodyHeight)
table.insert(points, x1)
table.insert(points, hillYPosition - h_height + y1)
table.insert(points, x2)
table.insert(points, hillYPosition - h_height + y2)
– Matt - Capturing values
local startX = x2
local startY = hillYPosition - h_height + y1
local endX = x1
local endY = hillYPosition - h_height + y2
local fillAngle
local fillX1, fillY2
local fillX2, fillX2
fillX2 = x2
– Fils in the hill with a color
for fillAngle = angle - angleStep, angle do
local fillX = hillOffSet + fillAngle/h_amp
local fillY = h_height * math.cos(math.rad(fillAngle))
local line = display.newLine(fillX, hillYPosition - h_height + fillY, fillX - 100, display.contentHeight)
line:setColor(hsv(fillAngle % 360 == 0 and 1 or fillAngle,.4,.8))
line.width = 3
mainGroup:insert(line)
table.insert(objects, line)
end
physics.addBody(rect, “static”, {w=0, bounce=0.0, shape=points})
mainGroup:insert(rect)
table.insert(objects, rect)
rect.xPos = x2
rect.name = “col_”…x1
– Collision Handler
rect.collision = function( self , event )
– On Collision
if ( event.phase == “began” ) then
event.other.isHit( startX , endX , startY , endY, rect.name )
end
if ( event.phase == “ended” ) then
event.other.endHit(rect.name)
end
end
rect:addEventListener( “collision” , rect )
x1 = x2
y1 = y2
if(angle == 360) then
local onRectEnterFrame
onRectEnterFrame = function(event)
local rectContentCoordX, notNeeded = rect:localToContent(rect.xPos,0)
if(rectContentCoordX < 0) then
Runtime:removeEventListener(“enterFrame”, onRectEnterFrame)
for key,body in pairs(objects) do
body:removeSelf()
end
createHill()
end
end
Runtime:addEventListener(“enterFrame”, onRectEnterFrame)
end
end
hillOffSet = x1
end
– Helps us make pretty colors by converting hsv to rgb
– From http://pastebin.com/pqXgvyCs
function hsv(h,s,v,a)
local c, h_ = v*s, (h%360)/60
local x = c*(1-(h_%2-1))
local c_, m = ({{c,x,0},{x,c,0},{0,c,x},{0,x,c},{x,0,c},{c,0,x}})[math.ceil(h_)], v-c
return 8, 205, 50
end
main() [import]uid: 126017 topic_id: 23587 reply_id: 323587[/import]