camera on robot arm

Blitz3D Forums/Blitz3D Programming/camera on robot arm

shawnus(Posted 2003) [#1]
I am trying to stick a second camera onto the end of the robot arm (Reda Borchardt’s open dna tutorial) object. I thought the best way would be to use the wrist’s global xyz co-ordinates to determine the second camera’s global xyz position, but I can’t get it to work. I would also like to be able to point the camera at the centre (0,0,0), and also free the camera to point in a specific direction. Any help would be appreciated.

Here’s the code:

; Fully Articulated Robotic Arm
; By Reda Borchardt

Include "start.bb"
Include "keyconstants.bb"
message=1
banner = LoadImage("top-banner.jpg")
MaskImage banner,255,255,0

; Load the object and assign a handle to all sub-objects
robotarm = LoadAnimMesh("ROBOTIC-ARM.3DS")
stand = FindChild(robotarm,"stand")
upperarm = FindChild(robotarm,"upperarm")
lowerarm = FindChild(robotarm,"lowerarm")
wrist = FindChild(robotarm,"wrist")
clamp = FindChild(robotarm,"clamp")

; Add Some Light
AmbientLight 0,0,255
light = CreateLight(1)
PositionEntity light,40,20,-30

; Load The Camera And Put It Into A Pivot With Two Axes
horizontal_camera_pivot = CreatePivot()
vertical_camera_pivot = CreatePivot(horizontal_camera_pivot)
camera = CreateCamera(vertical_camera_pivot)

camera2pivot=CreatePivot();''''''''''''''''''this''''''''''''''''''''''''''''''''''''''''''''''''''
camera2=CreateCamera(camera2pivot);''''''''''is'''''''''''''''''''''''''''''''''''''''''''''''''''
CameraViewport camera2,50,50,200,200;''''''''my'''''''''''''''''''''''''''''''''''''''''''''''''''
PointEntity camera2,horizontal_camera_pivot;'effort'''''''''''''''''''''''''''''''''''''''''''''''

; Put The Camera Somewhere And Resize The Model To A Reasonable Size
ScaleEntity robotarm,0.1,0.1,0.1
MoveEntity robotarm,0,-10,0
PositionEntity camera,0,10,-40
PointEntity camera,horizontal_camera_pivot


While Not KeyHit(1)

If KeyDown(key_q) Then TurnEntity stand,0,0.3,0
If KeyDown(key_a) Then TurnEntity stand,0,-0.3,0
If KeyDown(key_w) Then TurnEntity upperarm,0,0,0.3
If KeyDown(key_s) Then TurnEntity upperarm,0,0,-0.3
If KeyDown(key_e) Then TurnEntity lowerarm,0,0,-0.3
If KeyDown(key_d) Then TurnEntity lowerarm,0,0,0.3
If KeyDown(key_r) Then TurnEntity wrist,0,0,0.3
If KeyDown(key_f) Then TurnEntity wrist,0,0,-0.3
If KeyDown(key_t) Then TurnEntity clamp,0.3,0,0
If KeyDown(key_g) Then TurnEntity clamp,0.3,0,0

If KeyDown(key_arrowpad_up) Then TurnEntity vertical_camera_pivot,0.3,0,0
If KeyDown(key_arrowpad_down) Then TurnEntity vertical_camera_pivot,-0.3,0,0
If KeyDown(key_arrowpad_left) Then TurnEntity horizontal_camera_pivot,0,-0.3,0
If KeyDown(key_arrowpad_right) Then TurnEntity horizontal_camera_pivot,0,0.3,0

loc_x#=EntityX#(wrist):loc_y#=EntityY#(wrist):loc_z#=EntityZ#(wrist);'''''''''i tried this

PointEntity camera,horizontal_camera_pivot
PointEntity camera2,horizontal_camera_pivot;''''''''''''''''and this

PositionEntity camera2,loc_x#,loc_y#,loc_z#;'''''''''''''to position the camera



UpdateWorld
RenderWorld

DrawImage banner,0,0

If KeyHit(Key_h) Then
If message=0 Then message =1
If message=1 Then message =0
End If

If KeyHit(key_z) Then
WireMode = Not WireMode
WireFrame WireMode
End If

If message=1 Then

Text 0,100,"Robotic Arm"
Text 0,120,"Keys: Use Keys Q to T and A to G"
Text 0,140,"Use The Arrow Keys To Move The Camera"
Text 0,180,"Keys: 'Z' to toggle wireframe mode"
Text 0,200,"Keys: H to hide this message"

End If

Flip
Wend


DJWoodgate(Posted 2003) [#2]
Something like this maybe. Creates another pivot attached to the clamp in this case and adjusted for position and orientation. Spacebar lets you free the camera and fly around with the mouse. Note I don't actually attach the camera to the pivot, could do but it usually involves some tricky scale problems when working with scaled hierarchies.

; Fully Articulated Robotic Arm 
; By Reda Borchardt 

Include "start.bb" 
Include "keyconstants.bb" 
message=1 
banner = LoadImage("top-banner.jpg") 
MaskImage banner,255,255,0 

; Load the object and assign a handle to all sub-objects 
robotarm = LoadAnimMesh("ROBOTIC-ARM.3DS") 
stand = FindChild(robotarm,"stand") 
upperarm = FindChild(robotarm,"upperarm") 
lowerarm = FindChild(robotarm,"lowerarm") 
wrist = FindChild(robotarm,"wrist") 
clamp = FindChild(robotarm,"clamp")
cpivot=CreatePivot(clamp)
; create a pivot to attach our camera to, and rotate it to point correctly
PositionEntity cpivot,-20,-15,0
RotateEntity cpivot,0,90,180

; Add Some Light 
AmbientLight 0,0,255 
light = CreateLight(1) 
TurnEntity light,60,60,0 

; Load The Camera And Put It Into A Pivot With Two Axes 
horizontal_camera_pivot = CreatePivot() 
vertical_camera_pivot = CreatePivot(horizontal_camera_pivot) 
camera = CreateCamera(vertical_camera_pivot) 

camera2=CreateCamera()
CameraRange camera2,0.1,200
CameraZoom camera2,2 
CameraViewport camera2,0,GraphicsHeight()-200,200,200

; simple room
cube=CreateCube()
EntityColor cube,200,200,80
FitMesh cube,-50,0,-50,100,50,100
FlipMesh cube

; some objects
SeedRnd 500
For i=0 To 10
box=CreateCube()
FitMesh box,-2,0,-2,4,4,4
PositionEntity box,Rand(-40,40),0,Rand(-40,40)
TurnEntity box,0,Rand(-180,180),0
EntityColor box,200,200,0
Next

; Put The Camera Somewhere And Resize The Model To A Reasonable Size 
ScaleEntity robotarm,0.1,0.1,0.1 
PositionEntity camera,0,30,-40 
PointEntity camera,horizontal_camera_pivot 

spd#=1
camspd#=0.25

While Not KeyHit(1)

mxs#=MouseXSpeed()
mys#=MouseYSpeed()
MoveMouse GraphicsWidth()/2,GraphicsHeight()/2 

spd=spd#+(KeyHit(13)-KeyHit(12))*0.1 If spd<0.1 Then spd=0.1

If KeyDown(key_q) Then TurnEntity stand,0,spd,0 
If KeyDown(key_a) Then TurnEntity stand,0,-spd,0 
If KeyDown(key_w) Then TurnEntity upperarm,0,0,spd 
If KeyDown(key_s) Then TurnEntity upperarm,0,0,-spd 
If KeyDown(key_e) Then TurnEntity lowerarm,0,0,-spd 
If KeyDown(key_d) Then TurnEntity lowerarm,0,0,spd
If KeyDown(key_r) Then TurnEntity wrist,0,0,spd
If KeyDown(key_f) Then TurnEntity wrist,0,0,-spd 
If KeyDown(key_t) Then TurnEntity clamp,spd,0,0 
If KeyDown(key_g) Then TurnEntity clamp,-spd,0,0 

If KeyDown(key_arrowpad_up) Then TurnEntity vertical_camera_pivot,spd,0,0 
If KeyDown(key_arrowpad_down) Then TurnEntity vertical_camera_pivot,-spd,0,0 
If KeyDown(key_arrowpad_left) Then TurnEntity horizontal_camera_pivot,0,-spd,0 
If KeyDown(key_arrowpad_right) Then TurnEntity horizontal_camera_pivot,0,spd,0 

PointEntity camera,horizontal_camera_pivot

If KeyHit(57) Then fly=Not fly ; spacebar

If fly Then
	If MouseDown(1) Then
		MoveEntity camera2,mxs*camspd,0,-mys*camspd
	Else 
		TurnEntity camera2,-mys*camspd,0,0,False
		TurnEntity camera2,0,-mxs*camspd,0,True
	EndIf
Else
	PositionEntity camera2,EntityX(cpivot,1),EntityY(cpivot,1),EntityZ(cpivot,1)
	RotateEntity camera2,EntityPitch(cpivot,1),EntityYaw(cpivot,1),EntityRoll(cpivot,1)
EndIf

UpdateWorld 
RenderWorld 

DrawImage banner,0,0
; delineate 2nd camera screen 
Rect 0,GraphicsHeight()-200,200,200,0

If KeyHit(Key_h) message=Not message 

If KeyHit(key_z) Then 
WireMode = Not WireMode 
WireFrame WireMode 
End If 

If message Then 

Text 0,100,"Robotic Arm" 
Text 0,120,"Keys: Use Keys Q to T and A to G" 
Text 0,140,"Use The Arrow Keys To Move The Camera" 
Text 0,160,"Use +/- keys to change speed"
Text 0,180,"Keys: 'Z' to toggle wireframe mode" 
Text 0,200,"Keys: H to hide/show this message" 

End If 

Flip 
Wend