I'm working on a concept that makes heavy use of rotating objects, and after I gave up trying to work with matrices for hitboxes because they're a pain to work with in DB, I decided to try a more interesting approach -- hitboxes built out of vectors that are calculated by angles relative to each other, rather than annoying matrix operations.
The results seem pretty good using this method, and they're much easier to work with, however I'm having a hard time drawing the first point at a proper distance from the center, of the box. It likes to end up in a weird position.
Maybe I'm using the wrong equation here, but I'm pretty sure the distance from the center of the box should be determined by A^2+B^2=C^2, then divide C/2. Nope.
It looks to be accurate if I draw the box with equal width and height, but making it's width larger than the height or vise versa skews the calculation.
My only idea of what's going wrong is that it may have something to do with the fact that the equations I'm using don't translate perfectly to DarkBasic's coordinate system where Y gets larger as it moves downward rather than the other way around.
What might I be doing wrong here?
SYNC ON
SYNC RATE 30
Rem Obj#(obj number, vector number, 1/2=x/y coord).
Rem vector 5 = center, vector 1-4 = top left, top right, bottom right, bottom left.
DIM Obj#(10,5,2)
Rem The angle of the vector from the center of the object.
DIM Angle#(10,5)
Rem the size/scale of the object. 1/2 = x/y = width/height.
DIM Scale#(10,2)
Box_angle=0
DO
CLS
Box_Angle=Box_Angle+1
IF Box_Angle < 0 THEN Box_Angle=Box_Angle+360
IF Box_Angle > 359 THEN Box_Angle=Box_Angle-360
GOSUB Calculate
GOSUB Draw_Scene
sync
LOOP
Calculate:
Rem The center point of the player. Other poitns are calculated bades on the center.
Obj#(1,5,1)=100
Obj#(1,5,2)=100
Rem player is situated straight upwards.
Angle#(1,5)=Box_angle
IF Angle#(1,5) < 0 THEN Angle#(1,5)=Angle#(1,5)+360
IF Angle#(1,5) > 359 THEN Angle#(1,5)=Angle#(1,5)-360
Rem determining the position of the other vector points.
Angle#(1,1)=Angle#(1,5)-45
Rem Keep the angle within the bounds of 360 degrees.
IF Angle#(1,1) < 0 THEN Angle#(1,1)=Angle#(1,1)+360
IF Angle#(1,1) > 359 THEN Angle#(1,1)=Angle#(1,1)-360
Angle#(1,2)=Angle#(1,1)+135
IF Angle#(1,2) < 0 THEN Angle#(1,2)=Angle#(1,2)+360
IF Angle#(1,2) > 359 THEN Angle#(1,2)=Angle#(1,2)-360
Angle#(1,3)=Angle#(1,2)+90
IF Angle#(1,3) < 0 THEN Angle#(1,3)=Angle#(1,3)+360
IF Angle#(1,3) > 359 THEN Angle#(1,3)=Angle#(1,3)-360
Angle#(1,4)=Angle#(1,3)+90
IF Angle#(1,4) < 0 THEN Angle#(1,4)=Angle#(1,4)+360
IF Angle#(1,4) > 359 THEN Angle#(1,4)=Angle#(1,4)-360
Rem Object #1 scaled at 30x50 pixels
Scale#(1,1)=40
Scale#(1,2)=25
Rem Calculating the distance from the center of the box to the top left (starting point)
A2 = Scale#(1,1)/2
B2 = Scale#(1,2)/2
C2 = (A2*A2)+(B2*B2)
C2 = SQRT(C2)
Rem X coord of the top left vector point
Obj#(1,1,1)= Obj#(1,5,1) + (C2 * COS(Angle#(1,1)))
Obj#(1,1,2)= Obj#(1,5,2) + (C2 * SIN(Angle#(1,1)))
Rem On to the top right point
Obj#(1,2,1)= Obj#(1,1,1) + (Scale#(1,2) * COS(Angle#(1,2)))
Obj#(1,2,2)= Obj#(1,1,2) + (Scale#(1,2) * SIN(Angle#(1,2)))
Obj#(1,3,1)= Obj#(1,2,1) + (Scale#(1,1) * COS(Angle#(1,3)))
Obj#(1,3,2)= Obj#(1,2,2) + (Scale#(1,1) * SIN(Angle#(1,3)))
Obj#(1,4,1)= Obj#(1,3,1) + (Scale#(1,2) * COS(Angle#(1,4)))
Obj#(1,4,2)= Obj#(1,3,2) + (Scale#(1,2) * SIN(Angle#(1,4)))
RETURN
Draw_Scene:
Print STR$(Angle#(1,1))
Print STR$(Angle#(1,2))
Print STR$(Angle#(1,3))
Print STR$(Angle#(1,4))
Print STR$(Angle#(1,5))
Rem Draw the vector square
LINE Obj#(1,1,1), Obj#(1,1,2), Obj#(1,2,1), Obj#(1,2,2)
LINE Obj#(1,2,1), Obj#(1,2,2), Obj#(1,3,1), Obj#(1,3,2)
LINE Obj#(1,3,1), Obj#(1,3,2), Obj#(1,4,1), Obj#(1,4,2)
LINE Obj#(1,4,1), Obj#(1,4,2), Obj#(1,1,1), Obj#(1,1,2)
LINE Obj#(1,5,1), Obj#(1,5,2), (Obj#(1,5,1) + (30*COS(Angle#(1,5)))), (Obj#(1,5,2) + (30*SIN(Angle#(1,5))))
DOT Obj#(1,5,1), Obj#(1,5,2)
RETURN