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
import (
"fmt"
"github.com/bloeys/physx-go/pgo"
)
func contactHandler(cph pgo.ContactPairHeader) {
pairs := cph.GetPairs()
for i := 0; i < len(pairs); i++ {
// pairs := cph.GetPairs()
// for i := 0; i < len(pairs); i++ {
points := pairs[i].GetContactPoints()
for j := 0; j < pairs[i].GetContactPointCount(); j++ {
pos := points[j].GetPos()
println("Contact at pos:", pos.String())
}
}
// points := pairs[i].GetContactPoints()
// for j := 0; j < pairs[i].GetContactPointCount(); j++ {
// pos := points[j].GetPos()
// println("Contact at pos:", pos.String())
// }
// }
}
func main() {
@ -132,7 +134,13 @@ func main() {
s.Advance()
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)
// println("Press enter...")
// r.ReadBytes('\n')

View File

@ -127,20 +127,14 @@ func (s *Scene) SetScratchBuffer(multiplesOf16k uint32) {
C.CPxScene_setScratchBuffer(s.cS, C.uint(multiplesOf16k))
}
//TODO: Implement
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)
// x := unsafe.Slice(rb.cRb.touches, rb.cRb.nbTouches)
return bool(ret), rb
}
type RaycastHit struct {
cRh *C.struct_CPxRaycastHit
}
type RaycastBuffer struct {
cRb *C.struct_CPxRaycastBuffer
}
@ -150,7 +144,76 @@ func (rb *RaycastBuffer) HasBlock() bool {
}
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 {