Author Topic: Centering point of rotation on a rectangle[BB2D]  (Read 944 times)

0 Members and 1 Guest are viewing this topic.

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile


Jim posted some code for me the other day to draw a rotatable rectangle. I have been trying to change the point of rotation from a corner of the shape to the centre of it. I seem to have got it centred on one axis but cant centered on the other, and my rectangle now seems to be bigger than its meant to be o_0. Anyway I'm not so good at maths so I have sort of been guessing what to do, heres my code:
Code: [Select]
SetBuffer BackBuffer()
While Not KeyHit(1)
Cls
ROTATING_RECT(200, 150, 80, 40, i)
i=i+1
If i = 360 Then i = 0
Flip
Wend

Function ROTATING_RECT(x, y, w, h, angle)
s# = Sin(angle)
c# = Cos(angle)

ox = (w/2)
oy = (h/2)

sw# = ox*s
cw# = ox*c
sh# = oy*s
ch# = oy*c

x1 = x-cw-ch: y1 = y-sw-sh
x2 = x+cw+ch: y2 = y+sw+sh
x3 = x+cw-sh+ch-sw: y3 = y+sw+ch+sh+cw
x4 = x-sh-cw-sw-ch: y4 = y+ch-sw+cw-sh

Plot x, y
Plot x1, y1
Plot x2, y2
Plot x3, y3
Plot x4, y4
End Function
Since my code might be more confusing than helpfull. Heres Jims code converted to draw in Blitz as well:
Code: [Select]
SetBuffer BackBuffer()
While Not KeyHit(1)
Cls
ROTATING_RECT(200, 150, 80, 30, i)
i=i+1
If i = 360 Then i = 0
Flip
Wend

Function ROTATING_RECT(x, y, w, h, angle)
s# = Sin(angle)
c# = Cos(angle)
sw# = w*s
cw# = w*c
sh# = h*s
ch# = h*c

Plot x, y
Plot x+cw, y+sw
Plot x+cw-sh, y+sw+ch
Plot x-sh, y+ch
End Function
« Last Edit: July 21, 2007 by Shockwave »

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 886
  • Karma: 63
    • View Profile
    • zac-interactive
Re: Centering point of rotation on a rectangle
« Reply #1 on: January 12, 2007 »
I have not examined your code, unfortunately, but here is an example I have just coded. I hope that it gives you some ideas.

Code: [Select]
Graphics 640,480,32,2

Dim ccos#(360),ssin#(360)
For a = 0 To 359
ccos#(a) = Cos(a)
ssin#(a) = Sin(a)
Next

angle = 0
scale# = 3.0

SetBuffer BackBuffer()
While Not KeyHit(1)

