Hology
Hology 文檔
Hology 文檔
  • 👋歡迎來到Hology文檔
  • Getting started
    • Hology Engine簡介
    • 第一步指南
    • 編輯器基礎指南
      • 在場景中飛行
      • 放置物件
      • 選擇物件
      • 變換
      • 分組物件
      • 複製
    • 入門遊戲模板 - 第三人稱射擊遊戲
  • Tutorials
    • 滾球 - 遊戲玩法編程
    • 角色動畫程式設計
    • Character AI behavior
  • Release
    • 遊戲發行
      • Discord Activities
      • 臉書即時遊戲
      • 上傳至 Itch.io
      • 使用 GitHub Pages
      • 發布到Steam
      • iOS 和 Android
  • Assets
    • 3D模型
      • 客製化碰撞形狀
      • 材質自訂分配
    • 材質
    • 紋理
    • 預製體
  • Gameplay
    • 演員(Actors)
      • 創建演員類
      • 演員參數
      • Actor 元件
      • Actor 的生命週期
      • 生成 Actors
      • 移動 Actors
    • 服務
      • 載入資產
    • Player input
    • Collision detection
    • Physics
      • Physics body types
      • Applying forces
      • Ray casting
    • Trigger volumes
    • Character movement
    • Pointer events
    • Animation
      • Animation system
      • Character Animation
      • Animation State Machine
    • Sound
      • Audio parameter
    • 世界
    • Navigation
  • Shaders
    • 著色器介紹
    • Creating shaders
    • 著色器參數
    • Typescript shaders
      • Types
      • Math functions
      • Attributes
      • Varying
      • Uniforms
      • Textures
      • Arrays
      • Select
      • Lighting
    • Painted materials
    • Water shader tutorial
  • Level design
    • 地形雕刻
    • 地形繪製
    • Grass
  • User Interfaces
    • 創建用戶界面UI
    • 使用 React
    • VR
  • Visual Effects
    • 視覺效果簡介
    • 視覺特效資產
  • Integrations
    • Arcweave
由 GitBook 提供支持
在本页
  • Ray test
  • Parameters
  • RayTestResult
  • RayTestOptions
  1. Gameplay
  2. Physics

Ray casting

Ray casting is a technique to identify collisions of a ray from one point to another in 3D space. This can be useful to for example test if a character is in line of sight of another character or get the hit point of a bullet shot from a gun.

Ray test

The physics system has a method called rayTest which allows you to calculate collisions of a ray between two points.

import { Service, inject, PhysicsSystem, RayTestResult } from "@hology/core/gameplay"
import { Vector3 } from "three"

@Service()
class ExampleService {
    private physics = inject(PhysicsSystem)
    
    checkCollisions() {
        const result = new RayTestResult()
        const from = new Vector3(0,0,0)
        const to = new Vector3(0,0,100)
        this.physics.rayTest(from, to, result)
        
        console.log(result)
    }
}

export default ExampleService

Parameters

  • from: Vector3 - The starting point of the ray

  • to: Vector3 - The ending point of the ray.

  • result: RayTestResult - An object in which to store the result of the ray. If you are doing a lot of ray casting, it is often best to reuse the RayTestResult object to avoid garbage collection and improving the performance of your code.

  • options: RayTestOptions - Options to change the behaviour or debug of the ray testing.

RayTestResult

  • hasHit: boolean - Whether the ray hit an object.

  • hitPoint: Vector3 - The point in global space in which the ray hit

  • hitNormal: Vector3 - The normal of the surface hit

  • distance: number - The distance to the hit point from the start of the ray.

  • actor: BaseActor - If the hit object is an actor, this field will reference the actor instance.

RayTestOptions

  • debugColor: ColorRepresentation - If you have enabled the debug mode of the physics system, it will show an arrow for the ray. Use this option to change the color of the arrow to more easily distinguish it from others.

  • debugLifetime: number - How many milliseconds the arrow should be visible. If you are doing a lot of raytesting.

  • excludeActor: BaseActor - It is sometimes necessary to exclude some actor from collisions. Often it could be the actor from which the ray should be starting from to avoid colliding with the actor itself.

上一页Applying forces下一页Trigger volumes