• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
B0rn2c0de

box2d checking for multiple collisions simultaneously

6 posts in this topic

i am currently working on a 2d endless runner using sfml and box 2d in which my player body comprises of 2 fixtures one for the main body and one sensor for checking if it is grounded . also the game consisits of obstacles on which the player can stand and jump ..what want to do is to kill the player if the main body fixture touches the obstacle but not the other ground fixture because that happens when the player is standing on the obstacle .. is there a way to implement such a condition or is there any workaround on how to kill the player when he collides with an obstacle ?? all the fixtures have seperate int tags as the user data ex- every obstacle has int tag of 5 and the player ground sensor has the int tag of 4 .

0

Share this post


Link to post
Share on other sites

Yes, you can give the world a contact listener with which you check if the bodies of the collided fixtures are the player and an obstacle.

There are basically two ways about this, either you set up the contact listener, or you manually iterate over all contact after every step and manually check if the player and an obstacle are colliding. The contact listener is the superior way, because you don't have to do any kind of iteration, Box2D will provide you with the two fixtures that are colliding.

 

Some sample code from my Pong clone:

void PongGame::speedUpBall(b2Contact* contact)
{
	auto fA = contact->GetFixtureA();
	auto fB = contact->GetFixtureB();
	
	auto bA = fA->GetBody();
	auto bB = fB->GetBody();
	b2Body* paddle = nullptr;
	b2Body* ball = nullptr;
	if(bA->GetUserData() == &m_ball)
	{
		ball = bA;
		paddle = bB;
	}
	else if(bB->GetUserData() == &m_ball)
	{
		ball = bB;
		paddle = bA;
	}
	else
	{
		DEBUG_INFO("No ball in collision, leaving");
		return;
	}
		
	if(paddle->GetUserData() != &m_leftPaddle && paddle->GetUserData() != &m_rightPaddle)
	{
		DEBUG_INFO("No paddle in collision, leaving");
		return;
	}
}
0

Share this post


Link to post
Share on other sites

Just remember to never modify any of your physics bodies in response directly to one of these callbacks. Box2D doesn't like that at all. You need to cache the contacts in some kind of structure and process them after the update() call if you want to do that.

0

Share this post


Link to post
Share on other sites

i have done something like that the problem is the contact callback is called even when the player is standing on top of an obstacle . i want to to tackle that problem like i want to check if the player is colliding with the obstacle but not standing on it  so thta i can kll my player when he collides with an obstacle from the sides and not when he is standing on it.. any ideas on how i can do that ??

0

Share this post


Link to post
Share on other sites

Can you not check if a) the player intersects the object and b) the ground sensor doesn't intersect the object?

 

Stuff like this can be tricky. The other approach is to consider the player velocity when the collision occurs - if within a certain tolerance of straight down, consider him standing, if not, consider it a side collision.

0

Share this post


Link to post
Share on other sites

thats the thing i cant figure out that how do i check both a) and b) conditions simultaneously because the begin contact returns only one pair of fixtures at at a time .. any ideas about that ? .. i will try with the other approach .. but the first one will be more precise

0

Share this post


Link to post
Share on other sites

Don't do that inside your contact listener, instead, when the contact listener is called mark that the player is colliding with the obstacle inside your Player struct/class. Same thing with the ground sensor.

 

Then, after world.step() returns, check your boolean(s) to determine if you have to kill the player or not.

 

Example (pseudo code for the most part):

void killCheck(b2Contact* contact)
{
   auto fa = contact->getFixtureA();
   auto fb = contact->getFixtureB();

   auto ba = fa->getBody();
   auto bb = fb->getBody();

   if(ba->getUserData() == ENUM_PLAYER_TYPE && bb->getUserData() == ENUM_OBSTACLE_TYPE)
   {
      //the vice versa should also be checked, if bb is player and ba is obstacle
      m_player.isPlayerColliding = true;
   }

   if(ba->getUserData() == ENUM_GROUND_SENSOR_TYPE && bb->getUserData() == ENUM_OBSTACLE_TYPE)
   {
      //the vice versa should also be checked, if bb is sensor and ba is obstacle
      m_player.isSensorColliding = true;
   }
}


///after world.step() returns

if(m_player.isPlayerColliding == true && m_player.isSensorColliding == false)
{
   //kill player
}
...
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0