Raycasting

From Leadwerks Developer Wiki

Jump to: navigation, search

Contents

Description

The pick commands can be used to perform raycasts. Although picks are similar to collision, and the picking system even borrows the collision types system, it is important to make a distinction between the two: Picks work on meshes and terrain. Collisions work on bodies and terrain.

The collision type parameter works as follows: Imagine the pick-ray being a physical body with the specified collision type that flies in the pick direction. It'll stop at the first mesh it hits that has a collision type which would collide with it's type. Therefore, for the pick type parameter to work, Collisions must have been used to set up any type (response type doesn't matter) of collision between the pick's type and the mesh's type. So after calling (for example) Collsions(1,2), a pick of type 2 will hit meshes of type 1 and the other way around.

Raycast radii are not supported at this time.

Tutorials

Collision and Raycasting

Commands

Visibility Tests

PointVisible

  • C: int PointVisible(TVec3 &point1, TVec3 &point2, flt radius=0, int collisionType=0, BP pickfilter=NULL)
  • C++: bool Vector3::IsVisible( const TVec3& p1, const TVec3& p2, flt radius, int collisionType, Filter filter = 0 )
  • BlitzMax: PointVisible%( p1:TVec4, p2:TVec3 [, collisionType=0 ] )
  • Pascal: function PointVisible ( const point1:TVec3; const point2:TVec3; radius:Single=0; collisionType:Integer=0; pickfilter:Pointer=nil ): Integer;
Returns 1 is the specified points have a clear line of sight, else 0 is returned. The collisionType parameter can be used to control what collision types affect the ray test. If collisionType is 0 all pickable entities will affect the ray test.
[Examples]

EntityVisible

  • C: int EntityVisible(TEntity entity1, TEntity entity2, flt radius=0, int collisionType=0, BP pickfilter=NULL)
  • C++: bool Entity::IsVisible( const Entity& ent, flt radius = 0, int collisionType = 0, Filter filter = 0 ) const
  • BlitzMax: EntityVisible%( entity1:TEntity, entity2:TEntity [, radius#=0.0, collisionType=0, Filter:Int(entity:TEntity)=Null ] )
  • Pascal: function EntityVisible ( entity1:THandle; entity2:THandle; radius:Single=0; collisionType:Integer=0; pickfilter:Pointer=nil ): Integer;
Returns 1 is the specified entities have a clear line of sight, else 0 is returned. The collisionType parameter can be used to control what collision types affect the ray test. If collisionType is 0 all pickable entities will affect the ray test.
[Examples]

Picking Commands

LinePick

  • C: int LinePick(TPick *pick, TVec3 &point1, TVec3 &point2, flt radius=0, int collisionType=0, BP pickfilter=NULL)
  • C++: bool Draw::GetPick( Pick& pick, const TVec3& point1, const TVec3& point2, flt radius = 0 , const CollisionType& type = 0, *Byte filter = 0 ) const
  • BlitzMax: LinePick:TPick( p1:TVec3, p2:TVec3 [, radius#=0.0, collisionType=0, Filter:Int(entity:TEntity)=Null ] )
  • Pascal: function LinePick ( var pick:TPick; const point1:TVec3; const point2:TVec3; radius:Single=0; collisionType:Integer=0; pickfilter:Pointer=nil ): Integer;
Performs a ray test from point 1 to 2, and returns the closest collision. If nothing is hit, Null will be returned. The collisionType parameter can be used to control what collision types affect the ray test. If collisionType is 0 all pickable entities will affect the ray test.
[Examples]

EntityPick

  • C: int EntityPick(TPick *pick, TEntity entity, flt range=100, flt radius=0, int collisionType=0, BP pickfilter=NULL)
  • BlitzMax: EntityPick:TPick( entity:TEntity, range# [, radius#=0.0, collisionType=0, Filter:Int(entity:TEntity)=Null ] )
  • Pascal: function EntityPick ( var pick:TPick; entity:THandle; range:Single=100; radius:Single=0; collisionType:Integer=0; pickfilter:Pointer=nil ): Integer;
Performs a ray test in front of the specified entity, and returns the closest collision. If nothing is hit, Null will be returned. The collisionType parameter can be used to control what collision types affect the ray test. If collisionType is 0 all pickable entities will affect the ray test.
[Examples]

CameraPick

  • C: int CameraPick(TPick *pick, TCamera camera, TVec3 &position, flt radius=0, int collisionType=0, BP pickfilter=NULL)
  • C++: bool Camera::GetPick( Pick& pick, const TVec3& pos, flt radius = 0.0f, const CollisionType& type = 0, *Byte filter = 0 ) const
  • BlitzMax: CameraPick:TPick( camera:TCamera, p:TVec3 [, radius#=0.0, collisionType=0, Filter:Int(entity:TEntity)=Null ] )
  • Pascal: function CameraPick ( var pick:TPick; camera:THandle; const position:TVec3; radius:Single=0; collisionType:Integer=0; pickfilter:Pointer=nil ): Integer;
Performs a ray test from the specified camera screen position, and returns the closest collision. If nothing is hit, Null will be returned. The collisionType parameter can be used to control what collision types affect the ray test. If collisionType is 0 all pickable entities will affect the ray test. The z component of the position will specify how far the ray should be cast.
[Examples]

General Info

TPick

  • C:
struct TPick
{
	TEntity entity;
	TEntity surface; // This may be null if the picked entity is not a mesh (e.g. a terrain).
	flt X, Y, Z;
	flt NX, NY, NZ;
	int triangle; // the number of the picked triangle
}
  • C++:
struct Pick : public TPick
{
      TVec3 GetPosition( void ) const; 
      TVec3 GetNormal( void ) const;
}
  • BlitzMax:
Type TPick
	Field entity:TEntity
	Field surface:TSurface
	Field x#,y#,z#
	Field nx#,ny#,nz#
EndType
  • Pascal:
TPick  = packed record 
  entity, surface : THandle; 
  X,Y,Z : single; 
  NX,NY,NZ : single; 
end;


Filter

The optional Filter() [BP pickfilter argument] callback can be used for precise control of which entities are pickable. Return 1 to proceed with a pick test or 0 to skip.

  • C:
int _stdcall Filter( TEntity entity ) 
{
	return (/*entity should be tested*/) ? 1 : 0;
}
  • BlitzMax:
Function Filter:Int(entity:TEntity)
 
EndFunction
  • Pascal:
function Filter ( entity:THandle ) : integer; stdcall;
begin 
end;