# Circular Playing Field

This topic is 1770 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all,

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 [left], 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.

##### Share on other sites
This is basically "wall sliding". Except instead of a straight wall with a single normal vector, you have a curved surface with the surface normal being dependent on where you hit it (and pointing to the center of the playing field).

If you google "collision detection wall sliding" you can probably find some helpful info.

##### Share on other sites

The easiest method is to move the player to the closest valid position.

##### Share on other sites
Move the player as normal. If the distance from the centre to the player is greater than the maximum permitted, move the player directly back towards the centre by the excess amount.

Angles and trig functions are not required. All you need is the vector from the centre to the player. There's also no need to mix the movement code and the collision code.

##### Share on other sites
Thanks for all the input, I finally solved it.
However, one problem remains, is that the movement seems...jagged, for lack of a better term, as if the curved wall was actually made of steps.

EDIT: I fixed the jagged movement too, thanks again for the help =D Edited by ewaldhew

##### Share on other sites

Thanks for all the input, I finally solved it.
However, one problem remains, is that the movement seems...jagged, for lack of a better term, as if the curved wall was actually made of steps.

EDIT: I fixed the jagged movement too, thanks again for the help =D

Glad you figured out the solution, now tell us how you fixed it! :]

##### Share on other sites

Well, I basically sat down and considered carefully the four possible cases (in each of the four quadrants) and figured out that if the player was on the right half (?/2 < ? < -?/2) of the field, rx would be -rcos? and rwould be -rsin?, where r is the player's resultant speed (playerspeed for horizontal/vertical movement, and sqrt2*playerspeed for diagonal). Then, on the left side, rx = rcos(?+?), ry = rsin(?+?). What I was doing wrong before was, as you can see from the bit of code in my first post, not computing r at all, and using tan, as well as getting Newton's Third Law wrong. Now that I got my maths(and physics) figured out, it works exactly as I wanted it to.

Also, the jagged movement was due to me forgetting to add in the code to restrict the player's position. So the player would move outside the boundary momentarily before being pushed back in by the restoring force.

In conclusion, I was not thinking straight at all before, and adopted a totally illogical approach =\ I still feel kinda dumb even now D: