physics.box2d: Rid #include of 'box2d.h'.

This commit is contained in:
Rod Kay
2023-12-31 21:17:53 +11:00
parent 6188ad4e80
commit be98a56d0c
6 changed files with 104 additions and 23 deletions

View File

@@ -17,7 +17,7 @@ extern "C"
%import "../../c_math/generate/c_math_c.i" %import "../../c_math/generate/c_math_c.i"
%include "../source/c/box2d.h" # %include "../source/c/box2d.h"
%include "../source/c/box2d-shape.h" %include "../source/c/box2d-shape.h"
%include "../source/c/box2d-object.h" %include "../source/c/box2d-object.h"
%include "../source/c/box2d-joint.h" %include "../source/c/box2d-joint.h"

View File

@@ -1,7 +1,7 @@
#ifndef C_BOX2D_JOINT_H #ifndef C_BOX2D_JOINT_H
#define C_BOX2D_JOINT_H #define C_BOX2D_JOINT_H
#include "box2d.h" #include "c_math.h"
#include "box2d-object.h" #include "box2d-object.h"

View File

@@ -2,7 +2,7 @@
#define C_BOX2D_OBJECT_H #define C_BOX2D_OBJECT_H
#include "box2d.h" #include "c_math.h"
#include "box2d-shape.h" #include "box2d-shape.h"
@@ -14,7 +14,7 @@ extern "C"
Real Mass, Real Mass,
Real Friction, Real Friction,
Real Restitution, Real Restitution,
Shape* the_Shape); Shape* the_Shape);
void b2d_free_Object (Object* Self); void b2d_free_Object (Object* Self);

View File

@@ -1,7 +1,7 @@
#ifndef C_BOX2D_SHAPE_H #ifndef C_BOX2D_SHAPE_H
#define C_BOX2D_SHAPE_H #define C_BOX2D_SHAPE_H
#include "box2d.h" #include "c_math.h"

View File

