I am using box2d for physics in my game and I came across a problem. In the game , I want to generate a wall in a random position on the screen. Then, you will have to draw a line as close to that wall as possible. The problem is, if I randomly generated coordinates for the wall, it wouldn't be flat. I want to generate a flat wall in a random position on the screen. Is there any way to do this?
Comments
@H4ch1 gave us a "doodle jump clone" and although there are not a random walls if there are random floors.
You can take some ideas of this script
http://giderosmobile.com/forum/discussion/473/let039s-jump-d#Item_10
I hope this script can help you
Likes: Zizanyman
[-] Liasoft
You only need to randomly generate 1 center coordinate, and then dimensions (width and height) and rotation, so that way you can always have a flat random wall
Likes: Zizanyman
self:wall(realx,realy) -- realx and realy are the random coordinates
And my code when I am creating the wall object:
wall:beginPath()
wall:moveTo(-screenW/2,-screenH/2)
wall:lineTo(screenW/2, -screenH/2)
wall:closePath()
wall:endPath()
wall:setPosition(x,y)
function scene:wall(x, y, width, height)
local wall = Shape.new()
wall:beginPath()
wall:moveTo(-screenW/2,-screenH/2)
wall:lineTo(screenW/2, -screenH/2)
wall:closePath()
wall:endPath()
wall:setPosition(x,y)
local body = self.world:createBody{type = b2.STATIC_BODY}
body:setPosition(wall:getX(), wall:getY())
body:setAngle(wall:getRotation() * math.pi/180)
local poly = b2.PolygonShape.new()
poly:setAsBox(wall:getWidth()/2, wall:getHeight()/2)
local fixture = body:createFixture{shape = poly, density = 1.0,
friction = 10, restitution = 0.2}
wall.body = body
wall.body.type = "wall"
self.isVisible = false
self:addChild(wall)
return wall
end
And then as I said earlier I used self:wall(realx, realy) in the init function to set the random coordinates (realx and realy are random values).
My complete code so far is attached if you want to take a look( I know your really busy, though)
Placing them inside function scene:init() will help achieving new position on each scene change