Page 1 of 1

Extract tile position

Posted: Sat Feb 21, 2015 5:03 pm
by JoeGtake2
Alright, so I have 16x16px tiles with some metadata stored for collision and tile type in a lookup table. All is working great...no problems.

For one of the tile types, I need to gauge where the player is in relation to the tile itself, and I can't seem to extract the tile position in pixels for that comparison.

For instance, I use this subroutine to get the 'tile number' in the table:

Code: Select all


GetTile:
    LDA tempy
    AND #%11110000
    STA temp
    LDA tempx
    LSR A
    LSR A
    LSR A
    LSR A
    ORA temp
    TAY
    RTS

This gives me the tile that is lined up on the 16x16 grid based on a plugged in point in pixels.

Now for a slightly different purpose, I need to reverse that to find the x of the left of the tile in pixels and the y of the top of the tile in pixels so I can determine a point's position in relation to that corner point of a particular tile. Should be pretty simple, but can't get it working.

Any suggestions? Thanks!


*EDIT* My concept was simple - something like :

Code: Select all


    LDA tempx
    AND #%11110000
    STA tempx

    LDA tempy
    AND #%11110000
    STA tempy

...but it didn't work as expected. Does that seem a reasonable method to get these values? Should I be looking for the gremlin somewhere else in the routine? Thanks!

Re: Extract tile position

Posted: Sat Feb 21, 2015 6:18 pm
by tokumaru

Code: Select all

and #%00001111
Should give you the pixel offset within the metatile.

Re: Extract tile position

Posted: Sat Feb 21, 2015 6:58 pm
by JoeGtake2
Hm - yeah, that could be a good shortcut, I think. I used a similar method for something else, dunno why I didn't consider it in this instance.

Thanks!

Re: Extract tile position

Posted: Sat Feb 28, 2015 3:10 am
by Prime
Alot of older games would use
Rampage,Karnov

Code: Select all

 lda _XObjectTmp
 lsr
 lsr
 lsr
 lsr
 and #%00001111    ;Karnov
 sta _XTmp
 lda _YObjectTmp
 lsr
 lsr
 lsr
 lsr
 asl
 asl
 asl
 asl
 ora _Xtmp
 tay
 lda _MapCollisiondata,y
 rts