Make a platform in a 2D platformer

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Urvorn
Prole
Posts: 7
Joined: Thu Apr 12, 2018 5:36 pm

Make a platform in a 2D platformer

Post by Urvorn » Sat Apr 14, 2018 3:44 pm

Hi LOVE community !

So I have searched this topic on the forums but I've didn't find anything so here is the problem :

I've used a tutorial to help me creating the very basics of a 2D platformer : I have a ground, a character and now I want to put a platform
in the air. First of all in the sake of to not complicate the thing I've put the platform on the ground to try something : so I want my character to stay on it and then fall to the ground if he reaches the edges of this platform but I've don't figured out how.

So here is the code :

https://pastebin.com/cfkN5Bm0

You can see at line 67 to 72 what I've intended to do but it doesn't work.

Thank you in advance if someone want to help me.
"The most important is not to succeed, it's to try to succeed."

User avatar
pgimeno
Party member
Posts: 1909
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

Re: Make a platform in a 2D platformer

Post by pgimeno » Sat Apr 14, 2018 9:29 pm

Well, you can try to do tricks so that it more or less looks like physically accurate, which seems to be what you're trying to do. Or you can have something that more accurately resembles actual physics. Or you can use a physics engine (like love.physics, which uses Box2D internally) and let it handle the physics for you.

The first approach is useful in games with very simple physics, e.g. Pong. For a simple platformer, I'd recommend the second or the third approach, but the third one may be a bit overkill.

Remember that gravity constantly pulls objects down. If they don't fall, it's because they are colliding with something underneath. So you need to a) apply gravity constantly and b) check collisions. A jump is an impulse, which translates in practice to a sudden vertical upwards velocity. Gravity will take care of the parabolic movement that happens during a jump. You can only jump if you are colliding with an object under you (which is the object you use to take impulse), though some games allow double jumps.

Applying gravity is straightforward; you're already doing that. But it is always there, it's not conditional.

So, collision checking is the important part here. It's also the hardest. I suggest you take a look at the Bump library: viewtopic.php?f=5&t=79223

Using Bump, you would calculate a tentative new position for the player, by applying gravity and controls. You would then try to move the player there through a Bump function, and Bump will tell you the actual new coordinate for the player after handling the collisions. That will make your player not fall down while standing on ground, or on a platform.

User avatar
OmegaMax
Prole
Posts: 12
Joined: Wed Jun 07, 2017 3:49 pm

Re: Make a platform in a 2D platformer

Post by OmegaMax » Sun Apr 15, 2018 2:59 am

Firstly "Applying gravity is straightforward; you're already doing that. But it is always there, it's not conditional." <-- this depends on what Urvorn is trying to accomplish,if you want to duplicate older games like "mario,bubble bobble,ghosts and goblins ect" they don't add constant gravity.



;---------------------------------------------------------------
;6502 Assembly Code,gravity only applied during jump
;---------------------------------------------------------------
Do_HandleActorYVelocity:
LDA _ActorYVelocityL,X
CLC
ADC _ActorYVelocityLConst
STA _ActorYVelocityL,X
LDA _ActorYVelocityH,X
ADC _ActorYVelocityHConst
STA _ActorYVelocityH,X
CMP #$08
BMI +
LDA #MaxGravityForce
STA _ActorYVelocityH,X
LDA #$00
STA _ActorYVelocityL,X
+:
RTS -----------------------------------------


Ghosts and goblins uses a lookup table like so
;---------------------------------------------------------------
;C Code
;---------------------------------------------------------------
const ArthurJumpVelocityTable[] =
{
-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-2,-2,-2,-1,-1,0,-1,0,0,0,0,0,0,0,
1,1,1,2,2,2,2,2,3,3,3,3,4,4,4,4,4
};

ArthurJumping1()
{
ArthurYPosition += ArthurJumpVelocityTable[ArthurJumpIndex];
ArthurJumpIndex++;
ArthurFrame = ArthurJumpFrame1;
ArthurDIrectionHelp();
if (ArthurJumpIndex > 41) {
ArthurJumpIndex = 0;
ArthurJumpFlag = 0;
ArthurFrame = ArthurIdleFrame;
ArthurFrameIndex = 0;
ArthurState = ArthurOldState;
}
}

You need to learn bound box'rectangle' collision and to eject your entity from the amount of pixel it's overlapped
https://www.youtube.com/watch?v=pOunKCQsXPw <-- Using a look up table like I showed above,I've reversed engineered enough old 2d games to know majority of them don't use constant gravity.Gravity is used when the entity jumps or when it's falling 'example..walk off a tile,walk off platform'.You should post your love file when you need help.

User avatar
Urvorn
Prole
Posts: 7
Joined: Thu Apr 12, 2018 5:36 pm

Re: Make a platform in a 2D platformer

Post by Urvorn » Mon Apr 16, 2018 3:55 am

Ok, sorry for the late answer but I don't understand why I don't have the notifications...

What pgimeno said seems to be what I want to do so I'll try this. But currently I'm a beginner in Lua and I've maybe made a mistake by trying to do a platformer but anyway. Moreover I don't know a lot about libraries... Am I obliged to use the Bump libraries ? I don't like to use things that I don't understand but if I don't have the choice I will use it.

Thank you guys for your help ! :)
"The most important is not to succeed, it's to try to succeed."

User avatar
pgimeno
Party member
Posts: 1909
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

Re: Make a platform in a 2D platformer

Post by pgimeno » Mon Apr 16, 2018 10:52 am

Urvorn wrote:
Mon Apr 16, 2018 3:55 am
Ok, sorry for the late answer but I don't understand why I don't have the notifications...
No worries. You only get notifications if you're quoted. I've quoted you now so you get one :)

You can also subscribe to the thread if you want to receive notifications by email when someone posts, regardless of whether they quote you.

Urvorn wrote:
Mon Apr 16, 2018 3:55 am
[...] Am I obliged to use the Bump libraries ? I don't like to use things that I don't understand but if I don't have the choice I will use it.
Of course you're not obliged. You can implement your own collision physics. But as I said, collisions are the hardest part. Collision detection is just half of the problem; the other half is collision resolution (determining how to adjust the movement after the collision). Bump implements several strategies that are useful in different circumstances: bounce, slide, touch, cross.

You can read about how Bump implements collisions. The author explains it briefly here and links to an article about the method: viewtopic.php?f=4&t=83908&p=212348#p212348

You can implement the same method yourself, but due to the complexity of the task, you're better off just using the library. It's thoroughly documented and available here: https://github.com/kikito/bump.lua
Last edited by pgimeno on Mon Apr 16, 2018 10:55 am, edited 1 time in total.

User avatar
Urvorn
Prole
Posts: 7
Joined: Thu Apr 12, 2018 5:36 pm

Re: Make a platform in a 2D platformer

Post by Urvorn » Mon Apr 16, 2018 10:54 am

Ok I will use it thank you ! :)
"The most important is not to succeed, it's to try to succeed."

User avatar
OmegaMax
Prole
Posts: 12
Joined: Wed Jun 07, 2017 3:49 pm

Re: Make a platform in a 2D platformer

Post by OmegaMax » Fri Apr 20, 2018 6:43 pm

Were you able to get your platforms working Urvorn?

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 5 guests