How to make upward slopes / diagonal ground?

Hi,
I’m relatively new to Corona and lua and I have a question that may seem basic, but I have’nt seen something like this in the forums or other tutorials.

My first game is an adaptation of a old “always run” game I did in flash some time ago. I’m using this tutorial as a template to start me up: http://mobile.tutsplus.com/tutorials/corona/corona-sdk-create-a-side-scroller-from-scratch/

My problem is that in the original game I had floor that had both upwards and downwards slopes (meaning that it was not made of square shapes), and I can’t seem to find to do that, because all the material I have seen only uses square and rectagular floors.

I hope this is understandable, it’s sort of hard to explain. [import]uid: 136402 topic_id: 28736 reply_id: 328736[/import]

You’d use a custom physics body shape, see here; http://hlproject.com/2011/11/04/corona-sdk-13-custom-physics-bodies/

There’s also a section in the docs on it but this video is much more thorough.

Let me know how you get on.

Peach :slight_smile: [import]uid: 52491 topic_id: 28736 reply_id: 115890[/import]

Thanks! (sorry it took me so long to work on it).

The diagonal floor pieces have the correct shapes, and the downward slope is working as intended, but the upwards slope is not, as the player simply falls through that particular floor piece, and I’m a little clueless as to why does that happen.

Remember that in the case of my game, the player doesn’t actually move, is the floor pieces that constantly move left to give the illusion of movement. [import]uid: 136402 topic_id: 28736 reply_id: 116143[/import]

Without seeing any code my guess would be to set your floor physics body (upwards slope) isBullet = true

Hopefully that will remedy the issue of your player falling through it.

Just throwing my two cents in!

Brian. [import]uid: 40731 topic_id: 28736 reply_id: 116147[/import]

I just played around a bit with physics in Corona, so am by no means an expert, but how are you moving the bodies around? I have seen it mentioned that physics engine does not respond favorably to moving physics bodies by transitions or by changing their coords. It is best to move them, like in real world, by applying forces. [import]uid: 160496 topic_id: 28736 reply_id: 116153[/import]

Okay, after a lot of tests, I found out that is not only the upward slope that fails, it seems that the hero will go through and fall any floor object when it collides from the side instead of the top. (instead of pushing back the hero or allowing him to go up the slope).

Here is how I set up the bodies:

--setup first 8 floor pieces  
for a = 1, 8, 1 do  
 isDone = false;  
 --get a random number  
 local mRand = math.random;  
 local myRand = mRand(1, 5);  
 local newBlock;  
 if(myRand == 1 and isDone == false) then  
 newBlock = sprite.newSprite(floorSets["A1"]);  
 physics.addBody( newBlock, "kinematic", { friction=0.3, bounce=0.4 } );  
 isDone = true;  
 end  
 if(myRand == 2 and isDone == false) then  
 newBlock = sprite.newSprite(floorSets["A2"]);  
 physics.addBody( newBlock, "kinematic", { friction=0.3, bounce=0.4 } );  
 isDone = true;  
 end  
 if(myRand == 3 and isDone == false) then  
 newBlock = sprite.newSprite(floorSets["A3"]);  
 physics.addBody( newBlock, "kinematic", { friction=0.3, bounce=0.4 } );  
 isDone = true;  
 end  
 if(myRand == 4 and isDone == false) then  
 newBlock = sprite.newSprite(floorSets["A4"]);  
 diagShape = { -40,120, 40,120, 40,-120, -40,-90 };  
 physics.addBody( newBlock, "kinematic", { friction=0.3, bounce=0.4, shape=diagShape } );  
 isDone = true;  
 end  
 if(myRand == 5 and isDone == false) then  
 newBlock = sprite.newSprite(floorSets["A5"]);  
 diagShape2 = { -40,120, 40,120, 40,-90, -40,-120 };  
 physics.addBody( newBlock, "kinematic", { friction=0.3, bounce=0.4, shape=diagShape2 } );  
 isDone = true;  
 end  
  
 newBlock.name = ("block" .. a);  
 newBlock.id = a;  
 newBlock.x = (a \* 80) - 80;  
 newBlock.y = 400;  
 blocks:insert(newBlock);  
end   
--setup hero's body  
heroShape = { -20,50, 20,50, 20,-50, -20,-50 };  
physics.addBody( hero, "dynamic",{ density = 0.3, friction = 0.6, shape=heroShape } );  

What I do next is simply move all the ordered floorpieces to the left, to give the illusion of movement. (in here I used setLinearVelocity, but I also tried using translate, to the same result).

--update block movement function updateBlocks() for a = 1, blocks.numChildren, 1 do (blocks[a]):setLinearVelocity(moveSpeed,0); end end [import]uid: 136402 topic_id: 28736 reply_id: 116275[/import]

Well, I have kept trying to solve this, I have reoganized code, changed groups, tweaked body types and physics settings, but can’t figure it out =/

Here’s the full code of the screen (I’m using director, btw) if anyone wants to take a look:
http://pastebin.com/ChFZKhP1 [import]uid: 136402 topic_id: 28736 reply_id: 116597[/import]

Glancing at that it seems you may have defined your bodies points in an anti clockwise direction; that could very well be causing an issue - could you change this and let me know if you see a different result, please?

Peach :slight_smile: [import]uid: 52491 topic_id: 28736 reply_id: 116684[/import]

Thanks again! Even though I knew about the clockwise thing, I still neglected to take a second look at it. I guess an extra pair of eyes is always useful.

Collisions now happen and both slopes “work”. There’s still plenty to be done, but I’m on the right track now.

Now the problems are:

  1. the “hero” won’t climb the upwards slopes on its own, what I did to counteract this is applying a force that is the direct opposite of the blocks if the hero gets knocked too far. While this keeps the hero running. it looks like it’s trembling. I’ll also try with less steep slopes to see if that helps things.

  2. On certain floor configurations, the “hero” will tumble and spin out of control. I know there is something to avoid rotation (isFixedRotation) but it looks a bit weird. [import]uid: 136402 topic_id: 28736 reply_id: 116740[/import]