Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: thydowulays on January 08, 2012, 12:44:06 am

Title: Help with tilemap collision?
Post by: thydowulays on January 08, 2012, 12:44:06 am
Okay, so I have a simple problem. I am making an RPG, and I have a tilemap. pxl-Test(ing doesn't work for tilemaps, so I just want to know how to find tiles on the map and if they are "solid" then don't let the player move. Here is my code:
Code: [Select]
.LOLZ
prgmLOLZDATA <-- My tilemap and sprites are stored in here
0->X->Y
Repeat getKey(15)
For(B,0,7
For(A,0,11
{B*12+A+GDB1}->T
If T
T--
Pt-On(A*8,B*8,T*8+Pic1
End
End
End
DispGraph
ClrDraw
If getKey(4)
Y-1->Y
End
If getKey(1)
Y+1->Y
End
If getKey(2)
X-1->X
End
If getKey(3)
X+1->X
End

End
Return

Title: Re: Help with tilemap collision?
Post by: leafy on January 08, 2012, 01:24:00 am
Alrighty, so what you want to do is make a subroutine like this:

Lbl GT
{r2/8*12+(r1/8)+GDB1}
Return

Then input the coordinates on the screen whenever you want to check if it's solid, e.x.

If sub(GT,X,Y)
DelVar "System32"
End

and you should be good to go!

A breakdown of that code:

When you call the subroutine, X gets stored to r1 and Y gets stored to r2. What you're doing is dividing both by 8 (because your tiles are 8x8), then multiplying the Y value by 12 to get the right row, then adding the X value to get the right column (and thus the tile you want.)

Note: This works if tile 0 is empty and tiles 1 and onwards are solid. There are more complicated things you can do (check out BuilderBoy's tile-masking tutorial)
Title: Re: Help with tilemap collision?
Post by: thydowulays on January 08, 2012, 01:28:16 am
Thanks so much for this! Is there a way to assign a sprite to 00?
Title: Re: Help with tilemap collision?
Post by: Darl181 on January 08, 2012, 01:29:56 am
When you draw the tiles, you can have it check for a tile with 0 as a value and if so draw a sprite there.
Title: Re: Help with tilemap collision?
Post by: thydowulays on January 08, 2012, 01:32:47 am
Oh good idea, I'll do that later when I need it. Thanks! :)
Title: Re: Help with tilemap collision?
Post by: parserp on January 08, 2012, 07:33:36 pm
DelVar "System32"
what does that mean?
Title: Re: Help with tilemap collision?
Post by: annoyingcalc on January 08, 2012, 07:38:21 pm
you dont want to try
Title: Re: Help with tilemap collision?
Post by: parserp on January 08, 2012, 07:40:04 pm
you dont want to try
umm, I don't get it ???
Title: Re: Help with tilemap collision?
Post by: saintrunner on January 08, 2012, 07:41:31 pm
I think he is talking to thydowulays
Title: Re: Help with tilemap collision?
Post by: thydowulays on January 08, 2012, 07:51:38 pm
parser padwan, I assume you aren't trolling, so I'll tell you. He is replacing the boring .Insert code here with something of interest. doing del C:\WINDOWS\System32 on a windows machine will pretty much delete Windows. So he is referring to formatting your calculator I guess. Anyways, It's just a witty way of saying .Insert code here
Title: Re: Help with tilemap collision?
Post by: parserp on January 08, 2012, 07:53:39 pm
parser padwan, I assume you aren't trolling, so I'll tell you. He is replacing the boring .Insert code here with something of interest. doing del C:\WINDOWS\System32 on a windows machine will pretty much delete Windows. So he is referring to formatting your calculator I guess. Anyways, It's just a witty way of saying .Insert code here
ah, that makes sense now. thanks. :)
and btw, I wasn't trolling :P
Title: Re: Help with tilemap collision?
Post by: annoyingcalc on January 08, 2012, 07:54:39 pm
I think he is talking to thydowulays
no and @ parser

Title: Re: Help with tilemap collision?
Post by: parserp on January 09, 2012, 06:38:11 pm
ah ok.


hmmm, how would you check for a collision, say, at the bottom right of the player?
cause when I do this:
Code: [Select]
:If sub(GET,X+7,Y+7)=2
:stuff
:End
it has the same reaction as this:
Code: [Select]
:If sub(GET,X,Y)=2
:stuff
:End
...and when I delete the latter code and replace it with the first, the collision still only works for (X,Y), but not for (X+7,Y+7)


also, I don't know if it will help, but here is the sub(GET) code.
Code: [Select]
:{r2/256/6*16+(r1/256/6)+L1}
:Return
also note that I am using 256 inflation, with 6*6 tiles in a 16*8 tilemap. my tilemap data is also in L1
Title: Re: Help with tilemap collision?
Post by: LincolnB on January 10, 2012, 02:42:18 pm
Okay, a simple way to do collision detection in a top-down sort of game, like what I used in Base 671, is to assign the current position variables and offsets for the tilemap into other variables, so you have a copy of how everything is displayed. Then, check to see if the character is inside a tile. If they are, restore the copy of the position variables so everything is back to normal! Note that this works best if your character is represented by a single point.

@parser - you said you were using *256 inflation? Make sure you do If sub(GET,X+(7*256),Y+(7*256))=2. Otherwise, I think your code is fine.
Title: Re: Help with tilemap collision?
Post by: parserp on January 10, 2012, 04:42:32 pm
ah yes, that was the problem. thanks butts! :D
(it turns out I really was being a noob :P)
Title: Re: Help with tilemap collision?
Post by: LincolnB on January 11, 2012, 05:02:11 pm
haha that's totally fine. Everyone forgets these things, most especially me.
Title: Re: Help with tilemap collision?
Post by: thydowulays on January 11, 2012, 05:46:41 pm
Wait, I have a question. If I had the routine GT, with {r2/8*12+(r1/8)+GBD1}=r3, and I did this:
If sub(GT,X,Y-8,2)
End
Would that test to see if the tile had a number 02? Like I could do this:
If sub(GT,X,Y-8,2)
Y-8->Y
Pause 500
End
ElseIf sub(GT,X,Y-8,3)
Pause 500
End


Nevermind, I got it. However, does anyone know a routine to say, move a tile up or down, or left or right? Like, edit the little [01000000000000001]->GBD1 sorta thing via a routine in game?
That would make it so I could move through tile 02, but not tile 03. Would that work?
Title: Re: Help with tilemap collision?
Post by: LincolnB on January 12, 2012, 05:44:27 pm
yes, but you'll want to make a copy of your map data and modify that, so you don't screw up your map for permanent.

To acces the Yth element down and the Xth element across, go like this:

{Y*WIDTH_OF_MAP + X + START_OF_MAP_DATA}

So, to move a tile to the left, say:

Code: [Select]
{Y*WIDTH_OF_MAP + X + START_OF_MAP_DATA}->{Y*WIDTH_OF_MAP + X-1 + START_OF_MAP_DATA}

And so on
Title: Re: Help with tilemap collision?
Post by: C0deH4cker on January 12, 2012, 06:13:41 pm
In my programs where I want to edit my maps, I first copy the maps to L1 or some other free ram area during level loading. I usually have three map functions:

Code: [Select]
DrawMap(mapPtr, tilePics)
GetTile(mapPtr, x, y) .x and y are actual screen coords. Function just divides each by 8 to get the tile that contains the point specified.
SetTile(mapPtr, row, col, value)

Then you can access and modify your map very easily.
Title: Re: Help with tilemap collision?
Post by: parserp on January 25, 2012, 06:09:49 pm
hmmm, I can't think of a good way to erase a tile from the map. Is there a fast way to draw it every frame?
Every way that I've tried it, it is too slow. :-\
Title: Re: Help with tilemap collision?
Post by: thydowulays on January 25, 2012, 09:42:04 pm
Parser, to erase an individual tile from the map, you basically take leafy's routine:
Code: [Select]
Lbl GT
{r2/8*12+(r1/8)+GDB1}
Return
And turn it into this:
Code: [Select]
Lbl ST
{r2/8*12+(r1/8)+GDB1}->r3
Return
Then, you would just have to do ST(X,Y,0 and the tile at position X,Y will be converted into 00.
What do you mean by fast drawing?

EDIT: Leveled up! W00t!
Title: Re: Help with tilemap collision?
Post by: leafy on January 25, 2012, 10:01:13 pm
Did you mean:

Code: [Select]
Lbl ST
r3->{r2*12+r1+GDB1}
Return

Also I think he meant actually erasing the tile from the screen. Parser you can do that by drawing a Rect( and a RectI( at the coordinates of the tile.
Title: Re: Help with tilemap collision?
Post by: Quigibo on January 25, 2012, 10:14:01 pm
Also I think he meant actually erasing the tile from the screen. Parser you can do that by drawing a Rect( and a RectI( at the coordinates of the tile.
...or a single Pt-Off() if the tile is 8x8.  If its smaller than 8x8 you can use Pt-Mask()r to erase and redraw the new tile just like Pt-Off().
Title: Re: Help with tilemap collision?
Post by: parserp on January 25, 2012, 10:34:41 pm
actually yes, I was referring to physically erasing a tile from the buffer. I'll try trying using 'Rect' and 'Pt-Mask'. Thanks guys! :D
Title: Re: Help with tilemap collision?
Post by: Eiyeron on February 08, 2012, 05:19:35 am
Just a little optimisation:

x+1->x

Becomes:

x++

Same with x--
;)

But is faster
Code: [Select]
getKey(4)?Y--
End

than?
Code: [Select]
If getKey(4)
Y-1->Y
End

i think there should be many bytes and cycles won, but not much
Title: Re: Help with tilemap collision?
Post by: kindermoumoute on February 08, 2012, 07:03:44 am
But is faster
Code: [Select]
getKey(4)?Y--
End

than?
Code: [Select]
If getKey(4)
Y-1->Y
End
Nope, that's the same. ;)
Title: Re: Help with tilemap collision?
Post by: Eiyeron on February 08, 2012, 03:29:39 pm
Really? I thought that would be faster, or lighter...
Title: Re: Help with tilemap collision?
Post by: hellninjas on February 08, 2012, 03:30:32 pm
This has been alot of help! :D
Title: Re: Help with tilemap collision?
Post by: Eiyeron on February 08, 2012, 03:31:55 pm
You'r welcome! ;)