Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in 
CSDb User Forums


Forums > C64 Coding > Simple "look at" function, avoiding atan2
2018-06-26 11:39
jamiefuller

Registered: Mar 2018
Posts: 23
Simple "look at" function, avoiding atan2

Hi All,

I am writing a game that requires the player character to aim his gun at a movable point around the screen, I would usually calculate the angle using atan2 but don't want the overhead of a fully blown atan2 routine ( http://codebase64.org/doku.php?id=base:8bit_atan2_8-bit_angle )

The player can be any random x,y and the crosshaor can be any other random x,y

The gun sprite only needs to point in one of 8 directions so I can't help think that it must be easier to calculate/table. but I'm drawing a major blank :(

Any pointers or suggestions would be much appreciated.

Thanks
 
... 2 posts hidden. Click here to view all posts....
 
2018-06-26 12:14
jamiefuller

Registered: Mar 2018
Posts: 23
thanks so much JackAsser, That gives me some info to work through.

The only issue I can think of is that the octants are slightly offset to that,

see this image, excuse my terrible scribble :)
2018-06-26 12:22
ChristopherJam

Registered: Aug 2004
Posts: 867
Just set

x'=x+y/2
y'=y-x/2

before using Jackie's method. Your thresholds will be 4° off, but I don't think that'll be noticeable.

(or, (x*2.5+y, y*2.5-x) if you want to just be 0.7° out)
2018-06-26 15:38
JackAsser

Registered: Jun 2002
Posts: 1472
CJam applies a simple rotation matrix. Ideally you want to mul by sin(0.5) but sin(0.5) ~= 0.5
2018-06-26 16:20
jamiefuller

Registered: Mar 2018
Posts: 23
Damn you guys are good!, I'm still struggling to get my head around the first reply :)

Thanks to you both.
2018-06-27 00:28
Martin Piper

Registered: Nov 2007
Posts: 393
Calculate dx and dy. Shift the values, or lookup, so they're both fitting in 4 bits. You can vary the shift to maintain nearby accuracy or fit far away tests as long as you shift the same for both values.

Combine them into a 8 bit value.
Use that to index into a direction table.

The final lookup gives you the ability to finely tweak the direction sprite for whatever skewed quadrants you want.
2018-06-28 11:39
jamiefuller

Registered: Mar 2018
Posts: 23
Thanks everyone for your ideas, honestly some of them were well over my head but certainly put me on the right path.

This is the code I ended up with, which works well for my case needs. it doesn't compensate for my "offset" yet but it still looks pretty good and I may just skip that bit.

Huge thanks again everyone!

pointgun
        lda x1
        sec
        sbc x2
        sta dx
        bcs @at10
        eor #$ff
@at10   
        sta absdx
        rol octant

        lda y1
        sec             ; this bit just compensates for different starting co-ordinates
        sbc #$0c        ; this bit just compensates for different starting co-ordinates
        lsr            ; this bit just compensates for different starting co-ordinates
        
        sec
        sbc y2
        sta dy
        bcs @at20
        eor #$ff
@at20
        sta absdy
        rol octant
        sec
        sbc absdx
        rol octant
        lda octant
        and #$07
        tax
        lda guntab,x
        sta 2046        ;store anim frame in sprite
        rts

guntab  BYTE $8E,$8F,$8D,$8C,$91,$90,$92,$93



x1,y1,x2,y2,dx,dy,absdx and absdy and octant are all just zeropage variables.
2018-06-28 12:16
ChristopherJam

Registered: Aug 2004
Posts: 867
Nice.

I was thinking an alternate way to solve the offsets problem is to just have your artwork pointing the gun NNE, ENE, ESE, SSE etc :)

(oh, and warning - your "this bit" section is subtracting y2 from half of y1 at the moment; you'll probably want to make a halved version of y2 to subtract instead if you keep that)
2018-06-28 12:25
jamiefuller

Registered: Mar 2018
Posts: 23
Quote: Nice.

I was thinking an alternate way to solve the offsets problem is to just have your artwork pointing the gun NNE, ENE, ESE, SSE etc :)

(oh, and warning - your "this bit" section is subtracting y2 from half of y1 at the moment; you'll probably want to make a halved version of y2 to subtract instead if you keep that)


Yeah the "this bit" section adjusts for the fact that y1 and y2 have very differing co-ordinate systems in my game, y2 only has 2 pixel resolution so to speak, so is multiplied by 2 when drawing the sprite later on :)

I also just noticed that the code above can be optimised quite a bit by removing the "sta dx", "sta dy" and the "sta absdy" as none of them are used for anything else later on.

:)
2018-06-28 12:37
TWW

Registered: Jul 2009
Posts: 444
In which case you only need to LDA X1 CMP X2
2018-06-28 12:47
jamiefuller

Registered: Mar 2018
Posts: 23
Quote: In which case you only need to LDA X1 CMP X2

Good point, and I had to double check my code to make sure :)

but in this case while I am not using dx later, I am using dx to calculate absdx, so I still need to do the subtraction to calculate dx, just not store it anywhere, only store absdx.

But I think I could probably do that for the last subtraction
        sec
        sbc absdx

which calculates absdy-absdx and only uses the sign of the output. I'll give it a try :)
Previous - 1 | 2 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
CA$H/TRiAD
Dymo/G★P
Cruzer/CML^Oxy^Λrse..
Guests online: 27
Top Demos
1 Uncensored  (9.7)
2 Comaland 100%  (9.7)
3 Unboxed  (9.7)
4 Edge of Disgrace  (9.7)
5 Coma Light 13  (9.6)
6 The Shores of Reflec..  (9.6)
7 Lunatico  (9.6)
8 Wonderland XII  (9.6)
9 Old Men in Used Cars  (9.5)
10 C=Bit 18  (9.5)
Top onefile Demos
1 Arok 20 Invitation  (9.6)
2 Daah, Those Acid Pil..  (9.5)
3 FMX Music Demo  (9.5)
4 Crystal Gazer  (9.5)
5 Pandemoniac Part 2 o..  (9.5)
6 Rewind  (9.5)
7 Dawnfall V1.1  (9.5)
8 Party Horse  (9.4)
9 Dawnfall  (9.4)
10 In Memoriam BHF  (9.4)
Top Groups
1 Oxyron  (9.4)
2 Booze Design  (9.4)
3 Censor Design  (9.4)
4 Finnish Gold  (9.3)
5 The Transfer Team  (9.3)
Top Mega Swappers
1 Aslive  (9.4)
2 R.C.S.  (9.3)
3 Dishy  (9.2)
4 Nightshade  (9.1)
5 Calypso  (9.0)

Home - Disclaimer
Copyright © No Name 2001-2018
Page generated in: 0.054 sec.