Results 1 to 10 of 179

Thread: nxPascal

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #16
    You can do sphere-plane collision with nxPascal. I'll explain the math using nxMath3D:

    Code:
    function RayPlaneIntersect(const rayOrigin, rayDirection,
      planeOrigin, planeNormal: TVector; intersection: PVector): single;
    First imagine that the sphere casts a ray directly towards the plane. rayDirection is inverse of planeNormal (or we could use equal too, it might not matter). If function result is >= 0, we get value stored in intersection vector. This means nearest point on the plane towards sphere's center.

    Next we need to check if distance between intersection point and sphere is smaller than sphere radius. For that we could use Hypot3D(), but unless you plan to do something with the distance (such as manual physics), it may do more than you need. For simplicity's sake, you could place sphere back to where it was before collision. So instead of Hypot3D(), it will be faster to use PointInSphere():
    Code:
    function PointInSphere(const p,sphere: TVector; const radius: single): boolean;
    PointInSphere is a truncated Hypot3D() without sqrt() call, and only tells if it collides or not.

    Finally assuming that the sphere is moving in some direction, it will bounce off to other direction from the plane.
    Code:
    function Reflect(const rayStart, rayDir, wallPoint: TVector): TVector;
    RayStart = Sphere current position.
    RayDir = Sphere movement direction.
    wallPoint = Plane intersection point calculated above.
    Function gives as a result the new movement direction for sphere.
    Last edited by User137; 22-08-2013 at 08:23 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •