FAQ  •  Register  •  Login

Rotating some points , seems slow

<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Mon Apr 08, 2013 12:59 am

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 UByte
Dim trishipy(12) as UByte
Dim drawtrishipx(12) as UByte
dim drawtrishipy(12) as UByte


trishipx(0)=62
trishipy(0)=58

trishipx(1)=58
trishipy(1)=58

trishipx(2)=58
trishipy(2)=62

trishipx(3)=58
trishipy(3)=54

trishipx(4)=60
trishipy(4)=58

trishipx(5)=60
trishipy(5)=62

Dim trishipangle as Integer

Function rotate(degrees as Integer) as Integer
 dim transformedX as Ubyte=0
 dim transformedY as ubyte=0
dim pointx as Ubyte=54
dim pointy as Ubyte=54
  for  x=0 to 5
'Local theta:Float=1
transformedX= fCos(degrees) * (trishipx(x)-pointx) - fSin(degrees) * (trishipy(x)-pointy) + pointx
transformedY = fSin(degrees) * (trishipx(x)-pointx) + fCos(degrees) * (trishipy(x)-pointy) + pointy
 drawtrishipx(x) = transformedX
 drawtrishipy(x) = transformedY
Next
return 1
End Function

Function drawship() as Integer


for x=0 to 5
plotPoint(drawtrishipx(x),drawtrishipy(x))
next

return 1
End Function

While 1


trishipangle=trishipangle+1
if trishipangle>360 then
trishipangle=0
end if


rotate(trishipangle)

drawship()

Wend



SUB plotPoint (x as uByte, y as uByte)
ASM
ld d,(IX+5) ;'X
ld 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 ASM
END SUB
<<

boriel

Site Admin

Posts: 1461

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Mon Apr 08, 2013 8:04 am

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) + pointx
transformedY = fSin(degrees) * (trishipx(x)-pointx) + fCos(degrees) * (trishipy(x)-pointy) + pointy

To be rewritten as
  Code:
Dim fc, fs, tx, ty As Fixed
fc = fCos(degrees)
fs = fSin(degrees)
tx = trishipx(x) - pointx
ty = trishipy(x) - pointy

transformedX= fc * tx - fs * ty + pointx
transformedY = 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. :oops:

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

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Mon Apr 08, 2013 12:49 pm

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
<<

britlion

Posts: 762

Joined: Mon Apr 27, 2009 7:26 pm

Location: Slough, Berkshire, UK

Post Wed Apr 10, 2013 3:01 pm

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?)
<<

boriel

Site Admin

Posts: 1461

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Wed Apr 10, 2013 4:13 pm

Re: Rotating some points , seems slow

Awesome :!: :shock:

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 :?:
<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Wed Apr 10, 2013 7:29 pm

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...
<<

boriel

Site Admin

Posts: 1461

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Wed Apr 10, 2013 8:57 pm

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 :roll: ).

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").
<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Wed Apr 10, 2013 10:12 pm

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 Fixed
for iter=0 to 360
fsinarray(iter)=fSin(iter)
next

Dim fcosarray(361) as Fixed
for iter=0 to 360
fcosarray(iter)=fCos(iter)
next

Dim trishipx(2,12) as UByte
Dim trishipy(2,12) as UByte
Dim drawtrishipx(2,12) as UByte
dim drawtrishipy(2,12) as UByte
Dim fc, fs, tx, ty As Fixed


trishipx(0,0)=62
trishipy(0,0)=58

trishipx(0,1)=58
trishipy(0,1)=58

trishipx(0,2)=58
trishipy(0,2)=62

trishipx(0,3)=58
trishipy(0,3)=54

trishipx(0,4)=60
trishipy(0,4)=58

trishipx(0,5)=60
trishipy(0,5)=62


trishipx(1,0)=162
trishipy(1,0)=158

trishipx(1,1)=158
trishipy(1,1)=158

trishipx(1,2)=158
trishipy(1,2)=162

trishipx(1,3)=158
trishipy(1,3)=154

trishipx(1,4)=160
trishipy(1,4)=158

trishipx(1,5)=160
trishipy(1,5)=162


Dim trishipangle as Integer

Function rotate(degrees as Integer) as Integer
dim pointx as Ubyte
dim pointy as Ubyte

for trishipiter =0 to 1
if trishipiter=0 then
pointx=54
pointy=54
end if
if trishipiter=1 then
pointx=100
pointy=100
end if
if trishipiter=2 then
pointx=154
pointy=54
end if

  for  x=0 to 5
'Local theta:Float=1

fc = fcosarray(degrees)
fs = fsinarray(degrees)
tx = trishipx(trishipiter,x) - pointx
ty = trishipy(trishipiter,x) - pointy

drawtrishipx(trishipiter,x)= fc * tx - fs * ty + pointx
drawtrishipy(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
 
Next
next
return 1
End Function

Function drawship() as Integer
for trishipiter=0 to 1
for x=0 to 5
'plotPoint(drawtrishipx(trishipiter,x),drawtrishipy(trishipiter,x))

if x<5 then
PLOT drawtrishipx(trishipiter,x), drawtrishipy(trishipiter,x)
DRAW drawtrishipx(trishipiter,x+1), drawtrishipy(trishipiter,x+1)
end if
if x=5 then
PLOT drawtrishipx(trishipiter,x), drawtrishipy(trishipiter,x)
DRAW drawtrishipx(trishipiter,0), drawtrishipy(trishipiter,0)
end if

next
next
return 1
End Function

While 1
CLS

trishipangle=trishipangle+1
if trishipangle>360 then
trishipangle=0
end if



'for x=0 to 5
'plotPoint(drawtrishipx(x),drawtrishipy(x))
'next

rotate(trishipangle)

drawship()

Wend



SUB plotPoint (x as uByte, y as uByte)
ASM
ld d,(IX+5) ;'X
ld 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 ASM


EDIT-
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.html

looks like it will have to be...turn based spaceship combat
END SUB
<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Thu Apr 11, 2013 2:12 am

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
<<

boriel

Site Admin

Posts: 1461

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Thu Apr 11, 2013 7:15 am

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 2 guests

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

phpBB SEO