Jump + Physics: Problems in collision

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
Schbaltz
Prole
Posts: 14
Joined: Fri Jul 20, 2012 12:21 am

Jump + Physics: Problems in collision

Post by Schbaltz »

Hello guys,

I'm creating a "creative" game like terraria (but, it will be a "farm" game hehe)... Ok, i'm trying to check when my character collides with the ground (after collides with the ground, the jumping variable must be set to false, when false, the character can jump again right?)..ok... but, the problem is:
if character collides with a "wall", the jumping variable is set to false, then, if i continue to press "jump button", my character go away to the sky (¬¬)...

I don't know how to test if the character collided in a ground, the ground could be : over a table, a terrain, over a box, e.g...but it must NEVER get the collision when it is a wall collision, or a "monster" collision, or any other objects around the game world...

Here is my key controller:

Code: Select all

instance.update = function (dt)
	
		local needAnimate = false
		if love.keyboard.isDown("d") then
			if instance.direction == -1 then
					instance.direction = 1
					instance.animation = anim8.newAnimation('bounce',instance.grid('1-3,2'),0.1)
				end
			instance.body:applyForce(400,0)
			needAnimate = true
		end
		
		if love.keyboard.isDown("w") and (not instance.isJumping) then
			instance.body:applyForce(0,-150000)
			instance.isJumping = true
		end
		
		if love.keyboard.isDown("a") then
				if instance.direction == 1 then
					instance.direction = -1
					instance.animation = anim8.newAnimation('bounce',instance.grid('1-3,1'),0.1)
				end
				instance.body:applyForce(-400,0)
				needAnimate = true
		end
		
		if(needAnimate) then
			instance.animation:update(dt)
		else
			instance.animation:gotoFrame(2)
		end
	end
and here, when i get the collision event:

Code: Select all

function beginContact(a, b, coll)
	mainChar.isJumping = false
end
I'm so sorry for my bad english, i'm brazilian, i hope you to understand me...

Thx guys!
User avatar
juno
Citizen
Posts: 85
Joined: Thu May 10, 2012 4:32 pm
Location: London

Re: Jump + Physics: Problems in collision

Post by juno »

Schbaltz wrote:Hello guys,

I'm creating a "creative" game like terraria (but, it will be a "farm" game hehe)... Ok, i'm trying to check when my character collides with the ground (after collides with the ground, the jumping variable must be set to false, when false, the character can jump again right?)..ok... but, the problem is:
if character collides with a "wall", the jumping variable is set to false, then, if i continue to press "jump button", my character go away to the sky (¬¬)...

I don't know how to test if the character collided in a ground, the ground could be : over a table, a terrain, over a box, e.g...but it must NEVER get the collision when it is a wall collision, or a "monster" collision, or any other objects around the game world...

Here is my key controller:

Code: Select all

function beginContact(a, b, coll)
	mainChar.isJumping = false
end
I think the key is with the velocity of the character...
When you press the jump button, you apply the negative force to make him jump (mainChar.isJumping = true)

When the character's velocity reaches >0 then he is falling again... (mainChar.isJumping = false)

Then if you change your jump function to something like:

Code: Select all

if love.keyboard.isDown("w") and ( instance.body.velocity.y == 0 and instance.body.isJumping == false) then
         instance.body:applyForce(0,-150000)
         instance.isJumping = true
end
wat ya mean she's in another castle!?
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Jump + Physics: Problems in collision

Post by coffee »

Couldn't also help a creation of a status variable for inAir or isFalling? or isJumping = "Ascend/Descend/Nil"
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Jump + Physics: Problems in collision

Post by bartbes »

You could check the contact, if it happens at the character's feet, you're pretty much hitting ground. (Or doing mad spins.)
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

Re: Jump + Physics: Problems in collision

Post by OmarShehata »

You could also attach a fixture as a sensor https://love2d.org/wiki/Fixture:setSensor at the player's feet. Where the sensor shape's width would be slightly less than the character's body, so it wouldn't detect from the sides but would only detect from the bottom.
Schbaltz
Prole
Posts: 14
Joined: Fri Jul 20, 2012 12:21 am

Re: Jump + Physics: Problems in collision

Post by Schbaltz »

Hello!!!

Thanks for all!!!

First, i did this:

Code: Select all

speedX,speedY = instance.body:getLinearVelocity()
if love.keyboard.isDown("w") and (speedY > -0.05 and speedY < 0.05 and not instance.isJumping) then
			instance.body:applyLinearImpulse(0,-200)
			instance.body:applyForce(speedX,0)
			instance.isJumping = true
end
there is something interesting... i tried to test speedY == 0, but when the character "jumps" over a box or any other dynamic object, the jump has about 1 second delay...i think this happens because "dynamic" objects get some minimal velocity when collides with any other object (in this case, my character object)...well, after insert speedY > -0.05 and speedY < 0.05 i didn't get any delay...

I post a img to you take a look...

Thx!
Attachments
collision (2).png
collision (2).png (248.49 KiB) Viewed 2739 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests