(4.5.6) MoveToPlayer Monster Movement Speed

Craigery

Active member
The Monster Object AI script MoveTowardsPlayer uses a different speed than the one assigned under Object Details. I want to speed up the movement, because currently its too slow for my game.

As I understand it, having a Monster move toward the player involves 3 Scripts:
MoveTowardsPlayer.asm is the AI script
MoveTowardsPoint.asm is the macro from line 22 of the previous ASM
doMoveTowardsPoint.asm is the macro from line 22 of the previous ASM (coincidence??)

In doMoveTowardsPoint.asm you get tempA and tempB
TAY
LDA AngleToHVelLo,y
sec
sbc #$80
STA tempA
LDA AngleToVVelLo,y
CLC
ADC #$80
STA tempB

which are used in MoveTowardsPoint.asm as:
LDA tempA
STA Object_h_speed_lo,x
LDA #$00
STA Object_h_speed_hi,x
LDA tempB
STA Object_v_speed_lo,x

So its storing the H and V angle as the objects speed? If I increase either Object H or V speed variable it moves faster, but towards a different point. What is another way around this? I suppose I could offset the position it looks for to move to, but I wonder if someone else has a fix for this, so I can change the speed without changing the trajectory?
 
Last edited:

dale_coop

Moderator
Staff member
If you followed the shooter tutorial, you will see that the monster shoots projectile at player, and the projectile speed can be set. And it works.
In fact, in that module, it uses another settings for that "shoot at player" thing.

The Monster Object action is set to "Shoot At Player". Here the corresponding ShootAtPlayer.asm script (also check my comment in the script):
Code:
   LDA Object_x_hi,x
   ;; plus x offset
   STA tempA
   LDA Object_y_hi,x
   ;; plus y offset
   STA tempB
   LDA #$02 ;; <-- dale_coop: HERE !!! your projectile object to create
   STA tempC
   LDA #$00 ;; action step for that object
   STA tempD
   LDA Object_screen,x
   STA tempz
 
   CreateObjectOnScreen tempA, tempB, tempC, tempD, tempz

And the Projectile object itself has an Action Step 0 set to "Move Toward Player":
2020-12-08-12-25-39-Monster-Animation-Info.png


This way, the speed and acceleration of the projectile object is not ignored.
 

9Panzer

Well-known member
Did this work for you @Craigery ? I've been pulling my hair out for weeks and nothing I do seems to get it right. I did have a hailstorm of bullets once... but that wasn't what I was after lol.
 

Craigery

Active member
Did this work for you @Craigery ? I've been pulling my hair out for weeks and nothing I do seems to get it right. I did have a hailstorm of bullets once... but that wasn't what I was after lol.
Not specifically, no. Somewhere, some seemingly unrelated code was messing it up unintentionally. I ended up restarting it as a new project in a fresh download of NESMaker and reimplementing doing all my custom ASM one piece at a time and it worked that time around. So no clue what I had done wrong. Sorry that's not helpful.
 

5kids2feed

Well-known member
Anybody have a luck with this.. move towards player moves at a snails pace.. but as soon as i replace the script with move right or left it goes at the actual speed you have it at.. here's the code for move towards player that I have.. I waving the dale symbol in hopes he helps @dale_coop :ROFLMAO: 🤞

Code:
    TYA
    PHA
    TXA
    PHA
    STA tempx
   
        LDA Object_x_hi,x
        STA tempA
        LDA Object_y_hi,x
        STA tempB
        LDX player1_object
        LDA Object_x_hi,x
        STA tempC
        LDA Object_y_hi,x
        STA tempD
       
        LDX tempx
       
    MoveTowardsPoint tempA, tempC, tempB, tempD
    LDA Object_direction,x
    AND #%00000111
    ORA #%00001000
    STA Object_direction,x
   
    PLA
    TAX
    PLA
    TAY
 

CluckFox

Active member
MoveTowardsPoint only sets up Object_h_speed_lo and Object_v_speed_lo. Try setting the corresponding _hi variables to the same number between 0 and F
(edited for accuracy)
 

5kids2feed

Well-known member
MoveTowardsPoint only sets up Object_h_speed_lo and Object_v_speed_lo. Try setting the corresponding _hi variables to the same number between 0 and F
(edited for accuracy)

Ooo thank you! Anyway you can give me an example of how to write it our for that code?
 

CluckFox

Active member
Ooo thank you! Anyway you can give me an example of how to write it our for that code?
Here is your code, modified:

