How to orient an object to point at another?
Community Forums/General Help/How to orient an object to point at another?
| ||
If I've an object at 0,0 and another at 9,9 - how would I calculate the 45 degrees that object a needs to be rotated through to point at object B? I reckon the solution might lie within the triginometry SOHCAHTOA.... [EDIT] Codebox at the bottom now moves the rocket into the Z-plane [end - edit] This looks promising: http://www.mathsisfun.com/polar-cartesian-coordinates.html Hmm, seems like I'll need some quadrant logic: To convert from Cartesian Coordinates (x,y) to Polar Coordinates (r,θ): r = (sqr root) ( x2 + y2 ) (theta) = tan-1 ( y / x ) The value of tan-1( y/x ) may need to be adjusted: Quadrant I: Use the calculator value Quadrant II: Add 180° Quadrant III: Add 180° Quadrant IV: Add 360° Last edited 2012 Last edited 2012 Last edited 2012 |
| ||
This will do it. It's in Blitzmax format but it's just the ATan2 bit you'll be interested in, which is the same in every other version of Blitz, as far as I know.Local x1:Int = 0, y1:Int = 0 Local x2:Int = 9, y2:Int = 9 Local angle:Float = ATan2(y2-y1,x2-x1) Print angle |
| ||
Thanks GfK, will give it a whirl this evening |
| ||
Jeez, my B3D has got a bit rusty - cobbled this together:Graphics3D 1024,768,32,1 x1= 0 y1= 0 x2= 9 y2= 9 angle#= Float ATan2(y2-y1,x2-x1) Print "Angle is"+angle .cmere VWait 1000 Goto cmere Cheers GfK, works a treat! Last edited 2012 |
| ||
A little work to do yet:--Graphics3D 1024,768,32,2 x1= 0 y1= 0 x2= 9 y2= 9 x3=9 y3=-9 x4=-9 y4=-9 x5=-9 y5=9 x6=-18 y6=3 x7=-1 y7=12 angle#= Float ATan2(y2-y1,x2-x1) ;2nd quadrant angle2#= (Float (ATan2(y3-y1,x3-x1)))+180 ;3rd quadrant angle3#= (Float (ATan2(y4-y1,x4-x1)))+360 ;4th quadrant angle4#= (Float (ATan2(y5-y1,x5-x1)))+180 ;4th quadrant angle5#= (Float (ATan2(y5-y1,x5-x1)))+180 angle6#= (Float (ATan2(y6-y1,x6-x1)))+180 ;this would be 'right' if x and y values transposed angle7#= (Float (ATan2(y7-y1,x7-x1)))+180 ;this would be 'right' if x and y values transposed Print "Angle is "+angle Print "Angle 2,3,4 is "+angle2+" 3: "+angle3+" 4: "+angle4 ;needs work for other quadrants Print "4th Quadrant -- 5(315 degrees ish) : "+angle5+" 6 (280 degrees ish): "+angle6+" Angle 7 (350 degress ish) is:"+angle7 ;Make a little loop to keep the results on the screen until escape is pressed: While Not KeyHit(1) VWait 1000 Wend End |
| ||
Pointy rockets, the Dictator would be pleased ;) Needs the rocket.3ds entity Last edited 2012 |
| ||
ATan2 considers the X+ axis as zero degrees and Y+ as ninety. To use this properly the rocket should point to the right when its rotation is zero. It actually points up. If you just rotate the mesh so it initially points to the right then all the problems go away and ATan2 gives the angle you want. Here's the simplified code. Use the arrow keys to move the target. Graphics3D 800,600 camera=CreateCamera() PositionEntity camera, 0, 0, -80 CameraZoom camera, 4 target = CreateSphere() PositionEntity target, 10, 0, 0 rocket=LoadMesh( "rocket.3ds" ) ;rocket=CreateCone() ; alternative to loaded 3ds model RotateMesh rocket, 0, 0, -90 ; so a rotation of 0 points to the right While Not KeyHit(1) x1# = EntityX( rocket ) y1# = EntityY( rocket ) x2# = EntityX( target ) y2# = EntityY( target ) RotateEntity rocket, 0, 0, ATan2( y2 - y1, x2 - x1 ) MoveEntity target, 0.1 * ( KeyDown(205) - KeyDown(203) ), 0.1 * ( KeyDown(200) - KeyDown(208) ), 0 RenderWorld Flip Wend Last edited 2012 |
| ||
the best way to do it is to create a pivot parented to the rocket point the pivot to the target and its roll,yaw,pitch values will give you the difference in angle. |
| ||
you could also just have a pivot called dummy then position this at the rocket, point it at the target then get the angles, compare that to the rocket angle to get which way it has to turn and there ya go. Using that method you only need one pivot for a million rockets :D |
| ||
you could also just have a pivot called dummy then position this at the rocket But a million transformations heheh |
| ||
Floyd, thanks for the feedback - will incorporate it at my next revision, which might be move all the rockets at once.... Into the Horizon:- |