Author Topic: FAST way to limit the player to playing field?  (Read 3466 times)

0 Members and 1 Guest are viewing this topic.

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile

I make shmups, it's what I do. Consequently my game programming skills are specialized in one area. Recently I've wanted to design an arena shmup (no not Geometry Wars). My problem is using oddly shaped playing areas. I assume some sort of linear programming might be useful. Linear programming meaning using linear equations to define the edges of a polygon and the inner area. I have also thought of subdivision of the playing field into triangles. I need to also provide some sort of amateur ray casting to move the player back to a sensible position should his movement vector cross the edge of the playing surface.

In high school I was excited that we would be learning "linear programming" in math. I cursed my head off when I found out it was mistitled and worthless to me.

I know this question is broad has anyone done this kind of work? I'm used to just limiting the player to the inside of the screen.
Example of playing field:
« Last Edit: December 10, 2007 by Pixel_Outlaw »
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: FAST way to limit the player to playing field?
« Reply #1 on: December 10, 2007 »
I don't know if this is the fastest way about solving your problem, but you could use a little routine to detect if two lines are intersecting.

One line could be drawn through the players centre and this could be tested against the edges of the map.

You can find some stuff about this if you google Cramers Rule

There are also ofhter solutions, a representation of the map can be kept in memory and a location can be added to the map.

If legal areas are coloured blue for instance and the "dot" representing the players location is on a non-blue part of the map you could restrict the movement accordingly.

The second solution is the least mathematical but perhaps offers more interesting possibilities for map design.

Ray casting with curved walls for instance is not straight forward.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline p01

  • Atari ST
  • ***
  • Posts: 158
  • Karma: 51
    • View Profile
    • www.p01.org
Re: FAST way to limit the player to playing field?
« Reply #2 on: December 10, 2007 »
Well if your playfield is composed of primitives like the one in your image, it's quite simple to check if a unit lies in one primitive. First you need a spatial hash/group to figure in which sector the unit is and which primitives are in there, then check the position of the unit against the equation of the primitives in there. It's fast and really precise.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: FAST way to limit the player to playing field?
« Reply #3 on: December 10, 2007 »
If your map is bounded by a set of line segments, then it's pretty easy to work out if a point is inside or outside it.  First build a 2d line segment <-> 2d line segment intersection routine.  Then, pick any point which is definitely outside the map.  For every line segment in the map outline, work out if a line from the player to the outside point crosses that line segment.  If it does, then add 1 to a counter.  Once you've checked all the line segments, if the counter is odd then the player is inside the map, if the counter is even, then the player is outside.

Jim
Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: FAST way to limit the player to playing field?
« Reply #4 on: December 12, 2007 »
I would use either SAT or closest point on line algo.  They would also give you a good way to slide parallel to your line segments if you hit a wall. However Jim's idea is quite novel it should work too.

Here's an explanation:
http://petesqbsite.com/sections/express/issue21/index.html#bounce

There are some example exes to play around.

However, there's I know of a way to test if a point is inside a bounding polygon. You only need to use line normals.

Code: [Select]
[color=black]        'check if point is inside the poly
        dim as single dota
        dim as vector2d v
        v.x = new_rot_ls(i).x1 - msx
        v.y = new_rot_ls(i).y1 - msy
        'draw normal       
        head.x = new_rot_ls(i).x1 + new_rot_ls(i).normal.x * 20
        head.y = new_rot_ls(i).y1 + new_rot_ls(i).normal.y * 20   
        line (new_rot_ls(i).x1,new_rot_ls(i).y1)-(head.x,head.y), rgb(255,255,0)       
        dota = dot(v, new_rot_ls(i).normal)       
        dim as integer p_side = 0
        if dota >=0 then p_side = 1       
        sameside = sameside and p_side       [/color]
« Last Edit: December 12, 2007 by relsoft »
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: FAST way to limit the player to playing field?
« Reply #5 on: December 12, 2007 »
Have some good Karma for that Rel.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: FAST way to limit the player to playing field?
« Reply #6 on: December 12, 2007 »
Yes, thank you! :buddies:
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: FAST way to limit the player to playing field?
« Reply #7 on: December 12, 2007 »
My way definitely works, because I used it in my Halloween demo to determine the positions of the little cubes inside the big objects - that's what some of the precomp is about :)
But 'novel'?  I think the algorithm was invented in 1962 ;D
http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html

Jim
Challenge Trophies Won: