PhysicsLab Test9 (edges and chains) also broken on iPad

@Simeon: On my iPad Pro, Test9 puts the little cart on a straight line, and it doesn’t move at all.

It’s supposed to be on a gentle slope, so that it rolls into the edge line. This should probably be fixed, I’d guess.

I tried rewriting the code myself, to make it work on both iPad Pro and iPhone, but I barely knew what I was doing, so I had to fiddle with it this way and that to get anywhere.

The challenges were:

  • make all the sizes proportional to the screen size, which was essential to making waves appear at all in the ground
  • make there not be too few or too many slopes, and not make them too steep or too shallow
  • make the little cart the right size so that it doesn’t fall over as it rolls on the slopes
  • make the cart appear high enough on screen that it starts rolling when it hits the ground, but not so high that it bounces and falls over
  • make the cart roll the right direction to hit the edge (I gave up on this and just added edges to both sides, so it could hit one no matter what direction it rolled)
  • make the cart not hit the edges too hard and fall over (which is why I ended up making the edges sloped)

And, as you’ll see from my code, I had to pull apart this one line of @John’s:


for i = 0, WIDTH, WIDTH/30 do
     table.insert(points, vec2(i, math.sin(i *4)*20+60))
end

…and explicate all the numbers into named variables, which I’m not even sure I got right, because basically I don’t understand sine and cosine and tangent and all that stuff.

Ideally, there would be just one hump in the middle of the screen, with the top of the hump slightly to the left of center, so that the cart would always roll to the right, but it was beyond me how to make that happen consistently.

At any rate, the following code will approximate what the original demo was supposed to do, if anyone for whom Test9 is broken wants to paste it into their PhysicsLab and try it out (it replaces the existing setup() code in Test9):


    local points = {}
    local overallY = WIDTH * 0.08
    local peaksMultiple = WIDTH * 0.000009
    local waveHeight = HEIGHT * 0.025
    local segmentLength = WIDTH/150
    for i = 0, WIDTH, segmentLength do
        table.insert(points, vec2(i,(math.sin(i*peaksMultiple) * waveHeight) + overallY))
    end
    
    local ground = physics.body(CHAIN, false, table.unpack(points))
    debugDraw:addBody(ground)
    
    local edge1 = physics.body(EDGE, vec2(WIDTH*0.75,0), vec2(WIDTH*0.72, overallY * 3))
    local edge2 = physics.body(EDGE, vec2(WIDTH*0.25,0), vec2(WIDTH*0.28, overallY * 3))
    debugDraw:addBody(edge1)
    debugDraw:addBody(edge2)
    
    local cartWidth = overallY * 1.2
    local cartHeight = cartWidth * 0.65
    local wheelRadius = cartHeight * 0.3
    local wheelOffsetX = cartWidth * 0.3
    local wheelOffsetY = cartHeight * 0.5
    local chase = createBox(WIDTH*0.45, overallY * 1.95, cartWidth, cartHeight)
    local leftWheel = createCircle(chase.x - wheelOffsetX, chase.y - wheelOffsetY, wheelRadius)
    local rightWheel = createCircle(chase.x + wheelOffsetX, chase.y - wheelOffsetY, wheelRadius)
    
    self.leftJ = physics.joint(REVOLUTE, chase, leftWheel, leftWheel.position)
    self.rightJ = physics.joint(REVOLUTE, chase, rightWheel, rightWheel.position)

…if anyone can correct the explication, and/or make the hump-in-the-center thing work correctly, I’d greatly appreciate the guidance.