Ooops
November 25, 2020, 07:43:35 AM

Author Topic: [bmx] minib3d AlignToVector by Warner [ 1+ years ago ]  (Read 690 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] minib3d AlignToVector by Warner [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : minib3d AlignToVector
Author : Warner
Posted : 1+ years ago

Description : A very basic minib3d version of AlignToVector. Hopefully it works as planned. Includes a small test program.

Code :
Code: BlitzMax
  1. Strict
  2.  
  3. Import sidesign.minib3d
  4.  
  5. Graphics3D 800, 600, 0, 2
  6.  
  7. CreateLight()
  8.  
  9. 'camera
  10. Local cam:TCamera = CreateCamera()
  11. MoveEntity cam, 0, 5, -15
  12.  
  13. 'cone
  14. Local cube:TMesh = CreateCone()
  15. RotateMesh cube, 90, 0, 0
  16. PositionMesh cube, 0, 0, 1
  17. Local surf:TSurface = GetSurface(cube, 1)
  18. VertexColor surf, 0, 255, 0, 0
  19. VertexColor surf, 1, 255, 0, 0
  20. VertexColor surf, 2, 255, 0, 0
  21. EntityFX cube, 2
  22.  
  23. 'sphere
  24. Local sph:TMesh = CreateSphere()
  25. ScaleMesh sph, 5, 5, 5
  26. EntityColor sph, 0, 255, 0
  27. EntityPickMode sph, 2
  28. surf = GetSurface(sph, 1)
  29. For Local i% = 0 To CountVertices(surf) - 1
  30.         VertexColor surf, i, 0, Rand(128, 255), 0
  31. Next
  32. EntityFX sph, 2
  33.  
  34. PointEntity cam, cube
  35.  
  36. Repeat
  37.  
  38.         'turn sphere using keys
  39.         TurnEntity sph, 0, KeyDown(39)-KeyDown(37), KeyDown(40)-KeyDown(38)
  40.        
  41.         'click on sphere to place cone
  42.         If MouseDown(1)
  43.                 CameraPick cam, MouseX(), MouseY()
  44.                 PositionEntity cube, PickedX(), PickedY(), PickedZ()
  45.                
  46.                 AlignToVector cube, PickedNX(), PickedNY(), PickedNZ()
  47.         End If
  48.                
  49.         RenderWorld
  50.                        
  51.         Flip
  52.  
  53. 'esc=exit
  54. Until KeyHit(27)
  55.  
  56. End
  57.  
  58. '--------------------------------------------------------------------------------------------------------------------------
  59. '                                                                                       AlignToVector
  60. '--------------------------------------------------------------------------------------------------------------------------
  61. Function AlignToVector(e:TEntity, x#, y#, z#)
  62.  
  63.         'order=yaw-pitch-roll
  64.        
  65.         Local yaw#,pitch#,roll#
  66.         Local x1#,y1#,z1#
  67.         Local x2#,y2#,z2#
  68.         Local x3#,y3#,z3#
  69.        
  70.         yaw# = -ATan2(x, z)
  71.        
  72.         x1# = z*Sin(yaw) + x*Cos(yaw)
  73.         y1# = y
  74.         z1# = z*Cos(yaw) - x*Sin(yaw)
  75.        
  76.         pitch# = -ATan2(y1, z1)
  77.         x2# = x1
  78.         y2# = y1*Cos(pitch) - z1*Sin(pitch)
  79.         z2# = y1*Sin(pitch) + z1*Cos(pitch)
  80.        
  81.         roll# = -ATan2(x2, y2)
  82.         x3# = x2*Cos(roll) - y2*Sin(roll)
  83.         y3# = x2*Sin(roll) + y2*Cos(roll)
  84.         z3# = z2
  85.  
  86.         'FIX - might turn out it should be If y <= 0 .. haven't tested it thouroughly enough
  87.         If y < 0 roll :+ 180
  88.        
  89.         RotateEntity e, pitch, yaw, roll
  90.        
  91. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal