Hello,
For my 2d hockey game I am trying to figure out how to handle collisions with the puck and the boards. This is not one of my areas of expertise so I could really use your help, especially when it comes to the rounded corners. This is what I do now:
The short and long sides of the boards are each represented by a line and the rounded corners by a circle each. The puck is really just a point in the game but when doing collision checking I use a line with a starting point of the previous position of the puck and an end point of the current position + a value (to avoid the risk of missing the collision). Then I use a line/line intersection test (short or long side) or line/circle intersection test (corners) to get the intersection point. When the puck's current position is on the back side of the board's plane (outside the rink) it collided with I move the puck back to the intersection point and reflect the velocity vector based on the normal of the intersection point. Finally the distance that the puck traveled outside the rink is added to the current position but along the direction of the new velocity instead.
While this seems to work for keeping the puck inside the rink the puck travels far from smooth through the corners. For example if I shoot the puck right along one of the long sides of the rink I want the puck to slide along the corner and exit parallell to the short side; this doesn't happen, it exits at different angles depending on the speed of the puck.
Any idea what I am doing wrong? Is it possible to get smooth movement along a circle when reflecting the velocity? I appreciate any help.
Collision detection and response along a circle
While I cannot help you directly with your question, I can offer a different, and much easier solution, to your problem.
2d physics library. Personally, when I learned about them and started using them, it made everything so much easier. I use chipmunk-physics, but box2d is also popular.
For your game, I think it's an obvious choice. Basically, to simulate your situation, you would:
1: Create a physics space.
2: Create your puck as a body and give it a circle shape and define it (mass, radius, friction, elasticity, etc).
3: Create your walls as a static body (it doesn't handle rounded segments, but many short lines would work for the corners) and define it's elasticity.
4: Give your puck a force
5: Watch it go.
The physics engine will handle all the collisions for you, and do the proper responses as well. You can register for callbacks on collisions for certain types (in the goal for example).
Now, if you want to handle all the physics and collisions and responses yourself for a learning exercise, then disregard. But, if you are just interested in making a fun game, 2d physics engine is the way to go. (My old blog details a game I made using one).
2d physics library. Personally, when I learned about them and started using them, it made everything so much easier. I use chipmunk-physics, but box2d is also popular.
For your game, I think it's an obvious choice. Basically, to simulate your situation, you would:
1: Create a physics space.
2: Create your puck as a body and give it a circle shape and define it (mass, radius, friction, elasticity, etc).
3: Create your walls as a static body (it doesn't handle rounded segments, but many short lines would work for the corners) and define it's elasticity.
4: Give your puck a force
5: Watch it go.
The physics engine will handle all the collisions for you, and do the proper responses as well. You can register for callbacks on collisions for certain types (in the goal for example).
Now, if you want to handle all the physics and collisions and responses yourself for a learning exercise, then disregard. But, if you are just interested in making a fun game, 2d physics engine is the way to go. (My old blog details a game I made using one).
Thanks but I would prefer to learn more about this and implement it myself. But a physics library for C# could be plan B.
This is rough, but it may point you in the right direction. My math is rough and I havent tested this:
You can treat the corners as circles, for instance, the for the TOP RIGHT CORNER, to detect colision, you can do
You would to do this for each of the four corners.
You can treat the corners as circles, for instance, the for the TOP RIGHT CORNER, to detect colision, you can do
int free=0; int glide=1;
pAngle= angle of puck movement (use math.tan(pdy/pdx) I think , where pdxpdy= difference in puck x/y
//check if it is hitting the curved wall, cx/cy= center of circle, px/py= puck coordinates
if ( px>cx && py>cy && (Math.sqrt( (px-cx)*(px-cx)+(py-cy)*(py-cy) ) > radius) )
{
//angle of the wall where the puck hits. Be carefull about radians/degrees
wallAngle= math.atan2((py-cy) ,(px-py))
//code that compares angle of puck movement to angle of the wall where the puck it.
if (math.abs(wallAngle-pAngle) > 1) //pick which angle the puck should ricochet at
{
puck rickochets
puckMode==free;
}
else //low angle collison
{
//put code to glide, or trace the circle until px<cx or py<cy
puckMode=glide;
}
}
You would to do this for each of the four corners.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement