Help with 2 things, Camera following character, and jumping

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
veethree
Inner party member
Posts: 876
Joined: Sat Dec 10, 2011 7:18 pm

Help with 2 things, Camera following character, and jumping

Post by veethree »

Hey löve guys. So before i get to my problems, I'd like to make it clear that i'm new to love and lua, But i have some experience with coding. I know the basics of vb.net and i used to code sa-mp(san andreas multiplayer) servers, but the samp thing was way back. I also briefly played around with cleo scripts for san andreas.

Now to my problems.

I'm just testing out löve really, And what i'm trying to do is make a platform game with physics. I got some standard game mechanics down, But one thing i want to do is make a camera follow my character (just a white ball) when it's moved vertically(so the camera only moves up and down, not to the sides). On that i haven't even started, don't know where to start to be honest.

2nd problem is with jumping. Right now my character can actually jump, But the problem is you can jump as often as you want, so if you spam the jump key the character just keeps going up. I used objects.ball.body:applyImpulse for the jumping. I had an idea on how to deal with this earlier, but that doesn't work that well..it prevents double jumping, but you can still just spam the key. here's the idea i had:

Code: Select all

if key == "up" then
		if jumps == 1 then
			objects.ball.body:applyImpulse(0, -50)
			jumps = 2 
		elseif jumps == 2 then
			jumps = 1
		end
	end
Well hope someone can help me out with at least one of these, Thanks in advance. (included the lua file below if that helps.)
-Veethree
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Help with 2 things, Camera following character, and jump

Post by Taehl »

As for jumping only when you're touching the ground... Not sure, I don't use Box2D. But don't fret, someone else will be along shortly to answer that.

As for the camera following the player... Here's a very simple way:

Code: Select all

love.draw()
	love.graphics.push()
	love.graphics.translate(0, -objects.ball.body:getY())

	-- normal drawing stuff here
	
	love.graphics.pop()
	-- if you draw a HUD or something, it would go here
end
Translate simply repositions all drawn graphics.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
veethree
Inner party member
Posts: 876
Joined: Sat Dec 10, 2011 7:18 pm

Re: Help with 2 things, Camera following character, and jump

Post by veethree »

Thanks man! that works perfectly. But i just noticed another problem..is there a way to like extend the world? when i jump outside the frame the ball just freezes at the top..any way around that?
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Help with 2 things, Camera following character, and jump

Post by tentus »

You're going to have to set some callbacks for your world, and make it a lot bigger than 512x512. Also, make "jumps" a number representing how many jumps the player can do in a row- for most games this is one, but some games offer more (double jumping). Every time the player hits the jump button, the number of jumps left is decreased by one until it equals zero. Then, when the player collides with something below them, their jumps available is replenished to full.

I can't totally recommend checking out my game Kurosuke, since it's a very convoluted bit of code, but it has exactly the answers you're looking for buries inside it. I had to tackle all the probles you've asked about so far, and have come up with... something. Sometimes my solutions are pretty crude, but it's still pretty workable.
Kurosuke needs beta testers
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Help with 2 things, Camera following character, and jump

Post by The Burrito »

veethree wrote:when i jump outside the frame the ball just freezes at the top..any way around that?
Right now your best off just make the world bigger at the start like tentus said, in 0.8.0 that will no longer be a problem, but it doesn't hurt to make the world massive (if you're only going to be moving up, then maybe set the minimum y to negative a couple thousand or something)
tentus wrote:You're going to have to set some callbacks for your world
I actually disagree with tentus on this, while the callback system is pretty useful there are some issues (the first of which will be bumping into a wall counts as touching the ground). I prefer manually checking for collisions with a specific point, something like this:

Code: Select all

collision = false
	x,y = objects.ball.body:getWorldPoint(0,21)
	if objects.ground.shape:testPoint(x,y) then
		collision = true
		jumps = 2
	elseif objects.platform.shape:testPoint(x,y) then
		collision = true
		jumps = 2
	end
I know that seems like it would be super slow with a bunch of platforms but box2D's lookup for that is faster than you'd think and you can put them in their own table for easy iteration.

Heres what that might look like with double jumping the way tentus suggested:
Attachments
main.lua
(2.19 KiB) Downloaded 90 times
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests