Ooops
January 26, 2021, 06:30:49 AM

Author Topic: [bb] Object Movement Relative to Camera by xmlspy [ 1+ years ago ]  (Read 1267 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Object Movement Relative to Camera
Author : xmlspy
Posted : 1+ years ago

Description : The code shows movement of an object relative to the camera's position using RotateEntity.

Forward is forward as you look at it.


Code :
Code: BlitzBasic
  1. ;April 22, 2006
  2. Gosub setup
  3.  
  4. ;Main Loop
  5. Repeat
  6.         Gosub GetMKeys
  7.         Gosub MoveCube
  8.        
  9.         TurnEntity cam_pivot, 0, .1, 0
  10.        
  11.         If KeyHit(57) Then
  12.                 If relative = 1 Then relative = 0 Else relative = 1
  13.         EndIf
  14.        
  15.         RenderWorld()
  16.         Text 0, 0, "AWSD, Right Mouse, Spacebar"
  17.         Text 0, 20, "Relative: " + relative
  18.         Flip
  19.         Gosub FlushMKeys
  20. Until KeyHit(1)
  21. End
  22.  
  23. .setup
  24.         Graphics3D 640, 480, 0, 2
  25.         AppTitle "Object Movement Relative to Camera"
  26.         cam_pivot = CreatePivot()
  27.         cam = CreateCamera(cam_pivot)
  28.         MoveEntity cam, 0, 20, -20
  29.        
  30.         cube = CreateCube()
  31.         MoveEntity cube, 0, 0, 0
  32.        
  33.         p = CreatePlane()
  34.         Gosub maketexture
  35.         MoveEntity p, 0, -1, 0
  36.        
  37.         center = CreatePivot()
  38.         PointEntity cam,cube
  39.        
  40.         nc = CreateCube()
  41.         EntityColor nc, 0, 0, 255
  42.         MoveEntity nc, 0, 0, 20
  43.         sc = CreateCube()
  44.         EntityColor sc, 0, 255, 0
  45.         MoveEntity sc, 0, 0, -20
  46.         relative = 1
  47. Return
  48.  
  49. .maketexture
  50.         Tex=CreateTexture( 256,256 )  : SetBuffer TextureBuffer( Tex )
  51.         Color 255, 0, 0 : Rect 0, 0, 256, 256
  52.         Color 0, 255, 0 : Rect 0, 0, 256, 256, 0
  53.         ScaleTexture Tex, 128,128 : EntityTexture p,Tex
  54.         SetBuffer BackBuffer() : Color 255, 255, 255
  55. Return
  56.  
  57. .GetMKeys
  58.         MOVELEFT = KeyDown(203) Or KeyDown(30)
  59.         MOVERIGHT = KeyDown(205) Or KeyDown(32)
  60.         MOVEUP = KeyDown(200) Or KeyDown(17)
  61.         MOVEDOWN = KeyDown(208) Or KeyDown(31)
  62. Return
  63.  
  64. .FlushMKeys
  65.         MOVELEFT = 0 : MOVERIGHT = 0
  66.         MOVEUP = 0 : MOVEDOWN = 0
  67.         If EntityDistance(cube,center) > 40 PositionEntity cube, 0, 0, 0
  68. Return
  69.  
  70. .MoveCube
  71.         If MOVELEFT a=90
  72.         If MOVERIGHT a=-90
  73.         If MOVEUP a=0
  74.         If MOVEDOWN a=180
  75.        
  76.         If MOVELEFT And MOVEUP Then
  77.                 a=45
  78.         ElseIf MOVELEFT And MOVEDOWN
  79.                 a=135
  80.         EndIf
  81.        
  82.         If MOVERIGHT And MOVEUP Then
  83.                 a=-45
  84.         ElseIf MOVERIGHT And MOVEDOWN
  85.                 a=-135
  86.         EndIf
  87.        
  88.         ;Here is the trick of the whole movement of object relative to camera
  89.         ;it is actually not with camera, but it's pivot
  90.         If relative
  91.                 RotateEntity cube, 0, a + EntityYaw(cam_pivot), 0
  92.         Else
  93.                 RotateEntity cube, 0, a, 0
  94.         EndIf
  95.         If MOVELEFT Or MOVERIGHT Or MOVEUP Or MOVEDOWN Then
  96.                 MoveEntity cube, 0, 0, 1
  97.         EndIf
  98. Return


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal