2D Pivoting around a point

Blitz3D Forums/Blitz3D Programming/2D Pivoting around a point

Jager(Posted 2004) [#1]
Quick, could somesone give me a 2D pivoting formulae.

For example, a point x=5 and z=3 away from a cental pivot point, pivoting 45% 8 times.

for some strange reason mine only half works?


Matty(Posted 2004) [#2]
Okay...if you have a point which is 5 units in the x direction and 3 units in the z direction away from a point and you wish to rotate the coordinates by 45 degrees 8 times then do the following:

Radius#=Sqr(5^2+3^2)

NewX=PivotX+Radius*Cos(Angle)
NewZ=PivotZ+Radius*Sin(Angle)

assuming you are rotating in the xz plane and that PivotX,PivotZ are the world coordinates of your central pivot point.

The only parameter that you then need to pass is the "Angle" variable.


Jager(Posted 2004) [#3]
thanks but I lied a bit, I really want 4 points in a box formation to rotate around a central point - while keeping the formation.

The problem seems to be that the angle (in the above formula), is related to world coordinates not related to the object. I need to add /subtract extra degrees for each point and it's in adding and subtracting, things start to go wrong .. as you can gather at this point, I'm no mathematician.

Here's my test code .. don't laugh! :)


Graphics 800, 600, 32, 2

Cls

x1# = 200.0
z1# = 200.0
x2# = 250.0
z2# = 200.0
x3# = 200.0
z3# = 250.0
x4# = 250.0
z4# = 250.0

ax# = 350.0
az# = 350.0

deg# = 45.0

Color 250, 0, 0
Oval ax#, az#, 3, 3, True

For d = 0 To 360 Step 45

Radius1# = Sqr( (ax#-x1#)^2 + (az#-z1#)^2 )
NewAng1# = ASin( (ax#-x1#) / radius1# )
x1# = ax# + Cos( d-NewAng1# ) * radius1#
z1# = az# + Sin( d-NewAng1# ) * radius1#
Radius2# = Sqr( (ax#-x2#)^2 + (az#-z2#)^2 )
NewAng2# = ASin( (ax#-x2#) / radius2# )
x2# = ax# + Cos( d-NewAng2# ) * radius2#
z2# = az# + Sin( d-NewAng2# ) * radius2#
Radius3# = Sqr( (ax#-x3#)^2 + (az#-z3#)^2 )
NewAng3# = ASin( (ax#-x3#) / radius3# )
x3# = ax# + Cos( d-NewAng3# ) * radius3#
z3# = az# + Sin( d-NewAng3# ) * radius3#
Radius4# = Sqr( (ax#-x4#)^2 + (az#-z4#)^2 )
NewAng4# = ASin( (ax#-x4#) / radius4# )
x4# = ax# + Cos( d-NewAng4# ) * radius4#
z4# = az# + Sin( d-NewAng4# ) * radius4#


Color 250, 250, 250

Rect x1# , z1#, 20, 20
Rect x2# , z2#, 20, 20
Rect x3# , z3#, 20, 20
Rect x4# , z4#, 20, 20

Delay 2000

Next

WaitKey()


Floyd(Posted 2004) [#4]
To go back to your original example, x=5 z=3, we have

Radius#=Sqr(5^2+3^2)

Then points on a circle are given by

newX# = CenterX# + Radius*Cos(Angle#)
newY# = CenterZ# + Radius*Sin(Angle#)

The only remaining question is where to start. The answer is

Angle0# = ATan2( 3, 5 )

With this angle we get newX = 5 and newY = 3.

After that use 45+Angle0, 90+Angle0 etc.


Jager(Posted 2004) [#5]
strange?

CurAng# = ATan( -5 / -5 )

CurAng# = equals 45 degrees?

Shouldn't that be -45 degrees? I mean a point @ x = -5, z = -5 would be -45 o from origin, yes?


Floyd(Posted 2004) [#6]
ATan deals with a line though (0,0). Note that (5,5) and (-5,-5) are on the same line.
The same is true for any point (x,x) where x is not zero.

So ATan(x/x) is 45.

This is the reason ATan2 exists.


Jager(Posted 2004) [#7]
Ah! that's what I was looking for.

I'm blind as well as being a poor mathematician.

thanks. :)