Hi there,
I've added Box2D to my app over the weekend and am just getting started. (it's been lovely to use so far). I've got a couple of basic questions I was hoping someone could shine a light on please?
I'm trying to create an air hockey style game where the player has a rectangular static object bat at the bottom of the screen with the AI having one at the top of the screen and the puck is just a dynamic sphere object. (The table is a body with zero gravity with edge fixtures defining the boundaries).
The players can move the bat freely (within a "my end of the table" bounding area) to hit the puck up or down towards the opposing player. So in laymans terms, the faster the bat moves, the harder the ball is hit and as a result, the faster it moves back to the other player in the direction of the strike - within sanitized max speed limits.
There's a couple of guidelines I'm looking for:
- general set up - at the mo I've got two static bat objects, a dynamic sphere for the puck and edge fixtures for the table. Does that sound correct? I'm not convinced because:
- positioning the bat. If the player touches the bat on screen it becomes "draggable" and will follow the finger until the player stops touching the screen. The bat is currently a static object / body and i'm using b2Body.SetTransform to move it around. I get the feeling that this isn't the correct approach though?
The player is able to move the bat within the puck and separate the physics from the graphics. I'm not sure how to get round this - I've got a collision listener set up so my first solution was to disable bat dragging on collision but this seems to kill the ability for bat to actually hit the ball as the bat stops moving and the ball moves very little.
- applying the correct force to the bat. The faster the player moves the bat around, the bigger the force I want it to apply in a collision with the ball - How do I keep the object under player control but apply a force to it? Since using setTransform essentially teleports the bat around, It's not going to work like that- I'm guessing I should get the direction and velocity of the finger movement (via current frame and previous frame positioning deltas) and applying that as a force to the bat object?
- Does anyone know the correct approach to manipulating a object that will also correctly override the player if it's involved in a collision? I have "UpdateFromPhysics" to update my render object from Box2d and "UpdateToPhysics" to apply positional updates to the render object to the bat physics but they counteract each other and seem to be one or the other.
Oh and for some reason, my puck keeps going to sleep and then ignoring collisions from then on - I had to disallow sleeping on all objects - any particular reason for this?
Cheers - I'll add some more detail to how I've currently got it set up when I get back to the house.
I've added Box2D to my app over the weekend and am just getting started. (it's been lovely to use so far). I've got a couple of basic questions I was hoping someone could shine a light on please?
I'm trying to create an air hockey style game where the player has a rectangular static object bat at the bottom of the screen with the AI having one at the top of the screen and the puck is just a dynamic sphere object. (The table is a body with zero gravity with edge fixtures defining the boundaries).
The players can move the bat freely (within a "my end of the table" bounding area) to hit the puck up or down towards the opposing player. So in laymans terms, the faster the bat moves, the harder the ball is hit and as a result, the faster it moves back to the other player in the direction of the strike - within sanitized max speed limits.
There's a couple of guidelines I'm looking for:
- general set up - at the mo I've got two static bat objects, a dynamic sphere for the puck and edge fixtures for the table. Does that sound correct? I'm not convinced because:
- positioning the bat. If the player touches the bat on screen it becomes "draggable" and will follow the finger until the player stops touching the screen. The bat is currently a static object / body and i'm using b2Body.SetTransform to move it around. I get the feeling that this isn't the correct approach though?
The player is able to move the bat within the puck and separate the physics from the graphics. I'm not sure how to get round this - I've got a collision listener set up so my first solution was to disable bat dragging on collision but this seems to kill the ability for bat to actually hit the ball as the bat stops moving and the ball moves very little.
- applying the correct force to the bat. The faster the player moves the bat around, the bigger the force I want it to apply in a collision with the ball - How do I keep the object under player control but apply a force to it? Since using setTransform essentially teleports the bat around, It's not going to work like that- I'm guessing I should get the direction and velocity of the finger movement (via current frame and previous frame positioning deltas) and applying that as a force to the bat object?
- Does anyone know the correct approach to manipulating a object that will also correctly override the player if it's involved in a collision? I have "UpdateFromPhysics" to update my render object from Box2d and "UpdateToPhysics" to apply positional updates to the render object to the bat physics but they counteract each other and seem to be one or the other.
Oh and for some reason, my puck keeps going to sleep and then ignoring collisions from then on - I had to disallow sleeping on all objects - any particular reason for this?
Cheers - I'll add some more detail to how I've currently got it set up when I get back to the house.