Author Topic: Correct way to do smooth scrolling in a tilemapper?  (Read 9501 times)

0 Members and 2 Guests are viewing this topic.

Offline BrownyTCat

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 420
  • Rating: +37/-8
    • View Profile
Correct way to do smooth scrolling in a tilemapper?
« on: December 19, 2012, 04:59:12 pm »
I recently got help with learning to tilemap, but all my attempts to add offsets to the mapper to make "smooth scrolling" (not just "jumping" 8 pixels at a time) have ended in failure or broken collisions. A lot of the tutorials don't look suited to exactly what I'm doing, or have just confused me as a newcomer to this concept. I've tried a LOT of fixes with collisions, but they never worked, so I have to ask anyone with experience for help if you have the time. Thank you.

Source is attached, all one program. Should have comments/labels in it to tell you what some parts are.

P.S. - Are there any IDEs that work with Axe under Windows? The only thing I have is the old TI Data Editor for OSX, and my old G4 absolutely loves crashing.

Offline stevon8ter

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 663
  • Rating: +10/-0
    • View Profile
Re: Correct way to do smooth scrolling in a tilemapper?
« Reply #1 on: December 19, 2012, 05:04:18 pm »
Id noone has solves it , i'll answer tommorow, now i can't do all of it out of my head (i actually can but it would take a long time to type on ipod + i can't see your source)
None of my posts are meant offending... I you feel hurt by one of my posts, tell me ... So i can appoligise me and/or explain why i posted it


Hi there, I'm the allmighty (read as: stupid, weak...) STEVON8TER

Offline BrownyTCat

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 420
  • Rating: +37/-8
    • View Profile
Re: Correct way to do smooth scrolling in a tilemapper?
« Reply #2 on: December 19, 2012, 05:05:13 pm »
Id noone has solves it , i'll answer tommorow, now i can't do all of it out of my head (i actually can but it would take a long time to type on ipod + i can't see your source)

Thanks, I've been trying at this for days so I can wait.

Offline stevon8ter

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 663
  • Rating: +10/-0
    • View Profile
Re: Correct way to do smooth scrolling in a tilemapper?
« Reply #3 on: December 19, 2012, 05:12:03 pm »
Or if you could post the source in CODE block, then i could help now... I'm bored anyway xp
None of my posts are meant offending... I you feel hurt by one of my posts, tell me ... So i can appoligise me and/or explain why i posted it


Hi there, I'm the allmighty (read as: stupid, weak...) STEVON8TER

Offline BrownyTCat

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 420
  • Rating: +37/-8
    • View Profile
Re: Correct way to do smooth scrolling in a tilemapper?
« Reply #4 on: December 19, 2012, 05:17:07 pm »
Or if you could post the source in CODE block, then i could help now... I'm bored anyway xp
Sorry, I'm really not in the mood to boot up my iMac and then restart it 7 times.

Offline stevon8ter

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 663
  • Rating: +10/-0
    • View Profile
Re: Correct way to do smooth scrolling in a tilemapper?
« Reply #5 on: December 19, 2012, 05:18:59 pm »
Ok np, then i'll do it 1) out of my head or 2) tommorow xp


Edit: does the character move and then map scroll, or does the character always stays on the same possition?
« Last Edit: December 19, 2012, 05:30:30 pm by stevon8ter »
None of my posts are meant offending... I you feel hurt by one of my posts, tell me ... So i can appoligise me and/or explain why i posted it


Hi there, I'm the allmighty (read as: stupid, weak...) STEVON8TER

Offline squidgetx

  • Food.
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1881
  • Rating: +503/-17
  • rawr.
    • View Profile
Re: Correct way to do smooth scrolling in a tilemapper?
« Reply #6 on: December 19, 2012, 05:35:06 pm »
There are basically two ways to do it, one using your method where you keep track of the offset and tile separately, or a method where they are combined into one value. I've always used this latter method, which I'll try to briefly explain here.

The only value we'll bother keeping track of is absolute X and Y position, in pixels. If we're using 8x8 tiles, that means the tile coordinate of our point X,Y is the tile point X/8,Y/8. (Remember Axe division throws out the decimal/fractional portion). To find the pixel offset, we'll use the modulus operator ^8. Both of these operations are pretty fast since 8 is a nice power of 2.

Maybe we can make a routine that takes two coordinates for arguments, and returns what tile number that pixel lies in
Code: [Select]
Lbl getTile
{r2/8*tileMapWidth+(r1/8)+pointer}
Return
For optimization's sake, you can leave out the first r2 which is kind of nice. So anyway, now you can check collisions on any pixel-point you want-- for example, checking all 4 corners of a player sprite.
Code: [Select]
getTile(X,Y)+getTile(X+7,Y+7)+getTile(X,Y+7)+getTile(X+7,Y)
would add up the tile# of all 4 corners of your player; this is a nice way to check for collisions if 0 is the only traversable tile.

Now, how do we draw the tilemap with this scheme? It's pretty easy, we can start with pseudocode.
Code: [Select]
For(row,0,8)
For(column,0,12)
Pt-On(column*8-xOffset,row*8-yOffset,{yTile+column*tileMapWidth+xTile+pointer}*8+tileSprites})
End
End


I'll let you fill in the rest.
« Last Edit: December 19, 2012, 05:36:07 pm by squidgetx »

Offline stevon8ter

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 663
  • Rating: +10/-0
    • View Profile
Re: Correct way to do smooth scrolling in a tilemapper?
« Reply #7 on: December 19, 2012, 06:08:34 pm »
I wanted to explain :o xp

Btw: your for loops need to end with 7 and 11.  Not 8 and 12

Edit: nvm i forgot about there have to be drawn 13*9 tiles when scrolling
« Last Edit: December 19, 2012, 06:35:32 pm by stevon8ter »
None of my posts are meant offending... I you feel hurt by one of my posts, tell me ... So i can appoligise me and/or explain why i posted it


Hi there, I'm the allmighty (read as: stupid, weak...) STEVON8TER

Offline stevon8ter

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 663
  • Rating: +10/-0
    • View Profile
Re: Correct way to do smooth scrolling in a tilemapper?
« Reply #8 on: December 19, 2012, 06:43:52 pm »
Sorry for double-post
But i concider this an "update"

This is my way of doing it, it's kinda the same but not as optimised as squidget's code, and it's probably slower with some bugs cause it's out of my head

Code: [Select]
.smtscrll

.tiles (black and white)
[0000000000000000]-> pic0
[FFFFFFFFFFFFFFFF]

.sprite 8*8 stored in pic1

.map (15*10)
[010101010101010101010101010101]->GDB0
[010000000000000000000000000001] .this one 8 times
[010101010101010101010101010101]


.initialize vars (x=x-pos , y=y-pos , c=horizontal scroll, d= vertical scroll)

0->C->D+8->X->Y

.till [clear] is pressed
Repeat Getkey(15)
MOVE()
MAP()
.2 subroutines are called
Pt-on(X,Y,pic1)
DispGraph
End

Lbl MOVE
X+(Getkey(3) and (X<88))-(Getkey(2) and (X>0))->X
C+(Getkey(3) and (X=88))-(Getkey(2) and (X=0))->C

.only change X when it's on screen If it's on border of screen the change the scroll var (same for Y)

Y+(Getkey(1) and (Y<56))-(Getkey(4) and (Y>0))->Y
D+(Getkey(1) and (Y=56))-(Getkey(4) and (Y=0))->D

Return

Lbl MAP

For(A,C/8,C/8+12)
For(B,D/8,D/8+8)
Pt-on(A*8-C,B*8-C,{15*B+A+GDB0}*8+pic0)
.so this says: draw the map, where 15=the length of the map
End
End
Return

.end of program, no collision detection yet but easy to implement... Question? Ask me...

And this doesn't include collision detection yet, but it's easy to implement...

Edit1: forgot to paste code
« Last Edit: December 19, 2012, 07:07:02 pm by stevon8ter »
None of my posts are meant offending... I you feel hurt by one of my posts, tell me ... So i can appoligise me and/or explain why i posted it


Hi there, I'm the allmighty (read as: stupid, weak...) STEVON8TER

Offline BrownyTCat

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 420
  • Rating: +37/-8
    • View Profile
Re: Correct way to do smooth scrolling in a tilemapper?
« Reply #9 on: December 19, 2012, 09:40:37 pm »
And this doesn't include collision detection yet, but it's easy to implement...

Unfortunately, collisions are where I was having trouble. If you look on the old topic, it ended with me posting my attempted smooth scroll version of the mapper (I posted a new topic because the subject itself had changed). Is there something wrong with it?

Offline squidgetx

  • Food.
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1881
  • Rating: +503/-17
  • rawr.
    • View Profile
Re: Correct way to do smooth scrolling in a tilemapper?
« Reply #10 on: December 19, 2012, 10:19:17 pm »
@stevon8er, your method uses a different one than mine :) That code is good if you want the screen to scroll only if the player is near the edges; mine keeps the camera centered on the player. Also my code there is barely optimized lol, though I took the liberty of quick-optimizing yours for you (Didn't go crazy here, tried to keep the same overall layout and readability.)

Code: [Select]
...

0->C->D+8->X->Y

.till [clear] is pressed
While 1
MOVE()
MAP()
.2 subroutines are called
Pt-on(X,Y,pic1)
DispGraphClrDraw
EndIf getKey(15)

Lbl MOVE
getKey(3)-getKey(2)+X
!If +1   //if x = -1
->x   //make x = 0, since the !If +1 statement left a 0 in hl
C--  //decrement C
Else!If -89  //otherwise, check if x is 88 (actually, we're checking if x+1-89=0, but it's the same thing right?)
C++
88->X
End

getKey(1)-getKey(4)+Y
!If +1
->Y
D--
Else!If -57
D++
56->Y
End   //and same stuff for Y. Halved the number of getKeys here.

Return

Lbl MAP

.Okay, so the main driving force behind the optimization logic here is that we want to move as much stuff as we can outside of loops.
.So for example, C and D will never change while we're in the loop so...
.I made up some variable names but you can use whatever you want.
D/8->tileY*15+GDB0->tilePointer
C/8->tileX
-(C^8)-8->offX
-(D^8)->screenY

For(9)  //for the 9 rows to be drawn
tileX->tileX0
offX->screenX
For(A,0,12)
Pt-on(screenX+8->screenX,screenY,{tilePointer+A}*8+pic0)
End
screenY+8->screenY  //move the Y "pen position" down a row
tilePointer+15->tilePointer  //next row of tiles
End
Return
« Last Edit: December 19, 2012, 10:36:13 pm by squidgetx »

Offline stevon8ter

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 663
  • Rating: +10/-0
    • View Profile
Re: Correct way to do smooth scrolling in a tilemapper?
« Reply #11 on: December 20, 2012, 03:36:55 am »
Ok thx squidgetx
I'm rather new to axe so didn't come to much optimizing yet, but thx :p
None of my posts are meant offending... I you feel hurt by one of my posts, tell me ... So i can appoligise me and/or explain why i posted it


Hi there, I'm the allmighty (read as: stupid, weak...) STEVON8TER

Offline BrownyTCat

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 420
  • Rating: +37/-8
    • View Profile
Re: Correct way to do smooth scrolling in a tilemapper?
« Reply #12 on: December 20, 2012, 10:32:58 am »
Now I have to figure out how to get this into a platformer.. named variables in Axe hurt my brain.

Offline Vijfhoek

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 120
  • Rating: +13/-1
    • View Profile
Re: Correct way to do smooth scrolling in a tilemapper?
« Reply #13 on: December 20, 2012, 10:43:28 am »
As an IDE I'd suggest BexIDE. It's somewhere on this forum, just search for it

Offline Hayleia

  • Programming Absol
  • Coder Of Tomorrow
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3367
  • Rating: +393/-7
    • View Profile
Re: Correct way to do smooth scrolling in a tilemapper?
« Reply #14 on: December 20, 2012, 11:37:41 am »
There is also TokenIDE that works great under Windows, that is what I use :)
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

click here to know where you got your last +1s