Start raycast implementation

This commit is contained in:
bloeys
2022-02-16 08:17:49 +04:00
parent d118b82ce6
commit d143660a1f
5 changed files with 65 additions and 0 deletions

11
main.go
View File

@ -1,6 +1,10 @@
package main
import (
"bufio"
"fmt"
"os"
"github.com/bloeys/physx-go/pgo"
)
@ -111,12 +115,19 @@ func main() {
println("Capsule linear damping B:", dynCapsule.GetLinearDamping())
//Run simulation
r := bufio.NewReader(os.Stdin)
s.SetScratchBuffer(4)
for {
s.Collide(1 / 60.0)
s.FetchCollision(true)
s.Advance()
s.FetchResults(true)
rHit, _ := s.Raycast(pgo.NewVec3(0, 0, 0), pgo.NewVec3(0, 1, 0), 9)
fmt.Printf("\nRaycast hit: %v\n", rHit)
// println("Press enter...")
// r.ReadBytes('\n')
}
p.Release()

Binary file not shown.

View File

@ -126,6 +126,30 @@ func (s *Scene) SetScratchBuffer(multiplesOf16k uint32) {
C.CPxScene_setScratchBuffer(s.cS, C.uint(multiplesOf16k))
}
//bool CPxScene_raycast(CSTRUCT CPxScene* cs, CPxVec3* origin, CPxVec3* unitDir, CPxReal distance, CPxRaycastBuffer* hit)
func (s *Scene) Raycast(origin, unitDir *Vec3, distance float32) (bool, RaycastBuffer) {
rb := RaycastBuffer{}
ret := C.CPxScene_raycast(s.cS, &origin.cV, &unitDir.cV, C.float(distance), &rb.cRb)
return bool(ret), rb
}
type RaycastHit struct {
cRh *C.struct_CPxRaycastHit
}
type RaycastBuffer struct {
cRb *C.struct_CPxRaycastBuffer
}
func (rb *RaycastBuffer) HasBlock() bool {
return bool(rb.cRb.hasBlock)
}
func (rb *RaycastBuffer) GetBlock() RaycastHit {
return RaycastHit{}
}
type Physics struct {
cPhysics *C.struct_CPxPhysics
}

View File

@ -3,11 +3,39 @@
#include "CPxPvdSceneClient.h"
#include "CPxActor.h"
#include "CPxVec3.h"
#include "CPxShape.h"
#include "CPxRigidActor.h"
#ifdef __cplusplus
extern "C" {
#endif
struct CPxRaycastHit
{
//CPxHitFlags flags; //!< Hit flags specifying which members contain valid values.
struct CPxVec3 position; //!< World-space hit position (flag: #PxHitFlag::ePOSITION)
struct CPxVec3 normal; //!< World-space hit normal (flag: #PxHitFlag::eNORMAL)
/**
\brief Distance to hit.
\note If the eMTD flag is used, distance will be a negative value if shapes are overlapping indicating the penetration depth.
\note Otherwise, this value will be >= 0 */
CPxF32 distance;
CPxReal u, v;
CPxU32 faceIndex;
struct CPxShape shape;
struct CPxRigidActor actor;
};
struct CPxRaycastBuffer
{
struct CPxRaycastHit block;
struct CPxRaycastHit* touches;
CPxU32 nbTouches;
bool hasBlock;
};
struct CPxScene
{
void* obj;
@ -22,6 +50,7 @@ extern "C" {
CPxAPI bool CPxScene_fetchCollision(CSTRUCT CPxScene*, bool block);
CPxAPI void CPxScene_advance(CSTRUCT CPxScene*);
CPxAPI bool CPxScene_fetchResults(CSTRUCT CPxScene*, bool block, CPxU32* errorState);
CPxAPI bool CPxScene_raycast(CSTRUCT CPxScene* cs, CSTRUCT CPxVec3* origin, CSTRUCT CPxVec3* unitDir, CPxReal distance, CSTRUCT CPxRaycastBuffer** hitRet);
/// <summary>
/// Creates a scratch buffer thats a multiple of 16K to be used by the scene when running CPxScene_simulate.

View File

@ -7,6 +7,7 @@
#define CENUM enum
#define CPxU32 uint32_t
#define CPxReal float
#define CPxF32 float
#include <CPxFoundation.h>
#include <CPxPvd.h>