Added ray cast functions
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,4 @@
|
||||
case FN_Fprint: //Sub Procedure
|
||||
case FN_FPrint: //Sub Procedure
|
||||
rc_fprint( FPRINT_TXT$ );
|
||||
break;
|
||||
case FN_Input$: //String Function
|
||||
@@ -1646,6 +1646,15 @@ case FN_SetWorld2DAutoClearForces: //Sub Procedure
|
||||
case FN_GetWorld2DAutoClearForces: //Number Function
|
||||
rc_push_num(rc_getWorld2DAutoClearForces( ));
|
||||
break;
|
||||
case FN_CastRay2D: //Number Function
|
||||
rc_push_num(rc_castRay2D( CASTRAY2D_FROM_X, CASTRAY2D_FROM_Y, CASTRAY2D_TO_X, CASTRAY2D_TO_Y ));
|
||||
break;
|
||||
case FN_CastRay2D_All: //Number Function
|
||||
rc_push_num(rc_castRay2D_All( CASTRAY2D_ALL_FROM_X, CASTRAY2D_ALL_FROM_Y, CASTRAY2D_ALL_TO_X, CASTRAY2D_ALL_TO_Y ));
|
||||
break;
|
||||
case FN_GetRayHit2D: //Sub Procedure
|
||||
rc_getRayHit2D( GETRAYHIT2D_INDEX, &GETRAYHIT2D_SPR_ID, &GETRAYHIT2D_X, &GETRAYHIT2D_Y, &GETRAYHIT2D_NORMAL_X, &GETRAYHIT2D_NORMAL_Y );
|
||||
break;
|
||||
case FN_createDistanceJoint: //Number Function
|
||||
rc_push_num(rc_createDistanceJoint( CREATEDISTANCEJOINT_SPRITEA, CREATEDISTANCEJOINT_SPRITEB, CREATEDISTANCEJOINT_AX, CREATEDISTANCEJOINT_AY, CREATEDISTANCEJOINT_BX, CREATEDISTANCEJOINT_BY, CREATEDISTANCEJOINT_COLLIDE_CONNECT ));
|
||||
break;
|
||||
@@ -2234,6 +2243,15 @@ case FN_getActorLocalInertia: //Sub Procedure
|
||||
case FN_SetActorSleepState: //Sub Procedure
|
||||
rc_setActorSleepState( SETACTORSLEEPSTATE_ACTOR, SETACTORSLEEPSTATE_STATE );
|
||||
break;
|
||||
case FN_CastRay3D: //Number Function
|
||||
rc_push_num(rc_castRay3D( CASTRAY3D_FROM_X, CASTRAY3D_FROM_Y, CASTRAY3D_FROM_Z, CASTRAY3D_TO_X, CASTRAY3D_TO_Y, CASTRAY3D_TO_Z ));
|
||||
break;
|
||||
case FN_CastRay3D_All: //Number Function
|
||||
rc_push_num(rc_castRay3D_All( CASTRAY3D_ALL_FROM_X, CASTRAY3D_ALL_FROM_Y, CASTRAY3D_ALL_FROM_Z, CASTRAY3D_ALL_TO_X, CASTRAY3D_ALL_TO_Y, CASTRAY3D_ALL_TO_Z ));
|
||||
break;
|
||||
case FN_GetRayHit3D: //Sub Procedure
|
||||
rc_getRayHit3D( GETRAYHIT3D_INDEX, &GETRAYHIT3D_ACTOR_ID, &GETRAYHIT3D_X, &GETRAYHIT3D_Y, &GETRAYHIT3D_Z, &GETRAYHIT3D_NORMAL_X, &GETRAYHIT3D_NORMAL_Y, &GETRAYHIT3D_NORMAL_Z );
|
||||
break;
|
||||
case FN_createPointConstraint: //Number Function
|
||||
rc_push_num(rc_createPointConstraint( CREATEPOINTCONSTRAINT_ACTORA, CREATEPOINTCONSTRAINT_PXA, CREATEPOINTCONSTRAINT_PYA, CREATEPOINTCONSTRAINT_PZA ));
|
||||
break;
|
||||
|
||||
@@ -5,10 +5,12 @@
|
||||
#include "SDL.h"
|
||||
#include "btBulletDynamicsCommon.h"
|
||||
#include "BulletSoftBody/btSoftRigidDynamicsWorld.h"
|
||||
#include "BulletCollision/NarrowPhaseCollision/btRaycastCallback.h"
|
||||
#else
|
||||
#include <SDL2/SDL.h>
|
||||
#include <bullet/btBulletDynamicsCommon.h>
|
||||
#include <BulletSoftBody/btSoftRigidDynamicsWorld.h>
|
||||
#include <bullet/BulletCollision/NarrowPhaseCollision/btRaycastCallback.h>
|
||||
#endif // _IRR_ANDROID_PLATFORM_
|
||||
#include <irrlicht.h>
|
||||
#include <iostream>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#define RC_OS_DEFINES_H_INCLUDED
|
||||
|
||||
//USED FOR TESTING ONLY
|
||||
#define RC_TESTING
|
||||
//#define RC_TESTING
|
||||
|
||||
//I am checking Android first since I think it also defines __linux__
|
||||
|
||||
|
||||
@@ -3,6 +3,93 @@
|
||||
|
||||
#include "rc_gfx_core.h"
|
||||
|
||||
struct rc_rayHit3D_obj
|
||||
{
|
||||
int actor_id;
|
||||
btVector3 hit_point;
|
||||
btVector3 hit_normal;
|
||||
};
|
||||
|
||||
irr::core::array<rc_rayHit3D_obj> rc_rayHit3D;
|
||||
|
||||
///all hits
|
||||
int rc_castRay3D_All(double from_x, double from_y, double from_z, double to_x, double to_y, double to_z)
|
||||
{
|
||||
rc_rayHit3D.clear();
|
||||
btVector3 from(from_x, from_y, from_z);
|
||||
btVector3 to(to_x, to_y, to_z);
|
||||
//m_dynamicsWorld->getDebugDrawer()->drawLine(from, to, btVector4(0, 0, 0, 1));
|
||||
btCollisionWorld::AllHitsRayResultCallback allResults(from, to);
|
||||
allResults.m_flags |= btTriangleRaycastCallback::kF_KeepUnflippedNormal;
|
||||
//kF_UseGjkConvexRaytest flag is now enabled by default, use the faster but more approximate algorithm
|
||||
//allResults.m_flags |= btTriangleRaycastCallback::kF_UseSubSimplexConvexCastRaytest;
|
||||
allResults.m_flags |= btTriangleRaycastCallback::kF_UseSubSimplexConvexCastRaytest;
|
||||
|
||||
rc_physics3D.world->getPointer()->rayTest(from, to, allResults);
|
||||
|
||||
for (int i = 0; i < allResults.m_hitFractions.size(); i++)
|
||||
{
|
||||
rc_rayHit3D_obj hit;
|
||||
hit.hit_point = from.lerp(to, allResults.m_hitFractions[i]);
|
||||
hit.hit_normal = allResults.m_hitNormalWorld[i];
|
||||
btRigidBody* body = (btRigidBody*)allResults.m_collisionObjects[i];
|
||||
SCollisionObjectIdentification* colID = (SCollisionObjectIdentification*)body->getUserPointer();
|
||||
hit.actor_id = colID->getId();
|
||||
rc_rayHit3D.push_back(hit);
|
||||
//m_dynamicsWorld->getDebugDrawer()->drawSphere(p, 0.1, red);
|
||||
//m_dynamicsWorld->getDebugDrawer()->drawLine(p, p + allResults.m_hitNormalWorld[i], red);
|
||||
}
|
||||
|
||||
return allResults.m_hitFractions.size();
|
||||
}
|
||||
|
||||
///first hit
|
||||
bool rc_castRay3D(double from_x, double from_y, double from_z, double to_x, double to_y, double to_z)
|
||||
{
|
||||
rc_rayHit3D.clear();
|
||||
btVector3 from(from_x, from_y, from_z);
|
||||
btVector3 to(to_x, to_y, to_z);
|
||||
//m_dynamicsWorld->getDebugDrawer()->drawLine(from, to, btVector4(0, 0, 1, 1));
|
||||
|
||||
btCollisionWorld::ClosestRayResultCallback closestResults(from, to);
|
||||
closestResults.m_flags |= btTriangleRaycastCallback::kF_FilterBackfaces;
|
||||
|
||||
rc_physics3D.world->getPointer()->rayTest(from, to, closestResults);
|
||||
|
||||
if (closestResults.hasHit())
|
||||
{
|
||||
rc_rayHit3D_obj hit;
|
||||
hit.hit_point = from.lerp(to, closestResults.m_closestHitFraction);
|
||||
hit.hit_normal = closestResults.m_hitNormalWorld;
|
||||
btRigidBody* body = (btRigidBody*)closestResults.m_collisionObject;
|
||||
SCollisionObjectIdentification* colID = (SCollisionObjectIdentification*)body->getUserPointer();
|
||||
hit.actor_id = colID->getId();
|
||||
rc_rayHit3D.push_back(hit);
|
||||
|
||||
//m_dynamicsWorld->getDebugDrawer()->drawSphere(p, 0.1, blue);
|
||||
//m_dynamicsWorld->getDebugDrawer()->drawLine(p, p + closestResults.m_hitNormalWorld, blue);
|
||||
}
|
||||
|
||||
return closestResults.hasHit();
|
||||
}
|
||||
|
||||
void rc_getRayHit3D( int index, double* actor_id, double* x, double* y, double* z, double* normal_x, double* normal_y, double* normal_z )
|
||||
{
|
||||
if(index < 0 || index >= rc_rayHit3D.size())
|
||||
{
|
||||
*actor_id = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
*actor_id = rc_rayHit3D[index].actor_id;
|
||||
*x = rc_rayHit3D[index].hit_point.getX();
|
||||
*y = rc_rayHit3D[index].hit_point.getY();
|
||||
*z = rc_rayHit3D[index].hit_point.getZ();
|
||||
*normal_x = rc_rayHit3D[index].hit_normal.getX();
|
||||
*normal_y = rc_rayHit3D[index].hit_normal.getY();
|
||||
*normal_z = rc_rayHit3D[index].hit_normal.getZ();
|
||||
}
|
||||
|
||||
//Set Gravity
|
||||
void rc_setGravity3D(double x, double y, double z)
|
||||
{
|
||||
|
||||
@@ -586,3 +586,115 @@ void rc_getGravity2D(double* x, double* y)
|
||||
*x = rc_canvas[rc_active_canvas].physics2D.world->GetGravity().x;
|
||||
*y = rc_canvas[rc_active_canvas].physics2D.world->GetGravity().y;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Custom callback to collect all ray cast hits
|
||||
class RayCastCallback : public b2RayCastCallback {
|
||||
public:
|
||||
struct Hit {
|
||||
b2Fixture* fixture;
|
||||
b2Vec2 point;
|
||||
b2Vec2 normal;
|
||||
float fraction;
|
||||
};
|
||||
|
||||
std::vector<Hit> hits;
|
||||
|
||||
// This function is called for every fixture hit by the ray
|
||||
float ReportFixture(b2Fixture* fixture, const b2Vec2& point, const b2Vec2& normal, float fraction) override {
|
||||
hits.push_back({fixture, point, normal, fraction});
|
||||
return 1.0f; // Continue the ray cast to find all hits
|
||||
}
|
||||
};
|
||||
|
||||
struct rc_rayHit2D_obj
|
||||
{
|
||||
int sprite_id;
|
||||
b2Vec2 hit_point;
|
||||
b2Vec2 hit_normal;
|
||||
};
|
||||
|
||||
std::vector<rc_rayHit2D_obj> rc_rayHit2D;
|
||||
|
||||
// Function to perform a ray cast and collect all hits
|
||||
int rc_castRay2D_All(double from_x, double from_y, double to_x, double to_y)
|
||||
{
|
||||
rc_rayHit2D.clear();
|
||||
RayCastCallback callback;
|
||||
const b2Vec2 point1(from_x, from_y);
|
||||
const b2Vec2 point2(to_x, to_y);
|
||||
|
||||
rc_canvas[rc_active_canvas].physics2D.world->RayCast(&callback, point1, point2);
|
||||
|
||||
std::vector<RayCastCallback::Hit> cb_hits = callback.hits;
|
||||
|
||||
for(int i = 0; i < cb_hits.size(); i++)
|
||||
{
|
||||
rc_rayHit2D_obj hit;
|
||||
rc_sprite2D_obj* h_sprite = (rc_sprite2D_obj*)cb_hits[i].fixture->GetBody()->GetUserData().pointer;
|
||||
hit.sprite_id = h_sprite->id;
|
||||
hit.hit_point = cb_hits[i].point;
|
||||
hit.hit_normal = cb_hits[i].normal;
|
||||
rc_rayHit2D.push_back(hit);
|
||||
}
|
||||
|
||||
return cb_hits.size();
|
||||
}
|
||||
|
||||
// Function to perform a ray cast and collect the closest hit
|
||||
int rc_castRay2D(double from_x, double from_y, double to_x, double to_y)
|
||||
{
|
||||
rc_rayHit2D.clear();
|
||||
RayCastCallback callback;
|
||||
const b2Vec2 point1(from_x, from_y);
|
||||
const b2Vec2 point2(to_x, to_y);
|
||||
|
||||
rc_canvas[rc_active_canvas].physics2D.world->RayCast(&callback, point1, point2);
|
||||
|
||||
std::vector<RayCastCallback::Hit> cb_hits = callback.hits;
|
||||
|
||||
float min_fraction = 0;
|
||||
int index = 0;
|
||||
|
||||
for(int i = 0; i < cb_hits.size(); i++)
|
||||
{
|
||||
rc_rayHit2D_obj hit;
|
||||
rc_sprite2D_obj* h_sprite = (rc_sprite2D_obj*)cb_hits[i].fixture->GetBody()->GetUserData().pointer;
|
||||
hit.sprite_id = h_sprite->id;
|
||||
hit.hit_point = cb_hits[i].point;
|
||||
hit.hit_normal = cb_hits[i].normal;
|
||||
|
||||
if(i == 0 || cb_hits[i].fraction < min_fraction)
|
||||
{
|
||||
min_fraction = cb_hits[i].fraction;
|
||||
index = rc_rayHit2D.size();
|
||||
rc_rayHit2D.push_back(hit);
|
||||
}
|
||||
}
|
||||
|
||||
if(cb_hits.size() == 0)
|
||||
return 0;
|
||||
|
||||
rc_rayHit2D_obj min_hit = rc_rayHit2D[index];
|
||||
rc_rayHit2D.clear();
|
||||
rc_rayHit2D.push_back(min_hit);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void rc_getRayHit2D(int index, double* spr_id, double* x, double* y, double* normal_x, double* normal_y)
|
||||
{
|
||||
if(index < 0 || index >= rc_rayHit2D.size())
|
||||
{
|
||||
*spr_id = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
*spr_id = rc_rayHit2D[index].sprite_id;
|
||||
*x = rc_rayHit2D[index].hit_point.x;
|
||||
*y = rc_rayHit2D[index].hit_point.y;
|
||||
*normal_x = rc_rayHit2D[index].hit_normal.x;
|
||||
*normal_y = rc_rayHit2D[index].hit_normal.y;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
1734119283 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_os_defines.h
|
||||
<TargetConditionals.h>
|
||||
|
||||
1733959765 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_defines.h
|
||||
1734207007 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_defines.h
|
||||
|
||||
1730291453 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_stdlib.h
|
||||
"rc_os_defines.h"
|
||||
@@ -1272,13 +1272,15 @@
|
||||
"rc_joints.h"
|
||||
<irrtheora.h>
|
||||
|
||||
1733777902 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_gfx_core.h
|
||||
1734192650 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_gfx_core.h
|
||||
"SDL.h"
|
||||
"btBulletDynamicsCommon.h"
|
||||
"BulletSoftBody/btSoftRigidDynamicsWorld.h"
|
||||
"BulletCollision/NarrowPhaseCollision/btRaycastCallback.h"
|
||||
<SDL2/SDL.h>
|
||||
<bullet/btBulletDynamicsCommon.h>
|
||||
<BulletSoftBody/btSoftRigidDynamicsWorld.h>
|
||||
<bullet/BulletCollision/NarrowPhaseCollision/btRaycastCallback.h>
|
||||
<irrlicht.h>
|
||||
<iostream>
|
||||
<sstream>
|
||||
@@ -2273,7 +2275,7 @@
|
||||
"rc_gfx_core.h"
|
||||
<irrtheora.h>
|
||||
|
||||
1733959765 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_func130_cases.h
|
||||
1734207007 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_func130_cases.h
|
||||
|
||||
1724469097 source:/home/n00b/Projects/irrBullet/src/irrBullet.cpp
|
||||
"irrBullet.h"
|
||||
@@ -2506,7 +2508,7 @@
|
||||
<irrlicht.h>
|
||||
<vector>
|
||||
|
||||
1731903210 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_sprite_physics.h
|
||||
1734205862 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_sprite_physics.h
|
||||
"rc_sprite2D.h"
|
||||
|
||||
1730291453 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_joints.h
|
||||
@@ -2515,10 +2517,10 @@
|
||||
"rc_sprite2D.h"
|
||||
"rc_gfx_core.h"
|
||||
|
||||
1733590876 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_physics3D_base.h
|
||||
1734206925 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_physics3D_base.h
|
||||
"rc_gfx_core.h"
|
||||
|
||||
1734125778 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_base_actor.h
|
||||
1734138955 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_base_actor.h
|
||||
|
||||
1731710840 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_actor_material.h
|
||||
|
||||
@@ -2539,3 +2541,7 @@
|
||||
|
||||
1733959687 /home/n00b/Projects/RCBASIC4/rcbasic_runtime/rc_windowclose.h
|
||||
|
||||
1608686973 /usr/include/bullet/BulletCollision/NarrowPhaseCollision/btRaycastCallback.h
|
||||
"BulletCollision/CollisionShapes/btTriangleCallback.h"
|
||||
"LinearMath/btTransform.h"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user