Dark Bit Factory & Gravity
PROGRAMMING => General coding questions => Topic started by: Pixel_Outlaw on December 10, 2007
-
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:
-
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 (http://www.google.co.uk/search?hl=en&q=cramers+rule&btnG=Google+Search&meta=)
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.
-
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.
-
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
-
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.
[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]
-
Have some good Karma for that Rel.
-
Yes, thank you! :buddies:
-
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 (http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html)
Jim