| x#,y#,z# - the direction vector to measure |
|
Returns the pitch angle, in degrees, that a direction vector is pointing. This is the up-and-down partner to VectorYaw. Hand it a direction - the gap between an anti-aircraft gun and a plane, the velocity of an arrow in flight, the normal of the slope under a car - and it tells you how far above or below the horizontal that direction lies. Put the two together and RotateEntity turret,VectorPitch(dx,dy,dz),VectorYaw(dx,dy,dz),0 aims an entity straight down a vector in a single call. The sign is the thing to remember: up is negative. Straight up, (0,1,0), gives -90; straight down gives 90; anything level gives 0, and a 45 degree climb like (0,1,1) reads -45. That matches RotateEntity and EntityPitch, where a negative pitch tilts an entity's nose upward, so the numbers drop straight into a rotation without a sign flip. Only the height against the horizontal distance matters, so scaling the whole vector changes nothing, and a zero-length vector reads 0. Because it is an absolute angle rather than a difference, it is the right tool for "point at this" and the wrong one for "turn a bit towards this". For gradual tracking - a missile that only turns so fast, a camera that eases onto a target - use DeltaPitch, which measures the remaining angle from where the entity is already facing. See also: VectorYaw, DeltaPitch, EntityPitch, AlignToVector, PointEntity. |
; VectorPitch Example
; -------------------
Graphics3D 640,480,0,2
SetBuffer BackBuffer()
camera=CreateCamera()
PositionEntity camera,0,4,-14
RotateEntity camera,8,0,0
light=CreateLight()
RotateEntity light,45,45,0
ground=CreatePlane()
EntityTexture ground,LoadTexture("media/MossyGround.BMP")
; A turret that has to tilt as well as turn
turret=CreateCube()
PositionEntity turret,0,1,0
barrel=CreateCylinder(12,True,turret)
ScaleEntity barrel,0.3,1.2,0.3
RotateEntity barrel,90,0,0
PositionEntity barrel,0,0,1.2
; The drone it is tracking, sweeping past and bobbing
drone=CreateSphere(12)
ScaleEntity drone,0.5,0.5,0.5
EntityColor drone,240,120,60
height#=4.0
While Not KeyDown(1)
; [ and ] fly the drone lower and higher
If KeyDown(26) And height>-1 Then height=height-0.05
If KeyDown(27) And height<8 Then height=height+0.05
; Sweep the drone left and right so the aim keeps changing
PositionEntity drone,Sin(MilliSecs()/25.0)*7,height,7
; The direction from the turret to the drone (both sit at the top level,
; so their local coordinates are already world coordinates)
dx#=EntityX(drone)-EntityX(turret)
dy#=EntityY(drone)-EntityY(turret)
dz#=EntityZ(drone)-EntityZ(turret)
; VectorPitch is the up/down half of the aim, VectorYaw the left/right half
pitch#=VectorPitch(dx,dy,dz)
RotateEntity turret,pitch,VectorYaw(dx,dy,dz),0
; Arrow keys move the camera
If KeyDown(200) Then MoveEntity camera,0,0,0.1
If KeyDown(208) Then MoveEntity camera,0,0,-0.1
If KeyDown(203) Then TurnEntity camera,0,1,0
If KeyDown(205) Then TurnEntity camera,0,-1,0
RenderWorld
Text 0,0,"[ / ]: fly the drone up and down Arrows: camera Esc: exit"
Text 0,20,"VectorPitch("+dx+","+dy+","+dz+") = "+pitch
Text 0,40,"RotateEntity turret,pitch,yaw,0 aims the barrel in one go"
Text 0,60,"Up is NEGATIVE pitch: VectorPitch(0,1,0) = "+VectorPitch(0,1,0)
Flip
Wend
End