I am currently making a 2D shooting game, but have gotten stuck trying to solve this one problem.
I have a circular playing field, which is the area the player (or other entities) is allowed to move around in. However, I have tried all the method I could think of, and searched the forums here in an attempt to find anything useful, but did not. I have no problem with detecting when the player is on the boundary, but restricting the player within it is the problem for me..
What I wish to achieve is that the player will hit the wall and, if it is not on any of the eight cardinal or ordinal "corners" of the circle, slide towards it. So, for example, if the user held down [up] and
, the player would move northwest until it hits the wall, then continue to slide along till it is at the northwest position on the circle.
Illustration of my point (I hope it is clear enough):
As can be seen, my idea was to calculate an opposing force r. So, in this example, ry cancels out the player's y velocity, but the player continues to move leftwards. So as the player moves towards northwest, rx grows larger and eventually cancels out the player's x velocity too making that the final resting position.
I am not sure if there is any flaw in this approach itself, or that my method of computing rx and ry were wrong. Here is my most recent try:
[spoiler]
if (distance(me.sprite.position.x, me.sprite.position.y, 300, 300) >= 150)
{
if (_controls.pressing.up)
{
ry = -me.speedY;
if (_controls.pressing.left) //2ndQ
{
if (me.sprite.angle != -Math.PI/2)
rx = ry*Math.tan(me.sprite.angle);
}
else if (_controls.pressing.right) //1stQ
{
if (me.sprite.angle != -Math.PI/2)
rx = ry*Math.tan(Math.PI/2 - me.sprite.angle)
}
else rx = 0;
}
else if (_controls.pressing.down)
{ etc...
}
}
me.speedX += rx;
me.speedY += ry;
Note: distance() calculates the distance from (300,300)[the center of the window] to the player's position.angle is the angle from the center (as marked in the diagram above)
I tried putting up/down and left/right together at first, but after that failed I tried to calculate separately for each quadrant, which obviously did not work either.
[/spoiler]
My maths isn't that good, so I am really out of ideas. If someone could give me any help it would be greatly appreciated! Thanks.