## Rotating some points , seems slow

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

### Rotating some points , seems slow

I was going to make a vector graphics game like asteroids but different
I rotated some points around an origin but even that seems too slow

I was going to draw lines between the points

Code:
`#include <FSin.bas>Dim trishipx(12) as UByteDim trishipy(12) as UByteDim drawtrishipx(12) as UBytedim drawtrishipy(12) as UBytetrishipx(0)=62trishipy(0)=58trishipx(1)=58trishipy(1)=58trishipx(2)=58trishipy(2)=62trishipx(3)=58trishipy(3)=54trishipx(4)=60trishipy(4)=58trishipx(5)=60trishipy(5)=62Dim trishipangle as IntegerFunction rotate(degrees as Integer) as Integer dim transformedX as Ubyte=0 dim transformedY as ubyte=0dim pointx as Ubyte=54dim pointy as Ubyte=54  for  x=0 to 5'Local theta:Float=1transformedX= fCos(degrees) * (trishipx(x)-pointx) - fSin(degrees) * (trishipy(x)-pointy) + pointxtransformedY = fSin(degrees) * (trishipx(x)-pointx) + fCos(degrees) * (trishipy(x)-pointy) + pointy drawtrishipx(x) = transformedX drawtrishipy(x) = transformedYNextreturn 1End FunctionFunction drawship() as Integerfor x=0 to 5plotPoint(drawtrishipx(x),drawtrishipy(x))nextreturn 1End FunctionWhile 1trishipangle=trishipangle+1if trishipangle>360 thentrishipangle=0end ifrotate(trishipangle)drawship()WendSUB plotPoint (x as uByte, y as uByte)ASMld d,(IX+5) ;'Xld e,(IX+7) ;'Y    ld a, 191   sub e      ret c      ld e, a      and a          rra         scf         rra         and a      rra         xor e            and 248   xor e   ld h, a   ld a, d   rlca   rlca   rlca   xor e   and 199   xor e   rlca   rlca   ld l, a       ld a, d   and 7   ld b, a   inc b   ld a, 1       plotPoint_loop:        rrca   djnz plotPoint_loop      ;cpl    ld b, a      ld a, (hl)   or b      ld (hl), a   END ASMEND SUB`

Site Admin

Posts: 1521

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

### Re: Rotating some points , seems slow

slenkar wrote:I was going to make a vector graphics game like asteroids but different
I rotated some points around an origin but even that seems too slow

I was going to draw lines between the points

There is little you can do in your code, I guess. Some tips:

1. Not a huge optimization, but more elegant: initialize the trishipx array directly
2. Most of the demos I know cache all pre-calculated values in a table. So do something like DIM fSIN(360) As Fixed might be a good idea.
3. The above might be even faster if you use PEEK instead of array access.
4. The lines in the fsin / fcos degree calculates twice these values.

For the last point, consider:
Code:
`transformedX= fCos(degrees) * (trishipx(x)-pointx) - fSin(degrees) * (trishipy(x)-pointy) + pointxtransformedY = fSin(degrees) * (trishipx(x)-pointx) + fCos(degrees) * (trishipy(x)-pointy) + pointy`

To be rewritten as
Code:
`Dim fc, fs, tx, ty As Fixedfc = fCos(degrees)fs = fSin(degrees)tx = trishipx(x) - pointxty = trishipy(x) - pointytransformedX= fc * tx - fs * ty + pointxtransformedY = fs * tx + fc * ty + pointy`

And remember that variables declared (DIMed) within a function have slower access. This is because they are relative to the stack pointer (IX). Global variables are much faster. At the moment the compiler does not support static variables.

Maybe Britlion can give you more suggestions, as he is an optimization expert

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

### Re: Rotating some points , seems slow

thanks I put fsin and fcos into arrays and now a circle completes in a few seconds as opposed to a few minutes

Posts: 790

Joined: Mon Apr 27, 2009 7:26 pm

Location: Slough, Berkshire, UK

### Re: Rotating some points , seems slow

slenkar wrote:thanks I put fsin and fcos into arrays and now a circle completes in a few seconds as opposed to a few minutes

Wow. Really?

I mean...that's basically how Fsin works in the first place. I'm surprised it's possible to optimize it that massively!

(You are using http://www.boriel.com/wiki/en/index.php ... C:FSin.bas yes?)

Site Admin

Posts: 1521

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

### Re: Rotating some points , seems slow

Awesome

BTW I implemented CIRCLE using bresenham's algorithm. It does not use neither SIN nor COS but integer aritmethic (Try CIRCLE command).
Maybe you could use that to rotate your triangle in some way

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

### Re: Rotating some points , seems slow

yeah im using that FSin cos it seemed to be the fastest(?)

here is a little demo of the dots being rotated, it makes a nice picture too

http://www.4shared.com/file/VKojb8gm/rotate.html
there is a delay at the beginning while the FSin values are calculated

interesting point about the circle command...

Site Admin

Posts: 1521

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

### Re: Rotating some points , seems slow

slenkar wrote:yeah im using that FSin cos it seemed to be the fastest(?)

here is a little demo of the dots being rotated, it makes a nice picture too

http://www.4shared.com/file/VKojb8gm/rotate.html
there is a delay at the beginning while the FSin values are calculated
interesting point about the circle command...

Keep in mind that Circle only computes 1/4 of the circle. The other 3 quarters are just calculated by reflection
For drawing circles, I think you should definitely use CIRCLE (both CIRCLE & DRAW are heavily optimized, they don't use the ROM routines at all - ok, just bragging, but it took me a lot of time ).

On the other hand, I you want circle coords for other purposes (e.g. calc coordinates in a trajectory), maybe you should have a look at library-asm/circle.asm
The bresenham circle algorithm is described here: http://en.wikipedia.org/wiki/Midpoint_circle_algorithm (the C routine under the "Optimization section").

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

### Re: Rotating some points , seems slow

im drawing lines between the points now (with DRAW )
but they seem to go up and right for some reason,

here is the code

triship arrays are two dimensional, one dimension for which ship it refers to
and the other dimension for the points

Code:
`#include <FSin.bas>#include <SP/Fill.bas>Dim fsinarray(361) as Fixedfor iter=0 to 360fsinarray(iter)=fSin(iter)nextDim fcosarray(361) as Fixedfor iter=0 to 360fcosarray(iter)=fCos(iter)nextDim trishipx(2,12) as UByteDim trishipy(2,12) as UByteDim drawtrishipx(2,12) as UBytedim drawtrishipy(2,12) as UByteDim fc, fs, tx, ty As Fixedtrishipx(0,0)=62trishipy(0,0)=58trishipx(0,1)=58trishipy(0,1)=58trishipx(0,2)=58trishipy(0,2)=62trishipx(0,3)=58trishipy(0,3)=54trishipx(0,4)=60trishipy(0,4)=58trishipx(0,5)=60trishipy(0,5)=62trishipx(1,0)=162trishipy(1,0)=158trishipx(1,1)=158trishipy(1,1)=158trishipx(1,2)=158trishipy(1,2)=162trishipx(1,3)=158trishipy(1,3)=154trishipx(1,4)=160trishipy(1,4)=158trishipx(1,5)=160trishipy(1,5)=162Dim trishipangle as IntegerFunction rotate(degrees as Integer) as Integerdim pointx as Ubytedim pointy as Ubytefor trishipiter =0 to 1if trishipiter=0 thenpointx=54pointy=54end ifif trishipiter=1 thenpointx=100pointy=100end ifif trishipiter=2 thenpointx=154pointy=54end if  for  x=0 to 5'Local theta:Float=1fc = fcosarray(degrees)fs = fsinarray(degrees)tx = trishipx(trishipiter,x) - pointxty = trishipy(trishipiter,x) - pointydrawtrishipx(trishipiter,x)= fc * tx - fs * ty + pointxdrawtrishipy(trishipiter,x)= fs * tx + fc * ty + pointy'transformedX= fCos(degrees) * (trishipx(x)-pointx) - fSin(degrees) * (trishipy(x)-pointy) + pointx'transformedY = fSin(degrees) * (trishipx(x)-pointx) + fCos(degrees) * (trishipy(x)-pointy) + pointy Nextnextreturn 1End FunctionFunction drawship() as Integerfor trishipiter=0 to 1for x=0 to 5'plotPoint(drawtrishipx(trishipiter,x),drawtrishipy(trishipiter,x))if x<5 thenPLOT drawtrishipx(trishipiter,x), drawtrishipy(trishipiter,x)DRAW drawtrishipx(trishipiter,x+1), drawtrishipy(trishipiter,x+1)end ifif x=5 thenPLOT drawtrishipx(trishipiter,x), drawtrishipy(trishipiter,x)DRAW drawtrishipx(trishipiter,0), drawtrishipy(trishipiter,0)end ifnextnextreturn 1End FunctionWhile 1CLStrishipangle=trishipangle+1if trishipangle>360 thentrishipangle=0end if'for x=0 to 5'plotPoint(drawtrishipx(x),drawtrishipy(x))'nextrotate(trishipangle)drawship()WendSUB plotPoint (x as uByte, y as uByte)ASMld d,(IX+5) ;'Xld e,(IX+7) ;'Y    ld a, 191   sub e      ret c      ld e, a      and a          rra         scf         rra         and a      rra         xor e            and 248   xor e   ld h, a   ld a, d   rlca   rlca   rlca   xor e   and 199   xor e   rlca   rlca   ld l, a       ld a, d   and 7   ld b, a   inc b   ld a, 1       plotPoint_loop:        rrca   djnz plotPoint_loop      ;cpl    ld b, a      ld a, (hl)   or b      ld (hl), a   END ASMEDIT-after drawing my own lines I could draw a rotating part of a spaceship(featuring britlions fill routine)http://www.4shared.com/file/DLgxB2PY/rotate_1.htmllooks like it will have to be...turn based spaceship combatEND SUB`

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

### Re: Rotating some points , seems slow

I tried drawing my own lines and now I can draw part of a rotating spaceship
(also featuring britlions fill routine)
http://www.4shared.com/file/DLgxB2PY/rotate_1.html

turn based spaceship battles ahoy

Site Admin

Posts: 1521

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

### Re: Rotating some points , seems slow

slenkar wrote:I was going to make a vector graphics game like asteroids but different
I rotated some points around an origin but even that seems too slow

Sorry. Didn't read this msg yesterday. This could be a bug. Will have a look.
Also, Draw routines uses Integer values (Byte / Ubyte / Uinteger will overflow).

Let me check it...

Return to Help & Support

### Who is online

Users browsing this forum: No registered users and 0 guests

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin for Free Forums/DivisionCore.