Code:
    TYA
    PHA
    TXA
    PHA
    STA tempx
   
        LDA Object_x_hi,x
        STA tempA
        LDA Object_y_hi,x
        STA tempB
        LDX player1_object
        LDA Object_x_hi,x
        STA tempC
        LDA Object_y_hi,x
        STA tempD
       
        LDX tempx
       
    MoveTowardsPoint tempA, tempC, tempB, tempD
    LDA Object_direction,x
    AND #%00000111
    ORA #%00001000
    STA Object_direction,x
    ;; At this point, Object_h_speed_lo and Object_v_speed_lo are set at a 
    ;; ratio that will move the monster towards the player's direction. Aimed
    ;; physics are enabled, but the hi speed values are still 0
    FUDGE_FACTOR=$01
    LDA #FUDGE_FACTOR
    STA Object_h_speed_hi,x
    STA Object_v_speed_hi,x
    ;; Now, the monster will move much faster. Ideally the speed_hi values should
    ;; be similarly proportioned to the speed_lo values, but this approach will get
    ;; the monsters moving at more than a snail's pace.
    PLA
    TAX
    PLA
    TAY
 

5kids2feed

Well-known member
Fudge factor 🤣🤣

Thanks so much, I will try this out Monday when I’m around my computer! You’re the best!
 

5kids2feed

Well-known member
Works like a charm!! Must faster! Thanks man! Only problem is it only moves in diagonals.. hmm.. gotta see what i messed up now haha. Any idea if there's something in the code listed that only make it diagonal? With my original code I posted it moved any direction I needed it to. With the updated one it's only diagonals. Bizzare!
 
Last edited:

CluckFox

Active member
Works like a charm!! Must faster! Thanks man! Only problem is it only moves in diagonals.. hmm.. gotta see what i messed up now haha. Any idea if there's something in the code listed that only make it diagonal? With my original code I posted it moved any direction I needed it to. With the updated one it's only diagonals. Bizzare!
Its the (my) code - setting both H and V hi speeds to the same number will cause diagonal motion. I can modify it today to suit your purpose though.
 

5kids2feed

Well-known member
Its the (my) code - setting both H and V hi speeds to the same number will cause diagonal motion. I can modify it today to suit your purpose though.
Yes, please. If you wouldn’t mind. That’d really help me out and help me analyze what you did to change it.
 

5kids2feed

Well-known member
Is this more of what you had in mind?
NRiZKyZ.gif
Just as long as it goes every angle like the original code, definitely! Everything was good about the original code except it moved at snail's pace.

In my game, you have a cursor.. and when you press a button..it shoot towards the cursor.. wherever it may be on the screen
 

CluckFox

Active member
Here's the updated AI code to try:

Code:
TYA
    PHA
    TXA
    PHA
    STA tempx
   
        LDA Object_x_hi,x
        STA tempA
        LDA Object_y_hi,x
        STA tempB
        LDX player1_object
        LDA Object_x_hi,x
        STA tempC
        LDA Object_y_hi,x
        STA tempD
       
        LDX tempx
       
    MoveTowardsPoint tempA, tempC, tempB, tempD
    LDA Object_direction,x
    AND #%00000111
    ORA #%00001000
    STA Object_direction,x
    ;; At this point, Object_h_speed_lo and Object_v_speed_lo are set at a 
    ;; ratio that will move the monster towards the player's direction. Aimed
    ;; physics are enabled, but the hi speed values are still 0
    FUDGE_FACTOR=$02

    ;; the lo byte is signed. If its absolute value is over a certain value
    ;; "round" it up by adding FUDGE_FACTOR to the corresponding hi byte
    LDA Object_v_speed_lo,x
    BPL +absoluteValue
    EOR #$ff
    
    +absoluteValue
    AND #%01000000       
    BEQ +slowVertical
    LDA #FUDGE_FACTOR
    STA Object_v_speed_hi,x
    
    +slowVertical
    LDA Object_h_speed_lo,x
    BPL +absoluteValue
    EOR #$ff
    
    +absoluteValue
    AND #%01000000
    BEQ +slowHorizontal
    LDA #FUDGE_FACTOR
    STA Object_h_speed_hi,x

    +slowHorizontal
    PLA
    TAX
    PLA
    TAY
 

5kids2feed

Well-known member
Almost! Better than it was! Moves up, down, left, right, and diagonal which is great.. but doesn't move every single angle on diagonal like original code. Thanks so much for putting so much effort into it though. Is there a way to get it to move all angles instead of simple angles?

(Should be going everywhere the cursor goes in this gif.. graphics aren't finished yet.. don't mid the basicness of it)

Animation.gif
 
Top Bottom