Close

Asteroids #4 - Ship control, lasers and collisions

A project log for Merlin

68000 Retro Super Computer

matthew-pearceMatthew Pearce 02/15/2022 at 15:350 Comments

So we have a ship rotation using keyboard controls. I just rotated the vectors in excel and added them to an array, the same with the thruster vectors which are drawn twice with additive blending to increase brightness. The bullet is a simple point in the direction of the ship nose. Collision detection is just using 2 circular boundaries. 

The vectors are rotated using: 

𝑥2=cos𝛽𝑥1−sin𝛽𝑦1 

𝑦2=sin𝛽𝑥1+cos𝛽𝑦1

where 𝛽 is the rotation angle in radians.

collision detection is :

asteroidX-shipX < shipBoundaryRadius + asteroidBoundaryRadius &&
asteroidY-shipY < shipBoundaryRadius + asteroidBoundaryRadius

This is translated into asm:

*****************************************************
* asteroid collisions
*
ast_collision:

        *a4x a5y - asteroid

        movem.l d0-d7/a0-a6,-(sp)        ; backup registers

        lea ship_x,a0
        lea ship_y,a1

        move.l #0,d0
        move.l #0,d1
        move.l #0,d2

        move.w (a0),d0
        move.w (a1),d1
        move.w (a4),d2
        move.w (a5),d3

        move.l #20,d4
        add #8,d4 

        sub.w d0,d2
        sub.w d1,d3

        cmp.w #0,d2
        bge chky 

        muls.w #-1,d2

chky:
        cmp.w #0,d3
        bge chkcoll 

        muls.w #-1,d3

chkcoll:
        cmp.w d2,d4
        blt endcoll

        cmp.w d3,d4
        blt endcoll

        sub.b #1,ship_lives   * 1 life down

        cmp #0,ship_lives * end!
        beq end

        movem.l (sp)+,d0-d7/a0-a6         ; restore registers
        bra restart

endcoll:
        movem.l (sp)+,d0-d7/a0-a6         ; restore registers
        rts

Next on the list is collisions between the laser and asteroids.

As usual code is here:

https://github.com/mattuna15/merlin-software/blob/master/asteroids/asteroids.s

Discussions