This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's z80 & ez80 Assembly subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. Z80 & 68k Assembly => z80 & ez80 Assembly
Author Message
MaxVT103


Member


Joined: 24 Aug 2003
Posts: 109

Posted: 17 Dec 2003 04:03:24 pm    Post subject:

Does anyone know of any good vector routines to calculate the vector. I tried to write one and it didn't work. I think the routine would also be extremely slow considering its almost done completely with romcalls. Heres the code if anyone could take a look at it for me.


Code:
;==========================Sin & Cos Calc=====================
;inputs  op1=y
;     op2=x
;     a=magnitude
;     op3=angle
;Math formula   x+sin(b)*m
;output  op1=new y
;     op2=new x
;=============================================================   
sin_cos_calc:
  bcall(_pushrealo1) ;pushes y onto stack    stack order: y
  bcall(_pushrealo2) ;pushes x onto stack    stack order: x,y
  push af
  bcall(_op3toop1)
  bcall(_sin)  ;computes both sin and cos
  bcall(_pushrealo2) ;pushes the cos value onto fp stack  stack order: cos,x,y
  pop af
  ld (hl),a
  bcall(_setxxxxop2)
  bcall(_fpmult) ;multiplies the sin by the magnitude
  ld hl,op1
  bcall(_poprealo2)  ;stack order: x,y
  bcall(_pushrealo1)  ;stack order: m*sin(a),x,y
  bcall(_op2toop1)
  bcall(_setxxxxop2)
  bcall(_fpmult) ;multiplies the cos by the magnitude
  bcall(_op1toop2) ;cos(a)*m
  bcall(_poprealo3) ;op3 now = sin(a)*m;stack order: x,y  
  bcall(_poprealo1) ;op1 now = x  ;stack order: y
  bcall(_pushrealo3)      ;stack order: sin(a)*m,y
  bcall(_fpadd) ;adds x+cos(a)*m    in op1
  bcall(_poprealo2)      ;stack order: y
  bcall(_poprealo3)      ;stack order:
  bcall(_pushrealo1)      ;stack order: new x value
  bcall(_op3toop1) ;brings y to op1
  bcall(_fpadd) ;adds y+sin(a)*m
  bcall(_poprealo2) ;brings new x value back into op2
  ret


Last edited by Guest on 17 Dec 2003 04:04:32 pm; edited 1 time in total
Back to top
tr1p1ea


Elite


Joined: 03 Aug 2003
Posts: 870

Posted: 18 Dec 2003 07:40:55 pm    Post subject:

It would be far easier and HEAPS faster to use pre-calculated trig tables. But then again it would take up a few (by few i mean a lot) bytes of storage Smile.
Back to top
Job the GameQuitter


Member


Joined: 04 Jun 2003
Posts: 102

Posted: 18 Dec 2003 08:48:53 pm    Post subject:

Also, assuming you're only doing this for drawing purposes (and not calculations) the numbers you're using are unnecessarily precise. After all, we're talking 96x64 pixels here, we don't need 9-byte floating point numbers to represent the coordinates! You could increase speed immensely by reducing precision. Two byte fixed point math would be more than sufficient.

I actually wrote a Sine/Cosine routine a while back that used two byte FPM and a lookup table. It used a 256 "degree" scale, because that makes things easier with register overflow and such Smile. The LUT wasn't very big, because the sine function is symmetrical vertically and horizontally; so I could use 1 entry in the LUT for three answers, using some smart pre-calculations.

Sadly the routine is on my laptop, and I'm removed from it. I could look it up tomorrow, if you like. Though I can't garantee it's bug-free, as I never actually used the routine.


Last edited by Guest on 18 Dec 2003 08:49:49 pm; edited 1 time in total
Back to top
MaxVT103


Member


Joined: 24 Aug 2003
Posts: 109

Posted: 18 Dec 2003 09:20:44 pm    Post subject:

Yeah man, please look it up. My old version was fast but it didn't use vectors. I want this new one to for just reasons of game play and such.
Back to top
Job the GameQuitter


Member


Joined: 04 Jun 2003
Posts: 102

Posted: 19 Dec 2003 10:30:09 am    Post subject:


Code:
;  Input:
;   A=angle, with a 256 "degree" scale. If you don't understand that,
;                        maybe this will explain:
;  (A/256) * (2 Pi) in radians, which is
;  (A/256) * 360 in degrees
;  Output:
;   A = Int[Sine(angle)] with amplitude 127
;   (Signed, Two's complement)
;  Destroys:
;   A,C,DE,HL

Cos:
 Add A,64
Sin:
 LD C,A  ;Save A for later
 BIT 6,A;If you look at these instructions closely you'll
 JR NZ,NoSymFix;see that A never contains more than 64 after
 NEG  ;this
NoSymFix:
 AND %01111111
 LD E,A
 LD D,0
 LD HL,SineTable
 ADD HL,DE
 LD A,(HL)
 BIT 7,C
 RET NZ
 NEG
 RET


SineTable:
 .db 0,3,6,9,12,15,18,22,25,28,31,34,37,40,43,46,49
 .db 51,54,57,60,63,65,68,71,73,76,78,81,83,85,88,90
 .db 92,94,96,98,100,102,104,106,107,109,111,112,114,115,116,118
 .db 119,120,121,122,123,123,124,125,125,126,126,127,127,127,127


This should give you an approximation of the (co)sine function. If you need a smaller amplitude in FPM, you could use the following kind of code:

Code:
 LD E,D        ;D=0, remember?
 SRA A \ SRA E          ;Each time you do this, you divide the amplitude in half
 SRA A \ SRA E
 (etc.)
 LD D,A
If you do this, DE will contain the amplitude you require in FPM. You could even make it more complex, by doing things like
Code:
 LD E,D
 SRA A \ SRA E
 SRA A \ SRA E
 SRA A \ SRA E
 LD D,A                    ;DE now has (127/8)*Sine(angle)
 LD HL,$0000
 ADD HL,DE
 ADD HL,DE
 ADD HL,DE
 ADD HL,DE
 ADD HL,DE            ;HL contains (5/8)*127* Sine(angle)
... I know that's obvious, but I point it out anyway. Just to remind you, in case you didn't think of it Smile.

Last edited by Guest on 29 Feb 2004 10:50:43 am; edited 1 time in total
Back to top
MaxVT103


Member


Joined: 24 Aug 2003
Posts: 109

Posted: 19 Dec 2003 01:19:09 pm    Post subject:

I'm sorry to say but I'm uterly confused by that. I'm still a really basic programmer and I don't understand many of the concepts with srl and stuff like that. If someone could elxplain to me stuff like that, and how to use look up tables and sorts. I would really apreciate it. I'm on IM alot so if you guys want to IM me to help me, please do.
Back to top
Job the GameQuitter


Member


Joined: 04 Jun 2003
Posts: 102

Posted: 19 Dec 2003 02:19:24 pm    Post subject:

Rolling Eyes Most of us don't have the time to explain everything that should be explained. Try the pinned tutorial thread; most concepts can be found there.
Back to top
MaxVT103


Member


Joined: 24 Aug 2003
Posts: 109

Posted: 22 Dec 2003 05:08:20 pm    Post subject:

ok, I wrote another vector calculation thingy. But this time I just had it display the cordinates and stuff. but now I get something mad funky come on the screen for the values.


Code:
.nolist
#include "ion.inc"
#include "keys.inc"
#include "array.z80"
player_x = saferam1+1
player_y = saferam1+10
player_a = saferam1+20
player_m = saferam1+30
.list
;-------------Ion Header---------------------
#ifdef TI83P                
    .org    progstart-2
    .db     $BB,$6D
#else
    .org    progstart
#endif
    ret
    jr      nc,BEGIN
    .db     "NHL Hitz Vector 1",0
BEGIN:
  ld a,0
  ld (player_m),a
  ld a,20
  ld (player_y),a
  ld (player_x),a
  ld hl,90
  ld (player_a),hl
keys:
  bcall(_grbufclr)
  call decplayerm
  ld a,0FFh      
  out (1),a
  ld a,$fd        
  out (1),a
  in a,(1)
  cp kclear
  jp z,exitgame
  ld a,$FE
  out (1),a
  in a,(1)
  cp kup
  call z,player_up
  cp kdown
  call z,player_down
  cp kright
  call z,player_right
  cp kleft
  call z,player_left
  jp screen_update

player_up:
  ld (player_a),hl
  ld hl,90
  ld (player_a),hl
  ld a,(player_m)
  inc a
  ld (player_m),a
  ret
player_down:
  ld (player_a),hl
  ld hl,270
  ld (player_a),hl
  ld a,(player_m)
  inc a
  ld (player_m),a
  ret
player_left:
  ld (player_a),hl
  inc hl
  ld (player_a),hl
  ld a,(player_m)
  inc a
  ld (player_m),a
  ret
player_right:
  ld (player_a),hl
  dec hl
  ld (player_a),hl
  ld a,(player_m)
  inc a
  ld (player_m),a
  ret   
decplayerm:
  ld a,(player_m)
  dec a
  cp 0
  call z,magnitude_fix
  call m,magnitude_fix
  ld (player_m),a
  ret
magnitude_fix:
  ld a,0
  ret
screen_update:
  ld hl,(player_x)
  ld b,h
  ld c,l
  ld hl,(player_y)
  ld d,h
  ld e,l
  ld hl,(player_m)
  ld a,1
  call pos_calc
  ld (player_x),bc
  ld (player_Y),de
  bcall(_grbufclr)
  ld a,1
  ld (pencol),a
  ld (penrow),a
  ld hl,x_text
  bcall(_vputs)
  ld a,25
  ld (pencol),a
  ld a,1
  ld (penrow),a
  ld hl,(player_x)
  ld de,op1
  ld bc,9
  ldir
  ld a,8
  bcall(_dispop1a)
  ld a,7
  ld (penrow),a
  ld a,1
  ld (pencol),a
  ld hl,y_text
  bcall(_vputs)
  ld a,25
  ld (pencol),a
  ld a,7
  ld (penrow),a
  ld hl,(player_y)
  ld de,op1
  ld bc,9
  ldir
  ld a,8
  bcall(_dispop1a)
  ld a,14
  ld (penrow),a
  ld a,
  ld (pencol),a
  ld hl,angle_txt
  bcall(_vputs)
  ld a,25
  ld (pencol),a
  ld a,14
  ld (penrow),a
  ld hl,(player_a)
  ld de,op1
  ld bc,9
  ldir
  ld a,8
  bcall(_dispop1a)
  jp keys
   
exitgame:
  bcall(_grbufclr)
  ret
   


x_text:
  .db "X-Cor:",0
y_text:
  .db "Y-cor:",0
angle_txt:
  .db "Angle:",0
;=============Vector Calculation routine===============
;inputs   hl=angle
;  a=magnitude
;
;outputs bc=value to add to x
;   de=value to add to y
vector_calc:
   
;loads the angle into hl
  push af
  ld de,op1
  ld bc,8
  ldir
   
;gets the sin and cos of the angle
  bcall(_Sin)
  bcall(_int)  ;rounds off the next value into an integer, this is the sin value
  ld hl,op1
  push hl  ;stack: hl(sin),af
  ld hl,op2  ;puts op2 into op1
  ld de,op1
  ld bc,8
  ldir
  bcall(_int)  ;rounds off the next value into an integer, this is the cos value
  ld hl,op1   ;loads the op1 into hl
  ld b,h
  ld c,l  ;loads the cos value into bc
  pop hl  ;stack: af
  ld d,h
  ld e,l  ;loads the sin value into de
   
;multiply it by the magnitude
  pop af
  ld h,b
  ld l,c
  ld b,a
magnitude_1:
  add hl,hl
  djnz magnitude_1
  ld b,h
  ld c,l
  push bc;stack: bc
  ld h,d
  ld l,e
  ld b,a
magnitude_2:
  add hl,hl
  djnz magnitude_1
  ld d,h
  ld e,l
  pop bc
  ret
;==============end of the vector calculation============


;==============Add Vector to current position x
;inputs hl=current x position
;  bc=value to add to x
;outputs hl,new x position
x_vec_add:
  add hl,bc
  ret
;==============Add Vector to current position y
;inputs hl=current y position
;  de=value to add to y
;outputs hl,new x position
y_vec_add:
  add hl,de
  ret
   
;================Complete position calculation===========
;inputs bc,current x
;  de,current y
;  hl,angle
;  a,magnitude
;outputs bc,new x position
;   de,new y position
pos_calc:
  push de  ;stack: de
  push bc  ;stack: bc,de
  call vector_calc
  pop hl  ;pops the current xpos to hl, stack: de
  call x_vec_add
  ld b,h
  ld c,l
  pop hl  ;pops the current ypos to hl, stack: -empty-
  call y_vec_add
  ld d,h
  ld e,l
  ret

.end
END


Last edited by Guest on 22 Dec 2003 05:14:59 pm; edited 1 time in total
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are UTC - 5 Hours

 

Advertisement