Box2D Forums

It is currently Wed May 22, 2013 8:06 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Jun 15, 2011 5:05 pm 
Offline

Joined: Sat Dec 18, 2010 6:28 pm
Posts: 15
In my game, I make the player jump by applying a force. Unfortunately, this leads to the jump button being able to be pressed even when the player is in the air. Is there a way to determine if the body is touching the ground (and not rubbing on a side or something) ?

Thanks!


Top
 Profile  
 
PostPosted: Wed Jun 15, 2011 7:21 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
You can use collision callbacks to detect a collision with fixtures that you previously tagged (user data) as ground.
You can attach a small sensor fixture under the player to detect when something is under them.
You can look at the normals in the manifold data in the contact to determine if the other fixture is under the player (not recommended).


Top
 Profile  
 
PostPosted: Thu Jun 16, 2011 7:03 am 
Offline

Joined: Tue Feb 16, 2010 3:08 pm
Posts: 21
Also, you can do something like this too, to find out what shapes are at a certain point:
(i.e. define a point where the player's feet would be )

var maxShapes:int = 10;
var testAABB = new AABB();
testAABB.upperBound = player x, player y
testAABB.lowerBound = player.x + 0.001, player y + 0.001;

Shapes[] shapesUnderCharacter = world.queryAABB( AABB, maxShapes )

for each ( Shape s in shapesUnderCharacter ){
if ( s.getBody() == groundBody ){
grounded = true;
}
}

Basically what you're doing is making a tiny tiny bounding box, and
seeing what shapes overlap it.


Top
 Profile  
 
PostPosted: Fri Jun 17, 2011 9:24 am 
Offline

Joined: Mon Jun 13, 2011 5:19 am
Posts: 9
I've decided not to go for a complicated way (though probably a lot better ;-)) like Nopstick showed, but rather just check the Y-velocity. There's a minimal chance that you will be able to double-jump when at the peak of your jump, but after 10 minutes of holding the jump button, I decided it

NEVER.
HAPPENS.

Code:
//(c++)
//class variable
bool isMidair;

//update function
if ( body->GetLinearVelocity().y != 0 ) {
    isMidAir = true;
} else {
    isMidAir = false;
}


Top
 Profile  
 
PostPosted: Fri Jun 17, 2011 7:38 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
I guess that would work if your game has no slopes the player can run up/down, and he is standing still.


Top
 Profile  
 
PostPosted: Sat Jun 18, 2011 8:01 am 
Offline

Joined: Mon Jun 13, 2011 5:19 am
Posts: 9
irresistible force wrote:
I guess that would work if your game has no slopes the player can run up/down, and he is standing still.


You are ever so correct - I'm currently doing the approach nopstick posted.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group