AI aiming

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
Celius
Posts: 2158
Joined: Sun Jun 05, 2005 2:04 pm
Location: Minneapolis, Minnesota, United States
Contact:

AI aiming

Post by Celius »

Okay, I'm currently designing a short little game where you are a flying saucer, and you are flying around trying to shoot this guy below to get points. So, what is hard about this is that there are two cannons trying to shoot you down. Okay, I am wondering how to do this. I was trying to find out a way to say increase cannon balls x pos and y pos according to saucer's x and y pos. I know how to compare the x and y poses, but I don't know how to say the "increase according to" part of it. Any suggestions?
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples »

Have you heard of fixed-point math?

For each shot, store the displacement vector and the velocity vector. Every frame, add the velocity vector to the displacement vector.
User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg »

I think the problem is determining the displacement vector in the first place, having it go the proper angle towards the player (and then to add the "I" of AI, to have it collide with the player will be if he doesn't change his course).
Guest

Post by Guest »

i was wondering same thing. but what do you mean by displacment vector?
User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg »

The vector here is a pair of values. The displacement is the movement of the object. Thus, a displacement vector tells how far to move the object in the X and Y direction each time unit.
Celius
Posts: 2158
Joined: Sun Jun 05, 2005 2:04 pm
Location: Minneapolis, Minnesota, United States
Contact:

Post by Celius »

Yeah, how would you even detirmene the displacement vector in the first place? This is what I was confused about. I'm going to brain storm here:

Code: Select all

;ay = y coord of the upper left tile of the 2x2 saucer sprite
;ax = x coord of the upper left tile of the 2x2 saucer sprite
;gy = y coord of the cannon
;gx = x coord of the cannon
;iy = y coord of the cannon ball
;ix = x coord of the cannon ball
;yd = y distance
;xd = x distance
;yd1
;xd1

stuff:
     sec
     lda gy        ;since y coord increases as it goes down the screen
     sbc ay       ;subtract ay from gy
     sta yd
     sec
     lda ax      ;since it's the other way for x coord
     sbc gx     ;subtract gy from ay
     sta xd

stuffr:
     lda yd
    and #$07
     sta yd1
     lda xd
    and #$07
     sta xd1
stuffe:
     ldx yd1
strwr:
     inc iy
     dex
     bne strwr
stufft:
     ldx xd1
sarst:
     inc ix
     dex
     bne sarst
I just randomly named lables, by the way. Okay, do you think that would work? Is there anything wrong?
Celius
Posts: 2158
Joined: Sun Jun 05, 2005 2:04 pm
Location: Minneapolis, Minnesota, United States
Contact:

Post by Celius »

Okay, I'm a little stuck on one thing. Here is my current idea:

Code: Select all

stuffr:
     	sec
     	lda gy
     	sbc ay
     	sta yd
     	sec
     	lda ax
     	sbc gx
     	sta xd
	clc
	rts
Okay, I have that. ax and ay are sprite vars, gx and gy are sprite vars for the cannon, and xd and yd stand for x distance to the cannon and y distance to the cannon. Okay, what I'm trying to do is this:

Code: Select all

stuff:
      load xd1
      cmp #0
      beq keepgoing
      jmp morestuff

keepgoing:
      load xd
      divide by yd
      store in xd1

morestuff:
      inc ix ; cannon ball
      dec xd1
      bne return
      dec iy
      rts
return:
      rts

I think that code looks like it'd work. The logic is simple. If xd= 10 and yd= 2, you want to divide xd(10) by yd(2) and ix will increase by 5, then iy will decrease by 1! But of course, my problem is I don't know how to divide xd by yd! Any suggestions? And does this code look good?
User avatar
Bregalad
Posts: 8056
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Post by Bregalad »

Yeah, I also have a lot of trouble for division. Some CPUs have a DIV instruction, but the 6502 doesn't, and doing it via a binary way is often glitchy/incomplete.
A simple way to do it is :

Code: Select all

ldy #$00
lda Divisor
- cmp Dividend
bcc +
sbc Dividend
iny
bne -
+
You exit with A=rest and Y=quotient. This way have no flaws, it even hold divison by zero, where Y will stay at zero and A will keep the rest, but it will cycle 256 times while doing stupid substractions by zero. That's the only problem, it will be way to slow with low dividends. There exists other methods, but I'm unable to get how they works, and they often doesn't work very accuratly.
Useless, lumbering half-wits don't scare us.
Post Reply