Solved!! Pretty proud of this one!
[lua]-- Set up some basics about the screen width height etc…
screenW = display.contentWidth
screenH = display.contentHeight
centerX = display.contentWidth/2
centerY = display.contentHeight/2
– Get the physics going…
local physics = require “physics”
physics.start()
physics.setGravity( 0, 4.3 )
physics.setDrawMode( “hybrid” ) – overlays collision outlines on normal Corona objects
physics.setPositionIterations( 16 )
physics.setVelocityIterations( 6 )
– Create a player…well, he’s a box with some
– physics properties. You could easily change
– him to a circle or what-not
– player = display.newRect(0,0,20,20)
player = display.newCircle(40,0,20)
local crateMaterial = { density = 1.0, friction = 0.3, bounce = 0.2,radius = 20 }
physics.addBody( player, crateMaterial )
player.y = centerY
–Generate some terrain
startX = 0 – our leftmost starting point of our first segment
startY = screenH - 200 – the y location of our starting segment
– Create a table to stuff all our segments into
myGroup = display.newGroup()
– Number of segments horizontally
local numSegments = 50
– The height variance. The larger the number
– the more dramatic and jagged the segments
– will become. 10 is pretty smooth, and go
– up from there for exciting bumps!
local yHeightVariance = 10
– Since we have a scroling set of terrain
– let’s put up a left wall and a right wall
– so even if the player rolls backwards or
– forwards, he’ll be scooped continuously along the terrain.
local wallMaterial = { density = 1.0, friction = 0.3, bounce = 0.2}
leftWall = display.newRect(0,0,10,screenH)
rightWall = display.newRect(screenW-10,0,10,screenH)
– Note the static, otherwise it would be dynamic and
– our walls would just fall away into oblivion
physics.addBody( leftWall, “static”, wallMaterial )
physics.addBody( rightWall, “static”, wallMaterial )
for i=1,numSegments do
– Vary this yHeightVariance to increase the slope (or drop!) of the terrain as it’s created
local newX = startX + math.random(25,100)
local newY = startY + math.random(-yHeightVariance,yHeightVariance)
local tempLine = display.newLine( startX,startY,newX,newY)
tempLine:setColor(255,100,0)
tempLine.width = 2
print("X: "…startX)
print("Y: "…startY)
– Note the “yHeightVariance+1”, this is needed to keep the shape convex and not concave
physics.addBody(tempLine,“static”,{ density=10.0, friction=0.5, bounce=0.2,shape = {0,0,newX-startX,newY-startY,newX-startX,(newY-startY)+yHeightVariance+1,0,(newY-startY)+yHeightVariance+1}})
startX = newX
startY = newY
myGroup:insert(tempLine)
end
– OK we’ve made a bunch of segments for our
– terrain, now let’s scroll the parent group
– that contains them all slowly to the left
– MAIN LOOP
local function main( event )
– I tried just moving the actual group, but
– that doesn’t work. The physics objects
– themselves stay put…so I had to resort
– to moving each individual segment
– manually…
for i=1,myGroup.numChildren do
local tempObject = myGroup[i]
tempObject.x = tempObject.x - 0.5
end
local chance = math.random(1,50)
if(chance==1)then
– Just for fun, let’s set the player
– objects LinearImpulse to some random speeds
local tempVelocity = math.random(1,3)
player:applyLinearImpulse( tempVelocity/100, 0, player.x, player.y )
end
end
Runtime:addEventListener( “enterFrame”, main )[/lua] [import]uid: 11636 topic_id: 8739 reply_id: 308739[/import]