Page 1 of 1

Jump + Physics: Problems in collision

Posted: Fri Jul 20, 2012 12:39 am
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!

Re: Jump + Physics: Problems in collision

Posted: Fri Jul 20, 2012 9:18 am
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

Re: Jump + Physics: Problems in collision

Posted: Fri Jul 20, 2012 10:09 am
by coffee
Couldn't also help a creation of a status variable for inAir or isFalling? or isJumping = "Ascend/Descend/Nil"

Re: Jump + Physics: Problems in collision

Posted: Sat Jul 21, 2012 10:20 am
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.)

Re: Jump + Physics: Problems in collision

Posted: Sat Jul 21, 2012 2:25 pm
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.

Re: Jump + Physics: Problems in collision

Posted: Sat Jul 21, 2012 4:18 pm
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!