1

Hey, I'm trying to make a basic hockey style game. I have the basic physics working with the pitch, a player, an opponent, and a ball.

I'm struggling to figure out how I can allow the ball to travel into the goal while keeping the player and opponent from going into the goal.

Basically the opponent follows the ball, and the player is controlled by user input. so there are two restrictions I need.

I need to create a shape that only the ball can go through... is this even possible? Maybe I can sleep the opponent if the ball passes the goal line?

Am I going about it the right way?

Player1 can only move around in the red box Player2 can only move around in the green box The ball can move around in the outer blue box

playing field layout

epaik
  • 235
  • 2
  • 5
Rob
  • 7,039
  • 4
  • 44
  • 75

3 Answers3

2

If you're using Box2D, you probably want to look into Sensors.

Taken from the Box2D Manual:

Sometimes game logic needs to know when two fixtures overlap yet there should be no collision response. This is done by using sensors. A sensor is a fixture that detects collision but does not produce a response.

You can flag any fixture as being a sensor. Sensors may be static or dynamic. Remember that you may have multiple fixtures per body and you can have any mix of sensors and solid fixtures.

Sensors do not generate contact points. There are two ways to get the state of a sensor:

  1. b2Contact::IsTouching
  2. b2ContactListener::BeginContact and EndContact
epaik
  • 235
  • 2
  • 5
1

why don't you use simple collision detection?

if the object that moves over your hockey field is of type player and his position/hitbox collides with the goal, you change his position back to the line, so he possibly can't intersect with the goal.

if the type of object is not a player you simple let the object pass

sharpner
  • 3,857
  • 3
  • 19
  • 28
  • is this not going to cause simulation errors by forcing the position of the player? the box2d docs say "Manipulating a body's transform may cause non-physical behaviour" – Rob Mar 23 '11 at 12:38
  • i do not know what you mean by simulation errors, but you just have to distinguish between player and ball, you could even make the field only rectangular for the player and "normal" for the ball, – sharpner Mar 23 '11 at 12:39
  • Yeh that sounds like what I need to do, but I don't know if it's possible with box2d, maybe box2d isn't going to help me here. – Rob Mar 23 '11 at 12:43
0

Well, you don't allow the player to leave the playing field to the left or right do you? So You just need to do that same logic for the goal area for players.

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • the playing field is created with static objects all the way rounds, is there a way to create a virtual bounding box in box2d? – Rob Mar 23 '11 at 12:40