Jump to content

  • Log In with Google      Sign In   
  • Create Account


Circular Playing Field


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 ewaldhew   Members   -  Reputation: 112

Like
0Likes
Like

Posted 14 March 2013 - 05:19 AM

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):
 
circular.jpg
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

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.

Sponsor:

#2 phil_t   Crossbones+   -  Reputation: 3258

Like
0Likes
Like

Posted 14 March 2013 - 06:13 AM

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.

#3 Álvaro   Crossbones+   -  Reputation: 12918

Like
0Likes
Like

Posted 14 March 2013 - 07:49 AM

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



#4 EWClay   Members   -  Reputation: 659

Like
2Likes
Like

Posted 14 March 2013 - 08:05 AM

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.

#5 ewaldhew   Members   -  Reputation: 112

Like
0Likes
Like

Posted 15 March 2013 - 07:43 AM

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, 15 March 2013 - 08:53 AM.


#6 matrisking   Members   -  Reputation: 282

Like
0Likes
Like

Posted 15 March 2013 - 02:28 PM

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! :]



#7 ewaldhew   Members   -  Reputation: 112

Like
1Likes
Like

Posted 16 March 2013 - 03:13 AM

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:






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS