Full raycast data

This commit is contained in:
bloeys
2022-02-18 04:01:20 +04:00
parent dddb8a65d5
commit 2abb66ec14
2 changed files with 87 additions and 16 deletions

26
main.go
View File

@ -1,20 +1,22 @@
package main package main
import ( import (
"fmt"
"github.com/bloeys/physx-go/pgo" "github.com/bloeys/physx-go/pgo"
) )
func contactHandler(cph pgo.ContactPairHeader) { func contactHandler(cph pgo.ContactPairHeader) {
pairs := cph.GetPairs() // pairs := cph.GetPairs()
for i := 0; i < len(pairs); i++ { // for i := 0; i < len(pairs); i++ {
points := pairs[i].GetContactPoints() // points := pairs[i].GetContactPoints()
for j := 0; j < pairs[i].GetContactPointCount(); j++ { // for j := 0; j < pairs[i].GetContactPointCount(); j++ {
pos := points[j].GetPos() // pos := points[j].GetPos()
println("Contact at pos:", pos.String()) // println("Contact at pos:", pos.String())
} // }
} // }
} }
func main() { func main() {
@ -132,7 +134,13 @@ func main() {
s.Advance() s.Advance()
s.FetchResults(true) s.FetchResults(true)
s.Raycast(pgo.NewVec3(0, 0, 0), pgo.NewVec3(0, 1, 0), 9) _, b := s.Raycast(pgo.NewVec3(0, 0, 0), pgo.NewVec3(0, 1, 0), 9)
if b.HasBlock() {
block := b.GetBlock()
d := block.GetDistance()
pos := block.GetPos()
fmt.Printf("Raycast hit at dist (%v) and post %v\n", d, pos.String())
}
// fmt.Printf("\nRaycast hit: %v\n", rHit) // fmt.Printf("\nRaycast hit: %v\n", rHit)
// println("Press enter...") // println("Press enter...")
// r.ReadBytes('\n') // r.ReadBytes('\n')

View File

@ -127,20 +127,14 @@ func (s *Scene) SetScratchBuffer(multiplesOf16k uint32) {
C.CPxScene_setScratchBuffer(s.cS, C.uint(multiplesOf16k)) C.CPxScene_setScratchBuffer(s.cS, C.uint(multiplesOf16k))
} }
//TODO: Implement
func (s *Scene) Raycast(origin, unitDir *Vec3, distance float32) (bool, RaycastBuffer) { func (s *Scene) Raycast(origin, unitDir *Vec3, distance float32) (bool, RaycastBuffer) {
rb := RaycastBuffer{} rb := RaycastBuffer{}
ret := C.CPxScene_raycast(s.cS, &origin.cV, &unitDir.cV, C.float(distance), &rb.cRb) ret := C.CPxScene_raycast(s.cS, &origin.cV, &unitDir.cV, C.float(distance), &rb.cRb)
// x := unsafe.Slice(rb.cRb.touches, rb.cRb.nbTouches)
return bool(ret), rb return bool(ret), rb
} }
type RaycastHit struct {
cRh *C.struct_CPxRaycastHit
}
type RaycastBuffer struct { type RaycastBuffer struct {
cRb *C.struct_CPxRaycastBuffer cRb *C.struct_CPxRaycastBuffer
} }
@ -150,7 +144,76 @@ func (rb *RaycastBuffer) HasBlock() bool {
} }
func (rb *RaycastBuffer) GetBlock() RaycastHit { func (rb *RaycastBuffer) GetBlock() RaycastHit {
return RaycastHit{} return RaycastHit{
cRh: &rb.cRb.block,
}
}
func (rb *RaycastBuffer) GetnbTouches() int {
return int(rb.cRb.nbTouches)
}
func (rb *RaycastBuffer) GetTouches() []RaycastHit {
hits := make([]RaycastHit, rb.cRb.nbTouches)
touches := unsafe.Slice(rb.cRb.touches, rb.cRb.nbTouches)
for i := 0; i < len(hits); i++ {
hits[i].cRh = &touches[i]
}
return hits
}
type RaycastHit struct {
cRh *C.struct_CPxRaycastHit
}
func (rh *RaycastHit) GetActor() RigidActor {
return RigidActor{
cRa: rh.cRh.actor,
}
}
func (rh *RaycastHit) GetShape() Shape {
return Shape{
cShape: rh.cRh.shape,
}
}
func (rh *RaycastHit) GetDistance() float32 {
return float32(rh.cRh.distance)
}
func (rh *RaycastHit) GetFaceIndex() uint {
return uint(rh.cRh.faceIndex)
}
func (rh *RaycastHit) GetHitFlags() HitFlag {
return HitFlag(rh.cRh.flags)
}
func (rh *RaycastHit) GetNormal() gglm.Vec3 {
return gglm.Vec3{
Data: [3]float32{
float32(rh.cRh.normal.x),
float32(rh.cRh.normal.y),
float32(rh.cRh.normal.z),
},
}
}
func (rh *RaycastHit) GetPos() gglm.Vec3 {
return gglm.Vec3{
Data: [3]float32{
float32(rh.cRh.position.x),
float32(rh.cRh.position.y),
float32(rh.cRh.position.z),
},
}
}
func (rh *RaycastHit) GetUV() (float32, float32) {
return float32(rh.cRh.u), float32(rh.cRh.v)
} }
type Physics struct { type Physics struct {