I have a question about wrap-around maps (maps with no edge clipping). How do you test if a point on that type of map is in a specific rectangular area?
I'll explain more.
In my little RPG project, map coordinates are always from (0-map_width-1,0-map_height-1), even on the world map which wraps around. To test if other entities are on screen or to test which random encounter region the player is in, I need to test if a point lies in a given rectangle.
The problem is when the testing rectangle is intersected by the map's edges. Normal testing won't work and I've racked my brain trying to think of a solution for it.
You test against the rectangle on one side of the map, and then you move the rectangle to the other side of the map and test against that.
In which directions does your map wrap? If it wraps both N-S and E-W, then your planet is not a sphere; it's a torus.
I was going to say something similar: break up the rectangle in smaller rectangles when it goes over the edge of the map. And you test both new rectangles. If the map wraps on both axes you'll be testing 4 rectangles in the worst case.
EDIT: You could probably use the concept of "logical address" and "physical address". Do all the calculations based on the logical address, but when/if you need to read any data from the map you convert the logical address to a physical one.
I would avoid this scenario altogether and just not have rectanges which cross over the edge of the map.
If you want to have a enemy region rect to cross over the edge of the map for some strange reason -- instead of breaking it into two at runtime and checking each rect -- break it into two when you're designing the map and just have two region rects with the same properties -- neither of which go beyond map boundaries.
Only way I can see something like this being a problem is if you want region rects move around in-game... but I don't see any reason for that.
From what I understood the rectangles do move. He said he wanted to check if "other entities are on screen", so I think one of these rectangles represents the screen, wich would move.
I still didn't get exactly how the engine works, but maybe there is a better overall method to do this, wich doesnt involve rectangles.
OOooh, okay, that makes sense. My mistake. Whoops ^^
Just test four times. Normal test, then test with x-width, then test with y-height, then test with x-width and y-height.
Rectangles that go off the left or top edge should have width/height added to their x/y position if it is negative.
Thank you everyone for your help. I've come up with alittle rectangle testing routine that, as far as I can tell, works great (from testing I did).
Code:
.proc map_test_rec ; In :
; rx = Rectangle's Top Left x pos
; ry = Rectangle's Top Left y pos
; width = Width of rectangle
; height = Height of rectangle
; tx = Test point x
; ty = Test point y
; Out : a/z = 1 inside a/z = 0 outside
; map_twidth and map_theight are the tile sized
; dimensions of the current map
lda rx
clc
adc width
cmp map_twidth
bcc no_clipped_width
sbc map_twidth
cmp tx
bcs test_ver
bcc test_left
no_clipped_width:
cmp tx
bcc not_inside
test_left:
lda tx
cmp rx
bcc not_inside
test_ver:
lda ry
clc
adc height
cmp map_theight
bcc no_clipped_height
sbc map_theight
cmp ty
bcs is_inside
bcc test_up
no_clipped_height:
cmp ty
bcc not_inside
test_up:
lda ty
cmp ry
bcc not_inside
is_inside:
lda #1
rts
not_inside:
lda #0
rts
.endproc