-2

I might be stupid or something. But, I'm having a hard time with this. I usually find examples of code but it just confuses me. Unfortunately there isn't any good tutorial for this. I been using Lua for almost a year so I kinda have experience. Help would be extremely appreciate!

Basically I want to learn how to make rectangle jump up and then go back down.

Probix
  • 11
  • 4
  • What do you mean with making a rectangle jump? with physics and all that? acceleration, gravity and all that stuff? just up or in any direction? provide a sketch or more info what you want to achieve. you spend 3 lines to explain what you cannot find but not a single line on what you actually want to know... – Piglet Nov 30 '16 at 11:17
  • My apologies. Basically I want to learn how to make a rectangle jump up and then back down. Literally just simple jumping. – Probix Dec 01 '16 at 02:14
  • and what stops you from reading the manual? https://love2d.org/wiki/Tutorial:Baseline_2D_Platformer#Part_4:_Jumping – Piglet Dec 01 '16 at 07:21

1 Answers1

2

For a single block that you control, you essentially need to store it's gravity and figure out a nice acceleration for it.

currentGravity = 0;
gravity = 1;

Then in the loop, you have to check if it's on ground using some collision detection. You want to add the gravity acceleration to currentGravity:

currentGravity = currentGravity + gravity

Then, you add it to the current y axis of the block:

YAxis = YAxis + currentGravity

Once you land, make sure to set gravity to 0. Also be sure to keep setting it to 0 to ensure you don't fall through the ground (as you kept adding to gravity no matter what.)

if not inAir() then
    currentGravity = 0
end

And, of course, to jump, set currentGravity to a negative number (like 20) (if that's how you made the gravity work.)

Here's a collision detection function I made for Love2D:

function checkCollision(Pos1,Size1,Pos2,Size2)

    local W1,H1,W2,H2 = Size1[1]/2,Size1[2]/2,Size2[1]/2,Size2[2]/2

    local Center1,Center2 = {Pos1[1]+W1,Pos1[2]+H1},{Pos2[1]+W2,Pos2[2]+H2}

    local c1 = Center1[1]+(W1) > Center2[1]-W2 
    local c2 = Center1[1]-(W1) < Center2[1]+W2
    local c3 = Center1[2]+(H1) > Center2[2]-H2
    local c4 = Center1[2]-(H1) < Center2[2]+H2 

    if (c1 and c2) and (c3 and c4) then
        return true
    end
    return false
end

It assumes the position you gave it is the center of the block. If the box is rotated it won't work. You'll have to figure out how to make it work with walls and the like. And yes, it's ugly because it's very old. :p

Kertopher
  • 31
  • 3