Jump to content

Raycast Performance


Recommended Posts

Hey guys... got a couple of quick questions about raycasting...

1... Is recasting an 'Expensive Thing' for BabylonJS. I ask because in unity, a fully scripted player character might by casting 6  - 10 rays on each frame...

One ray might be be checking ground distance... Four separate rays might be casting to the four near clip plane points of the camera to check for Collision and or occlusion and one ray might checking wall distance ... and so on... Now that apparently is FINE in Unity on a native platform... 

Would that many rays be TOO MUCH for a BabylonJS Game... ???

2... Is is possible to create a "Sphere Cast" type functionality like Unity Has... Example:


public static bool SphereCast(Vector3 origin, float radius, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

origin	The center of the sphere at the start of the sweep.
radius	The radius of the sphere.
direction	The direction into which to sweep the sphere.
hitInfo	If true is returned, hitInfo will contain more information about where the collider was hit. (See Also: RaycastHit).
maxDistance	The max length of the cast.
layerMask	A Layer mask that is used to selectively ignore colliders when casting a capsule.
queryTriggerInteraction	Specifies whether this query should hit Triggers.

bool True when the sphere sweep intersects any collider, otherwise false.


Casts a sphere along a ray and returns detailed information on what was hit.

This is useful when a Raycast does not give enough precision, because you want to find out if an object of a specific size, such as a character, will be able to move somewhere without colliding with anything on the way. Think of the sphere cast like a thick raycast. In this case the ray is specified by a start vector and a direction.

Notes: SphereCast will not detect colliders for which the sphere overlaps the collider. Passing a zero radius results in undefined output and doesn't always behave the same as Physics.Raycast.

See Also: Physics.SphereCastAll, Physics.CapsuleCast, Physics.Raycast, Rigidbody.SweepTest.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
    CharacterController charCtrl;

    void Start()
        charCtrl = GetComponent<CharacterController>();

    void Update()
        RaycastHit hit;

        Vector3 p1 = transform.position + charCtrl.center;
        float distanceToObstacle = 0;

        // Cast a sphere wrapping character controller 10 meters forward
        // to see if it is about to hit anything.
        if (Physics.SphereCast(p1, charCtrl.height / 2, transform.forward, out hit, 10))
            distanceToObstacle = hit.distance;

 Yo @Deltakosh ... Gotta ping you in here ???


Link to comment
Share on other sites


1. It depends on the scene ;) but 6 rays seems acceptable. Perhaps we can find even faster way depending on the final goal

2. We support sphere vs spehre culling: https://github.com/BabylonJS/Babylon.js/blob/master/src/Culling/babylon.boundingSphere.ts#L50 or sphere vs point: https://github.com/BabylonJS/Babylon.js/blob/master/src/Culling/babylon.boundingSphere.ts#L36 or box vs sphere: https://github.com/BabylonJS/Babylon.js/blob/master/src/Culling/babylon.boundingBox.ts#L127

With all these tools you should be able to find what you need :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...