Dark Bit Factory & Gravity
PROGRAMMING => Other languages => Blitz => Topic started by: mike_g on January 10, 2007
-
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:
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:
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
-
I have not examined your code, unfortunately, but here is an example I have just coded. I hope that it gives you some ideas.
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.
-
here is an example with cos and sin modified from your example.
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
-
Here's the original code modified
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
-
wow! is that your own code? quite usefull makes me want to remove my post.
-
Wow, thanks for the help guys O0
I had thought this topic was dead.
-
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
-
@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 :)
-
Jim, can I borrow your brain for a few weeks? ;D
-
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
-
;D
-
@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]
-
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 (http://www40.brinkster.com/mikegrundel/). 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 :'(
-
here is the link:
http://www.gamedev.net/community/gds/viewentry.asp?projectid=427815
-
Oh I see I went to gamedev.org. Anyway its a cool game :)
-
oops! my fault. I didn't realized I keyed it wrong. anyway I fixed it.