@@ -43,11 +43,10 @@ public:
b2Fixture* Nearest; b2Fixture* Nearest;
float float
ReportFixture ReportFixture (b2Fixture* fixture,
(b2Fixture* fixture, const b2Vec2& point,
const b2Vec2& point, const b2Vec2& normal,
const b2Vec2& normal, float fraction)
float fraction)
{ {
Nearest = fixture; Nearest = fixture;
@@ -57,6 +56,7 @@ public:
}; };
/// Collisions /// Collisions
// //
@@ -223,7 +223,8 @@ b2d_free_Space (struct Space* Self)
void void
b2d_Space_Gravity_is (Space* Self, Vector_3* Now) b2d_Space_Gravity_is (Space* Self,
Vector_3* Now)
{ {
b2World* the_World = to_World (Self); b2World* the_World = to_World (Self);
@@ -232,7 +233,8 @@ b2d_Space_Gravity_is (Space* Self, Vector_3* Now)
void void
b2d_Space_evolve (Space* Self, float By) b2d_Space_evolve (Space* Self,
float By)
{ {
b2World* the_World = to_World (Self); b2World* the_World = to_World (Self);
contact_Listener* the_contact_Listener = dynamic_cast <contact_Listener*> (the_World->GetContactManager().m_contactListener); contact_Listener* the_contact_Listener = dynamic_cast <contact_Listener*> (the_World->GetContactManager().m_contactListener);
@@ -244,19 +246,20 @@ b2d_Space_evolve (Space* Self, float By)
void void
b2d_Space_add_Object (Space* Self, Object* the_Object) b2d_Space_add_Object (Space* Self,
Object* the_Object)
{ {
b2World* the_World = (b2World*)Self; b2World* the_World = (b2World*) Self;
the_Object->body = the_World->CreateBody (&the_Object->bodyDef); the_Object->body = the_World->CreateBody (&the_Object->bodyDef);
// the_Object->body->SetUserData (the_Object); the_Object->body->SetUserData (the_Object);
the_Object->body->CreateFixture (&the_Object->fixtureDef); the_Object->body->CreateFixture (&the_Object->fixtureDef);
} }
void void
b2d_Space_rid_Object (Space* Self, Object* the_Object) b2d_Space_rid_Object (Space* Self,
Object* the_Object)
{ {
((b2World*)Self)->DestroyBody (the_Object->body); ((b2World*)Self)->DestroyBody (the_Object->body);
the_Object->body = 0; the_Object->body = 0;
@@ -264,7 +267,8 @@ b2d_Space_rid_Object (Space* Self, Object* the_Object)
void void
b2d_Space_add_Joint (Space* Self, Joint* the_Joint) b2d_Space_add_Joint (Space* Self,
Joint* the_Joint)
{ {
b2World* the_World = (b2World*) Self; b2World* the_World = (b2World*) Self;
b2JointDef* jointDef = (b2JointDef*) the_Joint; b2JointDef* jointDef = (b2JointDef*) the_Joint;
@@ -351,21 +355,21 @@ b2d_ray_Collision
b2d_Space_cast_Ray (Space* Self, Vector_3* From, b2d_Space_cast_Ray (Space* Self, Vector_3* From,
Vector_3* To) Vector_3* To)
{ {
b2World* the_World = (b2World*) Self; b2World* the_World = (b2World*) Self;
my_raycast_Callback the_Callback; my_raycast_Callback the_Callback;
the_Callback.Nearest = 0; the_Callback.Nearest = 0;
the_World->RayCast (&the_Callback, the_World->RayCast (&the_Callback,
b2Vec2 (From->x, From->y), b2Vec2 (From->x, From->y),
b2Vec2 (To ->x, To ->y)); b2Vec2 (To ->x, To ->y));
b2d_ray_Collision the_Collision; b2d_ray_Collision the_Collision;
if (the_Callback.Nearest == 0) if (the_Callback.Nearest == 0)
the_Collision.near_Object = 0; the_Collision.near_Object = 0;
else else
the_Collision.near_Object = (Object*) (the_Callback.Nearest->GetBody()->GetUserData().pointer); the_Collision.near_Object = (Object*) (the_Callback.Nearest->GetBody()->GetUserData().pointer);
the_Collision.hit_Fraction = 0.0; the_Collision.hit_Fraction = 0.0;
the_Collision.Normal_world = Vector_3 (0.0, 0.0, 0.0); the_Collision.Normal_world = Vector_3 (0.0, 0.0, 0.0);
@@ -375,4 +379,81 @@ b2d_Space_cast_Ray (Space* Self, Vector_3* From,
} }
/// Pointcasts
//
class QueryCallback : public b2QueryCallback
{
public:
QueryCallback (const b2Vec2& point)
{
m_point = point;
m_fixture = NULL;
}
bool
ReportFixture (b2Fixture* fixture) override
{
b2Body* body = fixture->GetBody();
bool inside = fixture->TestPoint (m_point);
if (inside)
{
m_fixture = fixture;
return false; // We are done, terminate the query.
}
return true; // Continue the query.
}
b2Vec2 m_point;
b2Fixture* m_fixture;
};
b2d_point_Collision
b2d_Space_cast_Point (Space* Self,
Vector_3* Point)
{
b2d_point_Collision Result;
b2World* the_World = (b2World *)Self;
const b2Vec2 p = b2Vec2 (Point->x,
Point->y);
// Make a small box.
//
b2AABB aabb;
b2Vec2 d;
d.Set (0.001f, 0.001f);
aabb.lowerBound = p - d;
aabb.upperBound = p + d;
printf ("\n");
printf ("b2d_Space_cast_Point ~ p = %f %f\n", p.x, p.y);
// Query the world for overlapping shapes.
//
QueryCallback Callback (p);
the_World->QueryAABB (&Callback, aabb);
if (Callback.m_fixture)
{
Result.near_Object = (Object*) Callback.m_fixture->GetBody()->GetUserData().pointer;
}
else
{
Result.near_Object = NULL;
}
Result.Site_world = *Point;
return Result;
}
} // extern "C" } // extern "C"

View File

@@ -2,7 +2,7 @@
#define C_BOX2D_SPACE_H #define C_BOX2D_SPACE_H
#include "box2d.h" #include "c_math.h"
#include "box2d-object.h" #include "box2d-object.h"
#include "box2d-joint.h" #include "box2d-joint.h"