(Solved)How to determine player or object direction

pigeonaut

Member
Hey everyone!

I was wondering how one could check for the player's facing-direction from a tile collision. Here is what I tried:

I found the variable Object_direction under the "object variables" tab in the project settings. Under the nodes its states this:

Code:
xxxxZyyy

Where xxxx = inertial direction for physics
Where yyy = facing direction for animation
Where z = bit flip for aimed movement.

I tried to code this:
Code:
......
LDA Object_direction,x
AND #%00000100 ;;up direction
BNE +notUpDir
    ;play  a sound
+notUpDir
.......

And this:

Code:
LDA Object_direction,x

AND #%00000100 ;;up direction
     ;play a sound
BNE +notUpDir

But neither of them played a sound when my player was facing the up direction when I collided with my custom tile script. Only when I collided with the tile, while facing down or right, is when the sound triggered.

00000101 was also down or right
00000110 was down only
and 00000000 was any facing direction triggered the tile.
I was not able to figure out any other combination of these last 3 bits that gave me just a single facing direction.


I found out the proper player direction values from this link: https://www.nesmakers.com/index.php?threads/if-left-is-00000110-what-are-the-other-directions.6636/ But none of those gave me the expected output.

1. Do any of you know how to properly check for player direction?
2. In this line: LDA Object_direction,x. What does ",x" do? And I noticed that ",y" and "" could be placed there instead.
3. According to the notes, Object_direction is in the format: xxxxZyyy, where yyy is the facing direction. Is there a way I could only compare for those last 3 bits (5-7) and would that be the solution to my issue?

Thanks for reading! I really appreciate any tips or help.
 

CutterCross

Active member
You're using the incorrect branch instruction to check for the direction. The AND instruction returns a 1 for each compared bit if and only if both corresponding bits are 1.

Code:
LDA #%00110011
AND #%01010101

;;;; result in the accumulator is now #%00010001

You're currently branching if the result of the AND is nonzero, but that's EXACTLY the condition when it's the direction you want! So you're guaranteeing that the sound will never occur during your intended direction. Just flip the branch statement to BEQ and that should solve this issue.

The ,x signifies absolute addressing. Object_direction is a table with all your objects' direction data. To access the direction data for a specific object, you need its object index, which would be stored in the X register from the doHandleObjects loop.

If you need to AND multiple bits and compare for an exact result, simply AND then CMP those bits.

Code:
LDA variable
AND #%00011100
CMP #%00011100
BNE +
    ;; result of the CMP was equal to specified value.
+
 

pigeonaut

Member
You're using the incorrect branch instruction to check for the direction. The AND instruction returns a 1 for each compared bit if and only if both corresponding bits are 1.

Code:
LDA #%00110011
AND #%01010101

;;;; result in the accumulator is now #%00010001

You're currently branching if the result of the AND is nonzero, but that's EXACTLY the condition when it's the direction you want! So you're guaranteeing that the sound will never occur during your intended direction. Just flip the branch statement to BEQ and that should solve this issue.

The ,x signifies absolute addressing. Object_direction is a table with all your objects' direction data. To access the direction data for a specific object, you need its object index, which would be stored in the X register from the doHandleObjects loop.

If you need to AND multiple bits and compare for an exact result, simply AND then CMP those bits.

Code:
LDA variable
AND #%00011100
CMP #%00011100
BNE +
    ;; result of the CMP was equal to specified value.
+
Thanks that worked!!! And thanks for answering my other questions!
 
Top Bottom