making a stab at updating Shapes

--HG--
branch : box2d-update
This commit is contained in:
Bill Meltsner
2011-03-13 09:33:32 -04:00
parent e32a373dc9
commit c3d69746c1
16 changed files with 300 additions and 746 deletions
@@ -184,6 +184,7 @@
A986EC84132CE6DB00F048C8 /* b2PulleyJoint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A986EC0D132CE6D800F048C8 /* b2PulleyJoint.cpp */; }; A986EC84132CE6DB00F048C8 /* b2PulleyJoint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A986EC0D132CE6D800F048C8 /* b2PulleyJoint.cpp */; };
A986EC85132CE6DB00F048C8 /* b2RevoluteJoint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A986EC0F132CE6D800F048C8 /* b2RevoluteJoint.cpp */; }; A986EC85132CE6DB00F048C8 /* b2RevoluteJoint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A986EC0F132CE6D800F048C8 /* b2RevoluteJoint.cpp */; };
A986EC86132CE6DB00F048C8 /* b2WeldJoint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A986EC11132CE6D800F048C8 /* b2WeldJoint.cpp */; }; A986EC86132CE6DB00F048C8 /* b2WeldJoint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A986EC11132CE6D800F048C8 /* b2WeldJoint.cpp */; };
A986ECAF132CEBB000F048C8 /* Fixture.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A986ECAD132CEB9300F048C8 /* Fixture.cpp */; };
A98D914410507C97008E03F2 /* EncodedImageData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A98D914310507C97008E03F2 /* EncodedImageData.cpp */; }; A98D914410507C97008E03F2 /* EncodedImageData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A98D914310507C97008E03F2 /* EncodedImageData.cpp */; };
A9B4BA9C1045937F001DBC80 /* ParticleSystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9B4BA9A1045937F001DBC80 /* ParticleSystem.cpp */; }; A9B4BA9C1045937F001DBC80 /* ParticleSystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9B4BA9A1045937F001DBC80 /* ParticleSystem.cpp */; };
A9B4BA9D1045937F001DBC80 /* wrap_ParticleSystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9B4BA9B1045937F001DBC80 /* wrap_ParticleSystem.cpp */; }; A9B4BA9D1045937F001DBC80 /* wrap_ParticleSystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9B4BA9B1045937F001DBC80 /* wrap_ParticleSystem.cpp */; };
@@ -598,6 +599,8 @@
A986EC11132CE6D800F048C8 /* b2WeldJoint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = b2WeldJoint.cpp; sourceTree = "<group>"; }; A986EC11132CE6D800F048C8 /* b2WeldJoint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = b2WeldJoint.cpp; sourceTree = "<group>"; };
A986EC12132CE6D800F048C8 /* b2WeldJoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = b2WeldJoint.h; sourceTree = "<group>"; }; A986EC12132CE6D800F048C8 /* b2WeldJoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = b2WeldJoint.h; sourceTree = "<group>"; };
A986ECAC132CE82F00F048C8 /* Box2D.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Box2D.h; sourceTree = "<group>"; }; A986ECAC132CE82F00F048C8 /* Box2D.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Box2D.h; sourceTree = "<group>"; };
A986ECAD132CEB9300F048C8 /* Fixture.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Fixture.cpp; sourceTree = "<group>"; };
A986ECAE132CEB9B00F048C8 /* Fixture.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Fixture.h; sourceTree = "<group>"; };
A98D913E10507BF9008E03F2 /* EncodedImageData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EncodedImageData.h; sourceTree = "<group>"; }; A98D913E10507BF9008E03F2 /* EncodedImageData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EncodedImageData.h; sourceTree = "<group>"; };
A98D914310507C97008E03F2 /* EncodedImageData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = EncodedImageData.cpp; sourceTree = "<group>"; }; A98D914310507C97008E03F2 /* EncodedImageData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = EncodedImageData.cpp; sourceTree = "<group>"; };
A9B1AE451197293000D496EB /* love_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = love_Prefix.pch; sourceTree = "<group>"; }; A9B1AE451197293000D496EB /* love_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = love_Prefix.pch; sourceTree = "<group>"; };
@@ -1144,6 +1147,8 @@
A93E6AF010420AC7007D418B /* Contact.h */, A93E6AF010420AC7007D418B /* Contact.h */,
A93E6AF110420AC7007D418B /* DistanceJoint.cpp */, A93E6AF110420AC7007D418B /* DistanceJoint.cpp */,
A93E6AF210420AC7007D418B /* DistanceJoint.h */, A93E6AF210420AC7007D418B /* DistanceJoint.h */,
A986ECAD132CEB9300F048C8 /* Fixture.cpp */,
A986ECAE132CEB9B00F048C8 /* Fixture.h */,
A93E6AF310420AC7007D418B /* GearJoint.cpp */, A93E6AF310420AC7007D418B /* GearJoint.cpp */,
A93E6AF410420AC7007D418B /* GearJoint.h */, A93E6AF410420AC7007D418B /* GearJoint.h */,
A93E6AF510420AC7007D418B /* graham */, A93E6AF510420AC7007D418B /* graham */,
@@ -1672,6 +1677,7 @@
A986EC84132CE6DB00F048C8 /* b2PulleyJoint.cpp in Sources */, A986EC84132CE6DB00F048C8 /* b2PulleyJoint.cpp in Sources */,
A986EC85132CE6DB00F048C8 /* b2RevoluteJoint.cpp in Sources */, A986EC85132CE6DB00F048C8 /* b2RevoluteJoint.cpp in Sources */,
A986EC86132CE6DB00F048C8 /* b2WeldJoint.cpp in Sources */, A986EC86132CE6DB00F048C8 /* b2WeldJoint.cpp in Sources */,
A986ECAF132CEBB000F048C8 /* Fixture.cpp in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
+16 -29
View File
@@ -30,41 +30,28 @@ namespace physics
{ {
namespace box2d namespace box2d
{ {
CircleShape::CircleShape(Body * body, b2CircleDef * def) CircleShape::CircleShape(b2CircleShape * c)
: Shape(body)
{ {
def->localPosition = body->world->scaleDown(def->localPosition); shape = c;
def->radius = body->world->scaleDown(def->radius);
radius = def->radius;
this->localPosition = def->localPosition;
def->userData = (void*)data;
shape = body->body->CreateShape(def);
} }
CircleShape::~CircleShape() CircleShape::~CircleShape()
{ {
shape = NULL;
} }
float CircleShape::getRadius() const float CircleShape::getRadius() const
{ {
return body->world->scaleUp(radius); return shape->m_radius;
} }
void CircleShape::getLocalCenter(float & x, float & y) const /**
{ * Gets the radius for the circle.
x = localPosition.x; **/
y = localPosition.y; void CircleShape::setRadius(float r)
body->world->scaleUp(x, y); {
} shape->m_radius = r;
}
void CircleShape::getWorldCenter(float & x, float & y) const
{
b2Vec2 worldCenter = body->body->GetWorldPoint(localPosition);
worldCenter = body->world->scaleUp(worldCenter);
x = worldCenter.x;
y = worldCenter.y;
}
} // box2d } // box2d
} // physics } // physics
+6 -15
View File
@@ -41,15 +41,6 @@ namespace box2d
**/ **/
class CircleShape : public Shape class CircleShape : public Shape
{ {
private:
// The radius of the circle. We need to store this because
// Box2D has no built-in method for getting the radius.
float radius;
// Local offset.
b2Vec2 localPosition;
public: public:
/** /**
@@ -58,7 +49,7 @@ namespace box2d
* @param body The parent body. * @param body The parent body.
* @param def The CircleShape definition. * @param def The CircleShape definition.
**/ **/
CircleShape(Body * body, b2CircleDef * def); CircleShape(b2CircleShape * c);
virtual ~CircleShape(); virtual ~CircleShape();
@@ -66,11 +57,11 @@ namespace box2d
* Gets the radius for the circle. * Gets the radius for the circle.
**/ **/
float getRadius() const; float getRadius() const;
// There is no support for setting the radius. /**
* Gets the radius for the circle.
void getLocalCenter(float & x, float & y) const; **/
void getWorldCenter(float & x, float & y) const; void setRadius(float r);
}; };
} // box2d } // box2d
+9
View File
@@ -0,0 +1,9 @@
//
// Fixture.cpp
// love
//
// Created by Bill Meltsner on 3/13/11.
// Copyright 2011 Bill Meltsner. All rights reserved.
//
#include "Fixture.h"
+197
View File
@@ -0,0 +1,197 @@
/**
* Copyright (c) 2006-2011 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_PHYSICS_BOX2D_FIXTURE_H
#define LOVE_PHYSICS_BOX2D_FIXTURE_H
// LOVE
#include <physics/Shape.h>
#include <physics/box2d/Body.h>
#include <physics/box2d/Shape.h>
#include <common/Object.h>
#include <common/Reference.h>
// Box2D
#include <Box2D/Box2D.h>
namespace love
{
namespace physics
{
namespace box2d
{
/**
* This struct is stored in a void pointer
* in the Box2D Fixture class. For now, all we
* need is a Lua reference to arbitrary data,
* but we might need more later.
**/
struct fixtureudata
{
// Reference to arbitrary data.
Reference * ref;
};
/**
* A Fixture is used to attach a shape to a body for collision detection.
* A Fixture inherits its transform from its parent. Fixtures hold
* additional non-geometric data such as friction, collision filters,
* etc.
**/
class Fixture : public Object
{
protected:
b2Fixture * fixture;
fixtureudata * data;
public:
/**
* Creates a Fixture.
**/
Fixture(Body * body, Shape * shape);
virtual ~Fixture();
/**
* Gets the type of the Fixture's Shape. Useful for
* debug drawing.
**/
Shape::Type getType() const;
/**
* Sets the friction of the Fixture.
* @param friction The new friction.
**/
void setFriction(float friction);
/**
* Sets the restitution for the Fixture.
* @param restitution The restitution.
**/
void setRestitution(float restitution);
/**
* Sets the density of the Fixture.
* @param density The density of the Fixture.
**/
void setDensity(float density);
/**
* Set whether this Fixture should be a sensor or not.
* @param sensor True if sensor, false if not.
**/
void setSensor(bool sensor);
/**
* Gets the friction of the Fixture.
* @returns The friction.
**/
float getFriction() const;
/**
* Gets the restitution of the Fixture.
* @return The restitution of the Fixture.
**/
float getRestitution() const;
/**
* Gets the density of the Fixture.
* @return The density.
**/
float getDensity() const;
/**
* Checks whether this Fixture acts as a sensor.
* @return True if sensor, false otherwise.
**/
bool isSensor() const;
/**
* Get the body attatched to this Fixture.
* @return The parent Body.
**/
Body * getBody() const;
/**
* Checks if a point is inside the Fixture.
* @param x The x-component of the Fixture.
* @param y The y-component of the Fixture.
**/
bool testPoint(float x, float y) const;
/**
* Tests whether a line segment intersects a Fixture.
**/
int testSegment(lua_State * L);
/**
* Sets the filter data. An integer array is used even though the
* first two elements are unsigned shorts. The elements are:
* category (16-bits), mask (16-bits) and group (32-bits/int).
**/
void setFilterData(int * v);
/**
* Gets the filter data. An integer array is used even though the
* first two elements are unsigned shorts. The elements are:
* category (16-bits), mask (16-bits) and group (32-bits/int).
**/
void getFilterData(int * v);
void setGroupIndex(int index);
int getGroupIndex() const;
int setCategory(lua_State * L);
int setMask(lua_State * L);
int getCategory(lua_State * L);
int getMask(lua_State * L);
uint16 getBits(lua_State * L);
int pushBits(lua_State * L, uint16 bits);
/**
* This function stores an in-C reference to
* arbitrary Lua data in the Box2D Fixture object.
*
* The data set here will be passed to the collision
* handler when collisions occur.
**/
int setData(lua_State * L);
/**
* Gets the data set with setData. If no
* data is set, nil is returned.
**/
int getData(lua_State * L);
/**
* Gets the bounding box for this Fixture.
* The function returns eight values which can be
* passed directly to love.graphics.polygon.
**/
int getBoundingBox(lua_State * L);
};
} // box2d
} // physics
} // love
#endif // LOVE_PHYSICS_BOX2D_FIXTURE_H
+31 -26
View File
@@ -78,41 +78,40 @@ namespace box2d
return new CircleShape(body, &def); return new CircleShape(body, &def);
} }
PolygonShape * Physics::newRectangleShape(Body * body, float w, float h) PolygonShape * Physics::newRectangleShape(float w, float h)
{ {
return newRectangleShape(body, 0, 0, w, h, 0); return newRectangleShape(body, 0, 0, w, h, 0);
} }
PolygonShape * Physics::newRectangleShape(Body * body, float x, float y, float w, float h) PolygonShape * Physics::newRectangleShape(float x, float y, float w, float h)
{ {
return newRectangleShape(body, x, y, w, h, 0); return newRectangleShape(body, x, y, w, h, 0);
} }
PolygonShape * Physics::newRectangleShape(Body * body, float x, float y, float w, float h, float angle) PolygonShape * Physics::newRectangleShape(float x, float y, float w, float h, float angle)
{ {
b2PolygonDef def; b2PolygonShape s;
def.friction = 0.5f; s.SetAsBox(w/2.0f, h/2.0f, b2Vec2(x, y), angle);
def.restitution = 0.1f; return new PolygonShape(&s);
def.density = 1.0f; }
def.SetAsBox(w/2.0f, h/2.0f, b2Vec2(x, y), angle);
return new PolygonShape(body, &def); PolygonShape * Physics::newEdgeShape(float x1, float y1, float x2, float y2)
{
b2PolygonShape s;
s.SetAsEdge(b2vec2(x1, y1), b2vec2(x2, y2));
return new PolygonShape(&s);
} }
int Physics::newPolygonShape(lua_State * L) int Physics::newPolygonShape(lua_State * L)
{ {
int argc = lua_gettop(L); int argc = lua_gettop(L);
int vcount = (int)(argc-1)/2; int vcount = (int)argc/2;
// 1 body + 3 vertices // 3 vertices
love::luax_assert_argc(L, 1 + (2 * 3)); love::luax_assert_argc(L, 2 * 3);
Body * b = luax_checkbody(L, 1); b2PolygonShape s;
b2PolygonDef def; std::vector<point2d> points(s.m_vertexCount);
def.friction = 0.5f;
def.restitution = 0.1f;
def.density = 1.0f;
std::vector<point2d> points(def.vertexCount);
std::vector<point2d> convex_hull; std::vector<point2d> convex_hull;
for(int i = 0;i<vcount;i++) for(int i = 0;i<vcount;i++)
@@ -126,16 +125,22 @@ namespace box2d
// Compute convex hull. // Compute convex hull.
GrahamScanConvexHull()(points, convex_hull); GrahamScanConvexHull()(points, convex_hull);
int count = convex_hull.size();
def.vertexCount = (int32)convex_hull.size(); if(count < 3)
if(def.vertexCount < 3)
return luaL_error(L, "Polygon degenerated to less than three points."); return luaL_error(L, "Polygon degenerated to less than three points.");
b2vec2 vecs[] = new b2vec2[count];
for (int i = 0; i < count; i++) {
vecs[i].Set(convex_hull[i].x, convex_hull[i].y);
}
s.Set(vecs, count);
for(int i = 0;i<def.vertexCount;i++) PolygonShape * p = new PolygonShape(&s);
def.vertices[def.vertexCount-i-1].Set((float)convex_hull[i].x, (float)convex_hull[i].y); delete[] vecs;
PolygonShape * p = new PolygonShape(b, &def);
luax_newtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, (void*)p); luax_newtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, (void*)p);
+16 -5
View File
@@ -92,7 +92,7 @@ namespace box2d
* @param body The Body to create the Shape on. * @param body The Body to create the Shape on.
* @param radius The radius of the circle. * @param radius The radius of the circle.
**/ **/
CircleShape * newCircleShape(Body * body, float radius); CircleShape * newCircleShape(float radius);
/** /**
* Creates a new CircleShape at (x,y) in local coorinates. * Creates a new CircleShape at (x,y) in local coorinates.
@@ -101,7 +101,7 @@ namespace box2d
* @param y The offset along the y-axis. * @param y The offset along the y-axis.
* @param radius The radius of the circle. * @param radius The radius of the circle.
**/ **/
CircleShape * newCircleShape(Body * body, float x, float y, float radius); CircleShape * newCircleShape(float x, float y, float radius);
/** /**
* Shorthand for creating rectangular PolygonShapes. The rectangle * Shorthand for creating rectangular PolygonShapes. The rectangle
@@ -109,7 +109,7 @@ namespace box2d
* @param w The width of the rectangle. * @param w The width of the rectangle.
* @param h The height of the rectangle. * @param h The height of the rectangle.
**/ **/
PolygonShape * newRectangleShape(Body * body, float w, float h); PolygonShape * newRectangleShape(float w, float h);
/** /**
* Shorthand for creating rectangular PolygonShapes. The rectangle * Shorthand for creating rectangular PolygonShapes. The rectangle
@@ -119,7 +119,7 @@ namespace box2d
* @param w The width of the rectangle. * @param w The width of the rectangle.
* @param h The height of the rectangle. * @param h The height of the rectangle.
**/ **/
PolygonShape * newRectangleShape(Body * body, float x, float y, float w, float h); PolygonShape * newRectangleShape(float x, float y, float w, float h);
/** /**
* Shorthand for creating rectangular PolygonShapes. The rectangle * Shorthand for creating rectangular PolygonShapes. The rectangle
@@ -130,7 +130,18 @@ namespace box2d
* @param h The height of the rectangle. * @param h The height of the rectangle.
* @param angle The angle of the rectangle. (rad) * @param angle The angle of the rectangle. (rad)
**/ **/
PolygonShape * newRectangleShape(Body * body, float x, float y, float w, float h, float angle); PolygonShape * newRectangleShape(float x, float y, float w, float h, float angle);
/**
* Shorthand for creating edge PolygonShapes. The edge
* will be created at (x,y) in local coordinates.
* @param x The offset along the x-axis.
* @param y The offset along the y-axis.
* @param w The width of the rectangle.
* @param h The height of the rectangle.
* @param angle The angle of the rectangle. (rad)
**/
PolygonShape * newEdgeShape(float x1, float y1, float x2, float y2);
/** /**
* Creates a new PolygonShape. * Creates a new PolygonShape.
+3 -9
View File
@@ -30,14 +30,9 @@ namespace physics
{ {
namespace box2d namespace box2d
{ {
PolygonShape::PolygonShape(Body * body, b2PolygonDef * def) PolygonShape::PolygonShape(b2PolygonShape * p)
: Shape(body)
{ {
for(int i = 0; i<def->vertexCount; i++) shape = p;
def->vertices[i] = body->world->scaleDown(def->vertices[i]);
def->userData = (void*)data;
shape = body->body->CreateShape(def);
} }
PolygonShape::~PolygonShape() PolygonShape::~PolygonShape()
@@ -48,11 +43,10 @@ namespace box2d
{ {
love::luax_assert_argc(L, 0); love::luax_assert_argc(L, 0);
b2PolygonShape * p = (b2PolygonShape *)shape; b2PolygonShape * p = (b2PolygonShape *)shape;
const b2Vec2 * vertices = p->GetVertices();
int count = p->GetVertexCount(); int count = p->GetVertexCount();
for(int i = 0;i<count; i++) for(int i = 0;i<count; i++)
{ {
b2Vec2 v = body->world->scaleUp(body->body->GetWorldPoint(vertices[i])); b2Vec2 v = p->GetVertex(i);
lua_pushnumber(L, v.x); lua_pushnumber(L, v.x);
lua_pushnumber(L, v.y); lua_pushnumber(L, v.y);
} }
+1 -1
View File
@@ -47,7 +47,7 @@ namespace box2d
* @param body The parent Body. * @param body The parent Body.
* @param def The polygon definition. * @param def The polygon definition.
**/ **/
PolygonShape(Body * body, b2PolygonDef * def); PolygonShape(b2PolygonShape * p);
virtual ~PolygonShape(); virtual ~PolygonShape();
+4 -240
View File
@@ -33,265 +33,29 @@ namespace physics
{ {
namespace box2d namespace box2d
{ {
Shape::Shape(Body * body) Shape::Shape()
: body(body), shape(NULL) : shape(NULL)
{ {
body->retain();
data = new shapeudata();
data->ref = 0;
} }
Shape::~Shape() Shape::~Shape()
{ {
if(data->ref != 0)
delete data->ref;
delete data;
data = 0;
if (shape)
body->body->DestroyShape(shape);
shape = 0; shape = 0;
body->release();
} }
Shape::Type Shape::getType() const Shape::Type Shape::getType() const
{ {
switch(shape->GetType()) switch(shape->GetType())
{ {
case e_circleShape: case b2Shape::e_circle:
return SHAPE_CIRCLE; return SHAPE_CIRCLE;
case e_polygonShape: case b2Shape::e_polygon:
return SHAPE_POLYGON; return SHAPE_POLYGON;
default: default:
return SHAPE_INVALID; return SHAPE_INVALID;
} }
} }
void Shape::setFriction(float friction)
{
shape->m_friction = friction;
}
void Shape::setRestitution(float restitution)
{
shape->m_restitution = restitution;
}
void Shape::setDensity(float density)
{
shape->m_density = density;
}
void Shape::setSensor(bool sensor)
{
shape->m_isSensor = sensor;
}
float Shape::getFriction() const
{
return shape->GetFriction();
}
float Shape::getRestitution() const
{
return shape->GetRestitution();
}
float Shape::getDensity() const
{
return shape->m_density;
}
bool Shape::isSensor() const
{
return shape->IsSensor();
}
Body * Shape::getBody() const
{
return body;
}
bool Shape::testPoint(float x, float y) const
{
return shape->TestPoint(shape->GetBody()->GetXForm(), body->getWorld()->scaleDown(b2Vec2(x, y)));
}
int Shape::testSegment(lua_State * L)
{
love::luax_assert_argc(L, 4, 4);
b2Segment s;
s.p1.x = (float)lua_tonumber(L, 1);
s.p1.y = (float)lua_tonumber(L, 2);
s.p2.x = (float)lua_tonumber(L, 3);
s.p2.y = (float)lua_tonumber(L, 4);
s.p1 = body->getWorld()->scaleDown(s.p1);
s.p2 = body->getWorld()->scaleDown(s.p2);
float lambda;
b2Vec2 normal;
if(shape->TestSegment(shape->GetBody()->GetXForm(), &lambda, &normal, s, 1.0f))
{
lua_pushnumber(L, lambda);
normal = body->getWorld()->scaleUp(normal);
lua_pushnumber(L, normal.x);
lua_pushnumber(L, normal.y);
return 3;
}
return 0;
}
void Shape::setFilterData(int * v)
{
b2FilterData f;
f.categoryBits = (unsigned short)v[0];
f.maskBits = (unsigned short)v[1];
f.groupIndex = v[2];
shape->SetFilterData(f);
shape->GetBody()->GetWorld()->Refilter(shape);
}
void Shape::getFilterData(int * v)
{
b2FilterData f = shape->GetFilterData();
v[0] = (int)f.categoryBits;
v[1] = (int)f.maskBits;
v[2] = f.groupIndex;
}
int Shape::setCategory(lua_State * L)
{
b2FilterData f = shape->GetFilterData();
f.categoryBits = (uint16)getBits(L);
shape->SetFilterData(f);
shape->GetBody()->GetWorld()->Refilter(shape);
return 0;
}
int Shape::setMask(lua_State * L)
{
b2FilterData f = shape->GetFilterData();
f.maskBits = ~(uint16)getBits(L);
shape->SetFilterData(f);
shape->GetBody()->GetWorld()->Refilter(shape);
return 0;
}
void Shape::setGroupIndex(int index)
{
b2FilterData f = shape->GetFilterData();
f.groupIndex = (uint16)index;
shape->SetFilterData(f);
shape->GetBody()->GetWorld()->Refilter(shape);
}
int Shape::getGroupIndex() const
{
b2FilterData f = shape->GetFilterData();
return f.groupIndex;
}
int Shape::getCategory(lua_State * L)
{
return pushBits(L, shape->GetFilterData().categoryBits);
}
int Shape::getMask(lua_State * L)
{
return pushBits(L, ~(shape->GetFilterData().maskBits));
}
uint16 Shape::getBits(lua_State * L)
{
// Get number of args.
int argc = lua_gettop(L);
// The new bitset.
std::bitset<16> b;
for(int i = 1;i<=argc;i++)
{
size_t bpos = (size_t)(lua_tointeger(L, i)-1);
if(bpos > 16)
return luaL_error(L, "Values must be in range 1-16.");
b.set(bpos, true);
}
return (uint16)b.to_ulong();
}
int Shape::pushBits(lua_State * L, uint16 bits)
{
// Create a bitset.
std::bitset<16> b((int)bits);
// Push all set bits.
for(int i = 0;i<16;i++)
if(b.test(i))
lua_pushinteger(L, i+1);
// Count number of set bits.
return (int)b.count();
}
int Shape::setData(lua_State * L)
{
love::luax_assert_argc(L, 1, 1);
if(data->ref != 0)
{
delete data->ref;
data->ref = 0;
}
data->ref = new Reference(L);
return 0;
}
int Shape::getData(lua_State * L)
{
love::luax_assert_argc(L, 0, 0);
if(data->ref != 0)
data->ref->push();
else
lua_pushnil(L);
return 1;
}
int Shape::getBoundingBox(lua_State * L)
{
love::luax_assert_argc(L, 0, 0);
b2AABB bb;
shape->ComputeAABB(&bb, shape->GetBody()->GetXForm());
bb = body->getWorld()->scaleUp(bb);
// Top left.
lua_pushnumber(L, bb.lowerBound.x);
lua_pushnumber(L, bb.upperBound.y);
// Bottom left.
lua_pushnumber(L, bb.lowerBound.x);
lua_pushnumber(L, bb.lowerBound.y);
// Bottom right.
lua_pushnumber(L, bb.upperBound.x);
lua_pushnumber(L, bb.lowerBound.y);
// Top right.
lua_pushnumber(L, bb.upperBound.x);
lua_pushnumber(L, bb.upperBound.y);
return 8;
}
} // box2d } // box2d
} // physics } // physics
} // love } // love
+3 -137
View File
@@ -35,20 +35,9 @@ namespace physics
{ {
namespace box2d namespace box2d
{ {
/**
* This struct is stored in a void pointer
* in the Box2D Shape class. For now, all we
* need is a Lua reference to arbitrary data,
* but we might need more later.
**/
struct shapeudata
{
// Reference to arbitrary data.
Reference * ref;
};
/** /**
* A Shape is geometry attached to a Body. * A Shape is geometry, attached to a Body via a Fixture.
* A Body has position and orientation, and * A Body has position and orientation, and
* a Shape's geometry will be affected by the parent * a Shape's geometry will be affected by the parent
* body's transformation. * body's transformation.
@@ -57,26 +46,15 @@ namespace box2d
{ {
protected: protected:
// A pointer to the parent Body. If the Body
// is destroyed, all child shapes are destroyed as well.
// This ensures that all child shapes are always destroyed
// *before* the parent Body.
Body * body;
// The Box2D shape. // The Box2D shape.
b2Shape * shape; b2Shape * shape;
// Our shape data, to be stored in
// the Box2D shape.
shapeudata * data;
public: public:
/** /**
* Creates a Shape attatched to the specified * Creates a Shape.
* Body.
**/ **/
Shape(Body * body); Shape();
virtual ~Shape(); virtual ~Shape();
@@ -85,118 +63,6 @@ namespace box2d
* debug drawing. * debug drawing.
**/ **/
Type getType() const; Type getType() const;
/**
* Sets the friction of the Shape.
* @param friction The new friction.
**/
void setFriction(float friction);
/**
* Sets the restitution for the Shape.
* @param restitution The restitution.
**/
void setRestitution(float restitution);
/**
* Sets the density of the Shape.
* @param density The density of the Shape.
**/
void setDensity(float density);
/**
* Set whether this shape should be a sensor or not.
* @param sensor True if sensor, false if not.
**/
void setSensor(bool sensor);
/**
* Gets the friction of the Shape.
* @returns The friction.
**/
float getFriction() const;
/**
* Gets the restitution of the Shape.
* @return The restitution of the Shape.
**/
float getRestitution() const;
/**
* Gets the density of the Shape.
* @return The density.
**/
float getDensity() const;
/**
* Checks whether this Shape acts as a sensor.
* @return True if sensor, false otherwise.
**/
bool isSensor() const;
/**
* Get the body attatched to this Shape.
* @return The parent Body.
**/
Body * getBody() const;
/**
* Checks if a point is inside the Shape.
* @param x The x-component of the Shape.
* @param y The y-component of the Shape.
**/
bool testPoint(float x, float y) const;
/**
* Tests whether a line segment intersects a Shape.
**/
int testSegment(lua_State * L);
/**
* Sets the filter data. An integer array is used even though the
* first two elements are unsigned shorts. The elements are:
* category (16-bits), mask (16-bits) and group (32-bits/int).
**/
void setFilterData(int * v);
/**
* Gets the filter data. An integer array is used even though the
* first two elements are unsigned shorts. The elements are:
* category (16-bits), mask (16-bits) and group (32-bits/int).
**/
void getFilterData(int * v);
void setGroupIndex(int index);
int getGroupIndex() const;
int setCategory(lua_State * L);
int setMask(lua_State * L);
int getCategory(lua_State * L);
int getMask(lua_State * L);
uint16 getBits(lua_State * L);
int pushBits(lua_State * L, uint16 bits);
/**
* This function stores an in-C reference to
* arbitrary Lua data in the Box2D shape object.
*
* The data set here will be passed to the collision
* handler when collisions occur.
**/
int setData(lua_State * L);
/**
* Gets the data set with setData. If no
* data is set, nil is returned.
**/
int getData(lua_State * L);
/**
* Gets the bounding box for this Shape.
* The function returns eight values which can be
* passed directly to love.graphics.polygon.
**/
int getBoundingBox(lua_State * L);
}; };
} // box2d } // box2d
+7 -42
View File
@@ -38,54 +38,19 @@ namespace box2d
return 1; return 1;
} }
int w_CircleShape_getLocalCenter(lua_State * L) int w_CircleShape_setRadius(lua_State * L)
{ {
CircleShape * c = luax_checkcircleshape(L, 1); CircleShape * c = luax_checkcircleshape(L, 1);
float x, y; float r = (float)luaL_checknumber(L, 2);
c->getLocalCenter(x, y); c->setRadius(r);
lua_pushnumber(L, x); return 0;
lua_pushnumber(L, y);
return 2;
}
int w_CircleShape_getWorldCenter(lua_State * L)
{
CircleShape * c = luax_checkcircleshape(L, 1);
float x, y;
c->getWorldCenter(x, y);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
return 2;
} }
static const luaL_Reg functions[] = { static const luaL_Reg functions[] = {
{ "getRadius", w_CircleShape_getRadius }, { "getRadius", w_CircleShape_getRadius },
{ "getLocalCenter", w_CircleShape_getLocalCenter }, { "setRadius", w_CircleShape_setRadius },
{ "getWorldCenter", w_CircleShape_getWorldCenter },
// From Shape. // From Shape.
{ "getType", w_Shape_getType }, { "getType", w_Shape_getType },
{ "setFriction", w_Shape_setFriction },
{ "setRestitution", w_Shape_setRestitution },
{ "setDensity", w_Shape_setDensity },
{ "setSensor", w_Shape_setSensor },
{ "getFriction", w_Shape_getFriction },
{ "getRestitution", w_Shape_getRestitution },
{ "getDensity", w_Shape_getDensity },
{ "getBody", w_Shape_getBody },
{ "isSensor", w_Shape_isSensor },
{ "testPoint", w_Shape_testPoint },
{ "testSegment", w_Shape_testSegment },
{ "setFilterData", w_Shape_setFilterData },
{ "getFilterData", w_Shape_getFilterData },
{ "setCategory", w_Shape_setCategory },
{ "getCategory", w_Shape_getCategory },
{ "setMask", w_Shape_setMask },
{ "getMask", w_Shape_getMask },
{ "setData", w_Shape_setData },
{ "getData", w_Shape_getData },
{ "getBoundingBox", w_Shape_getBoundingBox },
{ "getGroupIndex", w_Shape_getGroupIndex },
{ "setGroupIndex", w_Shape_setGroupIndex },
{ "destroy", w_Shape_destroy }, { "destroy", w_Shape_destroy },
{ 0, 0 } { 0, 0 }
}; };
+1 -2
View File
@@ -34,8 +34,7 @@ namespace box2d
{ {
CircleShape * luax_checkcircleshape(lua_State * L, int idx); CircleShape * luax_checkcircleshape(lua_State * L, int idx);
int w_CircleShape_getRadius(lua_State * L); int w_CircleShape_getRadius(lua_State * L);
int w_CircleShape_getLocalCenter(lua_State * L); int w_CircleShape_setRadius(lua_State * L);
int w_CircleShape_getWorldCenter(lua_State * L);
int luaopen_circleshape(lua_State * L); int luaopen_circleshape(lua_State * L);
} // box2d } // box2d
@@ -42,28 +42,6 @@ namespace box2d
{ "getPoints", w_PolygonShape_getPoints }, { "getPoints", w_PolygonShape_getPoints },
// From Shape. // From Shape.
{ "getType", w_Shape_getType }, { "getType", w_Shape_getType },
{ "setFriction", w_Shape_setFriction },
{ "setRestitution", w_Shape_setRestitution },
{ "setDensity", w_Shape_setDensity },
{ "setSensor", w_Shape_setSensor },
{ "getFriction", w_Shape_getFriction },
{ "getRestitution", w_Shape_getRestitution },
{ "getDensity", w_Shape_getDensity },
{ "getBody", w_Shape_getBody },
{ "isSensor", w_Shape_isSensor },
{ "testPoint", w_Shape_testPoint },
{ "testSegment", w_Shape_testSegment },
{ "setFilterData", w_Shape_setFilterData },
{ "getFilterData", w_Shape_getFilterData },
{ "setCategory", w_Shape_setCategory },
{ "getCategory", w_Shape_getCategory },
{ "setMask", w_Shape_setMask },
{ "getMask", w_Shape_getMask },
{ "setData", w_Shape_setData },
{ "getData", w_Shape_getData },
{ "getBoundingBox", w_Shape_getBoundingBox },
{ "getGroupIndex", w_Shape_getGroupIndex },
{ "setGroupIndex", w_Shape_setGroupIndex },
{ "destroy", w_Shape_destroy }, { "destroy", w_Shape_destroy },
{ 0, 0 } { 0, 0 }
}; };
-196
View File
@@ -41,180 +41,6 @@ namespace box2d
return 1; return 1;
} }
int w_Shape_setFriction(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setFriction(arg1);
return 0;
}
int w_Shape_setRestitution(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setRestitution(arg1);
return 0;
}
int w_Shape_setDensity(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setDensity(arg1);
return 0;
}
int w_Shape_setSensor(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
bool arg1 = luax_toboolean(L, 2);
t->setSensor(arg1);
return 0;
}
int w_Shape_getFriction(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
lua_pushnumber(L, t->getFriction());
return 1;
}
int w_Shape_getRestitution(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
lua_pushnumber(L, t->getRestitution());
return 1;
}
int w_Shape_getDensity(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
lua_pushnumber(L, t->getDensity());
return 1;
}
int w_Shape_isSensor(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
luax_pushboolean(L, t->isSensor());
return 1;
}
int w_Shape_getBody(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
Body * body = t->getBody();
if(body == 0)
return 0;
body->retain();
luax_newtype(L, "Body", PHYSICS_BODY_T, (void*)body);
return 1;
}
int w_Shape_testPoint(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
float x = (float)luaL_checknumber(L, 2);
float y = (float)luaL_checknumber(L, 3);
luax_pushboolean(L, t->testPoint(x, y));
return 1;
}
int w_Shape_testSegment(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
lua_remove(L, 1);
return t->testSegment(L);
}
int w_Shape_setFilterData(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
int v[3];
v[0] = luaL_checkint(L, 2);
v[1] = luaL_checkint(L, 3);
v[2] = luaL_checkint(L, 4);
t->setFilterData(v);
return 0;
}
int w_Shape_getFilterData(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
int v[3];
t->getFilterData(v);
lua_pushinteger(L, v[0]);
lua_pushinteger(L, v[1]);
lua_pushinteger(L, v[2]);
return 3;
}
int w_Shape_setCategory(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
lua_remove(L, 1);
return t->setCategory(L);
}
int w_Shape_getCategory(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
lua_remove(L, 1);
return t->getCategory(L);
}
int w_Shape_setMask(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
lua_remove(L, 1);
return t->setMask(L);
}
int w_Shape_getMask(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
lua_remove(L, 1);
return t->getMask(L);
}
int w_Shape_setData(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
lua_remove(L, 1);
return t->setData(L);
}
int w_Shape_getData(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
lua_remove(L, 1);
return t->getData(L);
}
int w_Shape_getBoundingBox(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
lua_remove(L, 1);
return t->getBoundingBox(L);
}
int w_Shape_getGroupIndex(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
int i = t->getGroupIndex();
lua_pushinteger(L, i);
return 1;
}
int w_Shape_setGroupIndex(lua_State * L)
{
Shape * t = luax_checkshape(L, 1);
int i = luaL_checkint(L, 2);
t->setGroupIndex(i);
return 0;
}
int w_Shape_destroy(lua_State * L) int w_Shape_destroy(lua_State * L)
{ {
Proxy * p = (Proxy *)lua_touserdata(L, 1); Proxy * p = (Proxy *)lua_touserdata(L, 1);
@@ -227,28 +53,6 @@ namespace box2d
static const luaL_Reg functions[] = { static const luaL_Reg functions[] = {
{ "getType", w_Shape_getType }, { "getType", w_Shape_getType },
{ "setFriction", w_Shape_setFriction },
{ "setRestitution", w_Shape_setRestitution },
{ "setDensity", w_Shape_setDensity },
{ "setSensor", w_Shape_setSensor },
{ "getFriction", w_Shape_getFriction },
{ "getRestitution", w_Shape_getRestitution },
{ "getDensity", w_Shape_getDensity },
{ "getBody", w_Shape_getBody },
{ "isSensor", w_Shape_isSensor },
{ "testPoint", w_Shape_testPoint },
{ "testSegment", w_Shape_testSegment },
{ "setFilterData", w_Shape_setFilterData },
{ "getFilterData", w_Shape_getFilterData },
{ "setCategory", w_Shape_setCategory },
{ "getCategory", w_Shape_getCategory },
{ "setMask", w_Shape_setMask },
{ "getMask", w_Shape_getMask },
{ "setData", w_Shape_setData },
{ "getData", w_Shape_getData },
{ "getBoundingBox", w_Shape_getBoundingBox },
{ "getGroupIndex", w_Shape_getGroupIndex },
{ "setGroupIndex", w_Shape_setGroupIndex },
{ "destroy", w_Shape_destroy }, { "destroy", w_Shape_destroy },
{ 0, 0 } { 0, 0 }
}; };
-22
View File
@@ -33,28 +33,6 @@ namespace box2d
{ {
Shape * luax_checkshape(lua_State * L, int idx); Shape * luax_checkshape(lua_State * L, int idx);
int w_Shape_getType(lua_State * L); int w_Shape_getType(lua_State * L);
int w_Shape_setFriction(lua_State * L);
int w_Shape_setRestitution(lua_State * L);
int w_Shape_setDensity(lua_State * L);
int w_Shape_setSensor(lua_State * L);
int w_Shape_getFriction(lua_State * L);
int w_Shape_getRestitution(lua_State * L);
int w_Shape_getDensity(lua_State * L);
int w_Shape_isSensor(lua_State * L);
int w_Shape_getBody(lua_State * L);
int w_Shape_testPoint(lua_State * L);
int w_Shape_testSegment(lua_State * L);
int w_Shape_setFilterData(lua_State * L);
int w_Shape_getFilterData(lua_State * L);
int w_Shape_setCategory(lua_State * L);
int w_Shape_getCategory(lua_State * L);
int w_Shape_setMask(lua_State * L);
int w_Shape_getMask(lua_State * L);
int w_Shape_setData(lua_State * L);
int w_Shape_getData(lua_State * L);
int w_Shape_getBoundingBox(lua_State * L);
int w_Shape_getGroupIndex(lua_State * L);
int w_Shape_setGroupIndex(lua_State * L);
int w_Shape_destroy(lua_State * L); int w_Shape_destroy(lua_State * L);
int luaopen_shape(lua_State * L); int luaopen_shape(lua_State * L);