It is currently Mon Jul 16, 2018 4:06 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Wrap-around game maps
PostPosted: Thu Apr 20, 2006 8:17 am 
Offline
User avatar

Joined: Sat Jul 09, 2005 6:03 am
Posts: 85
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?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 11:31 am 
Offline
User avatar

Joined: Sat Jul 09, 2005 6:03 am
Posts: 85
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 11:43 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 20249
Location: NE Indiana, USA (NTSC)
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 11:58 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10583
Location: Rio de Janeiro - Brazil
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 12:08 pm 
Offline
User avatar

Joined: Wed Nov 10, 2004 6:47 pm
Posts: 1849
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 12:21 pm 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10583
Location: Rio de Janeiro - Brazil
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 12:34 pm 
Offline
User avatar

Joined: Wed Nov 10, 2004 6:47 pm
Posts: 1849
OOooh, okay, that makes sense. My mistake. Whoops ^^


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 12:39 am 
Offline
User avatar

Joined: Fri Nov 19, 2004 7:35 pm
Posts: 4055
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.

_________________
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 5:23 am 
Offline
User avatar

Joined: Sat Jul 09, 2005 6:03 am
Posts: 85
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



Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group