drawRotRect( 120,240, -20,-20, 20,-20, 10,20, -10,20, 3.0, angle, 255,0,0 )
drawRotRect( 320,240, -20,-20, 20,-20, 20,20, -20,20, scale#, angle2, 0,255,0 )
drawRotRect( 520,240, -10,-20, 10,-20, 20,20, -20,20, 3.0, angle, 0,0,255 )

If newpart + 15 <= MilliSecs() Then
angle = angle + 2
If angle > 359 Then angle = angle - 360
angle2 = angle2 - 2
If angle2 < 0 Then angle2 = angle2 + 360
scale# = 3.0 + Sin( angle ) * 1.5
newpart = MilliSecs()
End If

Flip
Cls

Wend
End

Function drawRotRect( x,y, x1,y1, x2,y2, x3,y3, x4,y4, scale#, rot, r,g,b )
xx1 = x + ((ccos#(rot) * x1) - (ssin#(rot) * y1)) * scale#
xx2 = x + ((ccos#(rot) * x2) - (ssin#(rot) * y2)) * scale#
xx3 = x + ((ccos#(rot) * x3) - (ssin#(rot) * y3)) * scale#
xx4 = x + ((ccos#(rot) * x4) - (ssin#(rot) * y4)) * scale#

yy1 = y + ((ssin#(rot) * x1) + (ccos#(rot) * y1)) * scale#
yy2 = y + ((ssin#(rot) * x2) + (ccos#(rot) * y2)) * scale#
yy3 = y + ((ssin#(rot) * x3) + (ccos#(rot) * y3)) * scale#
yy4 = y + ((ssin#(rot) * x4) + (ccos#(rot) * y4)) * scale#

Color r,g,b

Line xx1,yy1, xx2,yy2
Line xx2,yy2, xx3,yy3
Line xx3,yy3, xx4,yy4
Line xx4,yy4, xx1,yy1

End Function

I have used arrays for the sin and cos data, but you could also just use the sin() and cos() functions directly if you wanted to.


Offline JumpMan

  • C= 64
  • **
  • Posts: 45
  • Karma: 11
    • View Profile
Re: Centering point of rotation on a rectangle
« Reply #2 on: January 12, 2007 »
here is an example with cos and sin modified from your example.
Code: [Select]
SetBuffer BackBuffer()
While Not KeyHit(1)
Cls
ROTATING_RECT(200, 150, 80, 30, i)
i=(i+1) Mod 360
Flip
Wend

Function ROTATING_RECT(x, y, w#, h#, angle) ; x And y are used as the center of the Rect.
radius# = Sqr((w/2.0)^2+(h/2.0)^2) ; find radius from center of rect to one corner
rbx#= w/2.0 ;find center of rect
rby#= h/2.0
rba# = ATan2(rby,rbx) ;angle of bottom left corner of rect at 0 degrees.
rta# = 360.0-rba ;angle of right top corner
lba# = 180.0-rba ;angle of left bottom corner
lta# = 180.0+rba ;angle of left top corner
ltx# = Cos(lta+angle)*radius ;find x and y of each corner.
lty# = Sin(lta+angle)*radius
lbx# = Cos(lba+angle)*radius
lby# = Sin(lba+angle)*radius
rtx# = Cos(rta+angle)*radius
rty# = Sin(rta+angle)*radius
rbx# = Cos(rba+angle)*radius
rby# = Sin(rba+angle)*radius

Line x+rbx,y+rby,x+rtx,y+rty ; draw lines for rectanle
Line x+lbx,y+lby,x+rbx,y+rby
Line x+ltx,y+lty,x+lbx,y+lby
Line x+ltx,y+lty,x+rtx,y+rty

End Function

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5098
  • Karma: 380
    • View Profile
Re: Centering point of rotation on a rectangle
« Reply #3 on: January 13, 2007 »
Here's the original code modified
Code: [Select]
SetBuffer BackBuffer()
While Not KeyHit(1)
Cls
ROTATING_RECT(200, 150, 80, 30, i)
i=i+1
If i = 360 Then i = 0
Flip
Wend

Function ROTATING_RECT(x, y, w, h, angle)
s# = Sin(angle)
c# = Cos(angle)
sw# = w*s
cw# = w*c
sh# = h*s
ch# = h*c
ox=-(cw-sh)/2
oy=-(sw+ch)/2
Plot ox+x, oy+y
Plot ox+x+cw, oy+y+sw
Plot ox+x+cw-sh, oy+y+sw+ch
Plot ox+x-sh, oy+y+ch
End Function

Jim
Challenge Trophies Won:

Offline JumpMan

  • C= 64
  • **
  • Posts: 45
  • Karma: 11
    • View Profile
Re: Centering point of rotation on a rectangle
« Reply #4 on: January 13, 2007 »
wow! is that your own code? quite usefull makes me want to remove my post.

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Centering point of rotation on a rectangle
« Reply #5 on: January 13, 2007 »
Wow, thanks for the help guys   O0

I had thought this topic was dead.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5098
  • Karma: 380
    • View Profile
Re: Centering point of rotation on a rectangle
« Reply #6 on: January 13, 2007 »
Yeah, it's mine.
The deal is that sin(x) = cos(90-x).  And also to rotate 90 degress you can take x,y and make it y,-x so you can do the whole rectangle thing with just 2 pairs of sin and cos.

Jim
Challenge Trophies Won:

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Centering point of rotation on a rectangle
« Reply #7 on: January 13, 2007 »
@Jim: Yeah thats a good method and makes sense.

@Jumpman: Allways good to see another Blitz coder. Welcome to the forum. I've only been here a couple of weeks myself but I quite like it here :) 

Offline rdc

  • Pentium
  • *****
  • Posts: 1480
  • Karma: 138
    • View Profile
    • Clark Productions
Re: Centering point of rotation on a rectangle
« Reply #8 on: January 13, 2007 »
Jim, can I borrow your brain for a few weeks?  ;D

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5098
  • Karma: 380
    • View Profile
Re: Centering point of rotation on a rectangle
« Reply #9 on: January 13, 2007 »
You can have it right now.  It hurts and is useless for anything.  Red wine hangover.  Let me know when it's mended. :D :P
Jim

Challenge Trophies Won:

Offline rdc

  • Pentium
  • *****
  • Posts: 1480
  • Karma: 138
    • View Profile
    • Clark Productions
Re: Centering point of rotation on a rectangle
« Reply #10 on: January 13, 2007 »
 ;D

Offline JumpMan

  • C= 64
  • **
  • Posts: 45
  • Karma: 11
    • View Profile
Re: Centering point of rotation on a rectangle
« Reply #11 on: January 14, 2007 »
@Jim
I have recently started making games. I am working on a 2D overhead view tank game and use the four corners of the tank to check for collition against other objects. I used the formula in my previous post for calculating the colltion points. So abviously you can see how your formula would inprove performance on my game.  If you are interested in looking at the game it's at gamedev.net at GDshowcase under Tank Supremacy. Don't spect too much.
@mike_g
Thanks for the welcome.
I am a hobbist programmer. In the past I have use blitz basic then blitz plus and now I use BMAX and I love it. but every now and then I go through the blitz forums and look at other users code to see what I can learn. There is always someone I can learn from. And this thread was a perfect example. I will be checking in now and then. if anybody is interested in my help with anything programming I am willing to lend a hand even work on a project with someone. my skills are not great but I am shure there is someone I can help.

-cheers.
[edited]

« Last Edit: January 14, 2007 by JumpMan »

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Centering point of rotation on a rectangle
« Reply #12 on: January 14, 2007 »
I had a look in the gamedev showcase but couldent seem to find your game ??? Anyway I'm making a tank game too :)

I only started on it the other day, and the graphics are just basic vector stuff. I uploaded an exe here. You will have to click on the 'tank thing' link cos I cant do remote linking at d mo. Its nothing great though since you cant do much more than drive around and blow up the brown walls.

As for the collisions, i started off just checking the corner points but found for my prog it worked better to check all the pixels along the edge of the tank (4 lines). That said I still have some work to do on the collisions since you can get stuck against the walls at the moment.

Unfortunately though I dont have to much time for coding at the moment, got an exam on tuesday and an assignment that needs to  be done for tomorrow  :'(

Offline JumpMan

  • C= 64
  • **
  • Posts: 45
  • Karma: 11
    • View Profile

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Centering point of rotation on a rectangle
« Reply #14 on: January 14, 2007 »
Oh I see I went to gamedev.org. Anyway its a cool game :)

Offline JumpMan

  • C= 64
  • **
  • Posts: 45
  • Karma: 11
    • View Profile
Re: Centering point of rotation on a rectangle
« Reply #15 on: January 14, 2007 »
oops! my fault. I didn't realized I keyed it wrong. anyway I fixed it.