Implement Fixture class properly.

--HG--
branch : box2d-update
This commit is contained in:
Bill Meltsner
2011-09-23 18:01:40 -04:00
parent 95f6047417
commit 71abae06c7
11 changed files with 745 additions and 62 deletions
+288 -7
View File
@@ -1,9 +1,290 @@
//
// Fixture.cpp
// love
//
// Created by Bill Meltsner on 3/13/11.
// Copyright 2011 Bill Meltsner. All rights reserved.
//
/**
* 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.
**/
#include "Fixture.h"
// Module
#include "Body.h"
#include "World.h"
#include "Physics.h"
// STD
#include <bitset>
namespace love
{
namespace physics
{
namespace box2d
{
Fixture::Fixture(Body * body, Shape * shape)
: body(body), shape(shape), fixture(NULL)
{
body->retain();
shape->retain();
data = new fixtureudata();
data->ref = 0;
b2FixtureDef def;
def.shape = shape->shape;
def.userData = (void *)data;
fixture = body->body->CreateFixture(&def);
}
Fixture::~Fixture()
{
if(data->ref != 0)
delete data->ref;
delete data;
data = 0;
if (fixture)
body->body->DestroyFixture(fixture);
fixture = 0;
body->release();
}
Shape::Type Fixture::getType() const
{
return shape->getType();
}
void Fixture::setFriction(float friction)
{
fixture->SetFriction(friction);
}
void Fixture::setRestitution(float restitution)
{
fixture->SetRestitution(restitution);
}
void Fixture::setDensity(float density)
{
fixture->SetDensity(density);
}
void Fixture::setSensor(bool sensor)
{
fixture->SetSensor(sensor);
}
float Fixture::getFriction() const
{
return fixture->GetFriction();
}
float Fixture::getRestitution() const
{
return fixture->GetRestitution();
}
float Fixture::getDensity() const
{
return fixture->GetDensity();
}
bool Fixture::isSensor() const
{
return fixture->IsSensor();
}
Body * Fixture::getBody() const
{
return body;
}
Shape * Fixture::getShape() const
{
return shape;
}
void Fixture::setFilterData(int * v)
{
b2Filter f;
f.categoryBits = (unsigned short)v[0];
f.maskBits = (unsigned short)v[1];
f.groupIndex = v[2];
fixture->SetFilterData(f);
}
void Fixture::getFilterData(int * v)
{
b2Filter f = fixture->GetFilterData();
v[0] = (int)f.categoryBits;
v[1] = (int)f.maskBits;
v[2] = f.groupIndex;
}
int Fixture::setCategory(lua_State * L)
{
b2Filter f = fixture->GetFilterData();
f.categoryBits = (uint16)getBits(L);
fixture->SetFilterData(f);
return 0;
}
int Fixture::setMask(lua_State * L)
{
b2Filter f = fixture->GetFilterData();
f.maskBits = ~(uint16)getBits(L);
fixture->SetFilterData(f);
return 0;
}
void Fixture::setGroupIndex(int index)
{
b2Filter f = fixture->GetFilterData();
f.groupIndex = (uint16)index;
fixture->SetFilterData(f);
}
int Fixture::getGroupIndex() const
{
b2Filter f = fixture->GetFilterData();
return f.groupIndex;
}
int Fixture::getCategory(lua_State * L)
{
return pushBits(L, fixture->GetFilterData().categoryBits);
}
int Fixture::getMask(lua_State * L)
{
return pushBits(L, ~(fixture->GetFilterData().maskBits));
}
uint16 Fixture::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 Fixture::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 Fixture::setUserData(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 Fixture::getUserData(lua_State * L)
{
love::luax_assert_argc(L, 0, 0);
if(data->ref != 0)
data->ref->push();
else
lua_pushnil(L);
return 1;
}
bool Fixture::testPoint(float x, float y) const
{
return fixture->TestPoint(Physics::scaleDown(b2Vec2(x, y)));
}
int Fixture::rayCast(lua_State * L) const
{
float p1x = Physics::scaleDown((float)luaL_checknumber(L, 1));
float p1y = Physics::scaleDown((float)luaL_checknumber(L, 2));
float p2x = Physics::scaleDown((float)luaL_checknumber(L, 3));
float p2y = Physics::scaleDown((float)luaL_checknumber(L, 4));
float maxFraction = (float)luaL_checknumber(L, 5);
float x = Physics::scaleDown((float)luaL_checknumber(L, 6));
float y = Physics::scaleDown((float)luaL_checknumber(L, 7));
float r = (float)luaL_checknumber(L, 8);
int childIndex = (int)luaL_optint(L, 9, 0);
b2RayCastInput input;
input.p1.Set(p1x, p1y);
input.p2.Set(p2x, p2y);
input.maxFraction = maxFraction;
b2Transform transform(b2Vec2(x, y), b2Rot(r));
b2RayCastOutput output;
fixture->RayCast(&output, input, childIndex);
lua_pushnumber(L, Physics::scaleUp(output.normal.x));
lua_pushnumber(L, Physics::scaleUp(output.normal.y));
lua_pushnumber(L, output.fraction);
return 3;
}
int Fixture::getBoundingBox(lua_State * L) const
{
int childIndex = (int)luaL_optint(L, 1, 0);
b2AABB box = fixture->GetAABB(childIndex);
box = Physics::scaleUp(box);
lua_pushnumber(L, box.lowerBound.x);
lua_pushnumber(L, box.lowerBound.y);
lua_pushnumber(L, box.upperBound.x);
lua_pushnumber(L, box.upperBound.y);
return 4;
}
int Fixture::getMassData(lua_State * L) const
{
b2MassData data;
fixture->GetMassData(&data);
b2Vec2 center = Physics::scaleUp(data.center);
lua_pushnumber(L, center.x);
lua_pushnumber(L, center.y);
lua_pushnumber(L, data.mass);
lua_pushnumber(L, data.I);
return 4;
}
} // box2d
} // physics
} // love
+74 -55
View File
@@ -59,6 +59,8 @@ namespace box2d
{
protected:
Body * body;
Shape * shape;
b2Fixture * fixture;
fixtureudata * data;
@@ -76,6 +78,64 @@ namespace box2d
* debug drawing.
**/
Shape::Type getType() const;
/**
* Gets the Shape attached to this Fixture.
**/
Shape * getShape() const;
/**
* Checks whether this Fixture acts as a sensor.
* @return True if sensor, false otherwise.
**/
bool isSensor() const;
/**
* Set whether this Fixture should be a sensor or not.
* @param sensor True if sensor, false if not.
**/
void setSensor(bool sensor);
/**
* Gets the Body this Fixture is attached to.
**/
Body * getBody() const;
/**
* Gets the next Fixture attached to the parent Body.
**/
Fixture * getNext() const;
/**
* 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);
/**
* 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 setUserData(lua_State * L);
/**
* Gets the data set with setData. If no
* data is set, nil is returned.
**/
int getUserData(lua_State * L);
/**
* Sets the friction of the Fixture.
@@ -95,12 +155,6 @@ namespace box2d
**/
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.
@@ -118,45 +172,19 @@ namespace box2d
* @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.
* @param x The x-component of the point.
* @param y The y-component of the point.
**/
bool testPoint(float x, float y) const;
/**
* Tests whether a line segment intersects a Fixture.
* Cast a ray against this 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);
int rayCast(lua_State * L) const;
void setGroupIndex(int index);
int getGroupIndex() const;
@@ -167,27 +195,18 @@ namespace box2d
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);
int getBoundingBox(lua_State * L) const;
/**
* Gets the mass data for this Fixture.
* This operation may be expensive.
**/
int getMassData(lua_State * L) const;
};
} // box2d
+5
View File
@@ -59,6 +59,11 @@ namespace box2d
{
return new Body(world, b2Vec2(0, 0), 1, 1);
}
Fixture * Physics::newFixture(Body * body, Shape * shape)
{
return new Fixture(body, shape);
}
CircleShape * Physics::newCircleShape(float radius)
{
+8
View File
@@ -26,6 +26,7 @@
#include "World.h"
#include "Contact.h"
#include "Body.h"
#include "Fixture.h"
#include "Shape.h"
#include "CircleShape.h"
#include "PolygonShape.h"
@@ -98,6 +99,13 @@ namespace box2d
* @param world The world to create the Body in.
**/
Body * newBody(World * world);
/**
* Creates a new Fixture.
* @param body The body to attach the Fixture to.
* @param shape The shape to attach to the Fixture.
**/
Fixture * newFixture(Body * body, Shape * shape);
/**
* Creates a new CircleShape at (0, 0).
+1
View File
@@ -44,6 +44,7 @@ namespace box2d
**/
class Shape : public love::physics::Shape
{
friend class Fixture;
protected:
+284
View File
@@ -0,0 +1,284 @@
/**
* 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.
**/
#include "wrap_Fixture.h"
#include <common/StringMap.h>
namespace love
{
namespace physics
{
namespace box2d
{
Fixture * luax_checkfixture(lua_State * L, int idx)
{
return luax_checktype<Fixture>(L, idx, "Fixture", PHYSICS_FIXTURE_T);
}
int w_Fixture_getType(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
const char * type = "";
Shape::getConstant(t->getType(), type);
lua_pushstring(L, type);
return 1;
}
int w_Fixture_setFriction(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setFriction(arg1);
return 0;
}
int w_Fixture_setRestitution(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setRestitution(arg1);
return 0;
}
int w_Fixture_setDensity(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
float arg1 = (float)luaL_checknumber(L, 2);
t->setDensity(arg1);
return 0;
}
int w_Fixture_setSensor(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
bool arg1 = luax_toboolean(L, 2);
t->setSensor(arg1);
return 0;
}
int w_Fixture_getFriction(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
lua_pushnumber(L, t->getFriction());
return 1;
}
int w_Fixture_getRestitution(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
lua_pushnumber(L, t->getRestitution());
return 1;
}
int w_Fixture_getDensity(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
lua_pushnumber(L, t->getDensity());
return 1;
}
int w_Fixture_isSensor(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
luax_pushboolean(L, t->isSensor());
return 1;
}
int w_Fixture_getBody(lua_State * L)
{
Fixture * t = luax_checkfixture(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_Fixture_getShape(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
Shape * shape = t->getShape();
if(shape == 0)
return 0;
shape->retain();
luax_newtype(L, "Shape", PHYSICS_SHAPE_T, (void*)shape);
return 1;
}
int w_Fixture_testPoint(lua_State * L)
{
Fixture * t = luax_checkfixture(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_Fixture_rayCast(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
lua_remove(L, 1);
return t->rayCast(L);
}
int w_Fixture_setFilterData(lua_State * L)
{
Fixture * t = luax_checkfixture(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_Fixture_getFilterData(lua_State * L)
{
Fixture * t = luax_checkfixture(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_Fixture_setCategory(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
lua_remove(L, 1);
return t->setCategory(L);
}
int w_Fixture_getCategory(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
lua_remove(L, 1);
return t->getCategory(L);
}
int w_Fixture_setMask(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
lua_remove(L, 1);
return t->setMask(L);
}
int w_Fixture_getMask(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
lua_remove(L, 1);
return t->getMask(L);
}
int w_Fixture_setUserData(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
lua_remove(L, 1);
return t->setUserData(L);
}
int w_Fixture_getUserData(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
lua_remove(L, 1);
return t->getUserData(L);
}
int w_Fixture_getBoundingBox(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
lua_remove(L, 1);
return t->getBoundingBox(L);
}
int w_Fixture_getMassData(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
lua_remove(L, 1);
return t->getMassData(L);
}
int w_Fixture_getGroupIndex(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
int i = t->getGroupIndex();
lua_pushinteger(L, i);
return 1;
}
int w_Fixture_setGroupIndex(lua_State * L)
{
Fixture * t = luax_checkfixture(L, 1);
int i = luaL_checkint(L, 2);
t->setGroupIndex(i);
return 0;
}
int w_Fixture_destroy(lua_State * L)
{
Proxy * p = (Proxy *)lua_touserdata(L, 1);
p->own = false;
Fixture * t = (Fixture *)p->data;
t->release();
return 0;
}
static const luaL_Reg functions[] = {
{ "getType", w_Fixture_getType },
{ "setFriction", w_Fixture_setFriction },
{ "setRestitution", w_Fixture_setRestitution },
{ "setDensity", w_Fixture_setDensity },
{ "setSensor", w_Fixture_setSensor },
{ "getFriction", w_Fixture_getFriction },
{ "getRestitution", w_Fixture_getRestitution },
{ "getDensity", w_Fixture_getDensity },
{ "getBody", w_Fixture_getBody },
{ "getShape", w_Fixture_getShape },
{ "isSensor", w_Fixture_isSensor },
{ "testPoint", w_Fixture_testPoint },
{ "rayCast", w_Fixture_rayCast },
{ "setFilterData", w_Fixture_setFilterData },
{ "getFilterData", w_Fixture_getFilterData },
{ "setCategory", w_Fixture_setCategory },
{ "getCategory", w_Fixture_getCategory },
{ "setMask", w_Fixture_setMask },
{ "getMask", w_Fixture_getMask },
{ "setUserData", w_Fixture_setUserData },
{ "getUserData", w_Fixture_getUserData },
{ "getBoundingBox", w_Fixture_getBoundingBox },
{ "getMassData", w_Fixture_getMassData },
{ "getGroupIndex", w_Fixture_getGroupIndex },
{ "setGroupIndex", w_Fixture_setGroupIndex },
{ "destroy", w_Fixture_destroy },
{ 0, 0 }
};
int luaopen_fixture(lua_State * L)
{
return luax_register_type(L, "Fixture", functions);
}
} // box2d
} // physics
} // love
+67
View File
@@ -0,0 +1,67 @@
/**
* 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_WRAP_FIXTURE_H
#define LOVE_PHYSICS_BOX2D_WRAP_FIXTURE_H
// LOVE
#include <common/runtime.h>
#include "Fixture.h"
namespace love
{
namespace physics
{
namespace box2d
{
Fixture * luax_checkfixture(lua_State * L, int idx);
int w_Fixture_getType(lua_State * L);
int w_Fixture_setFriction(lua_State * L);
int w_Fixture_setRestitution(lua_State * L);
int w_Fixture_setDensity(lua_State * L);
int w_Fixture_setSensor(lua_State * L);
int w_Fixture_getFriction(lua_State * L);
int w_Fixture_getRestitution(lua_State * L);
int w_Fixture_getDensity(lua_State * L);
int w_Fixture_isSensor(lua_State * L);
int w_Fixture_getBody(lua_State * L);
int w_Fixture_getShape(lua_State * L);
int w_Fixture_testPoint(lua_State * L);
int w_Fixture_rayCast(lua_State * L);
int w_Fixture_setFilterData(lua_State * L);
int w_Fixture_getFilterData(lua_State * L);
int w_Fixture_setCategory(lua_State * L);
int w_Fixture_getCategory(lua_State * L);
int w_Fixture_setMask(lua_State * L);
int w_Fixture_getMask(lua_State * L);
int w_Fixture_setUserData(lua_State * L);
int w_Fixture_getUserData(lua_State * L);
int w_Fixture_getBoundingBox(lua_State * L);
int w_Fixture_getMassData(lua_State * L);
int w_Fixture_getGroupIndex(lua_State * L);
int w_Fixture_setGroupIndex(lua_State * L);
int w_Fixture_destroy(lua_State * L);
int luaopen_fixture(lua_State * L);
} // box2d
} // physics
} // love
#endif // LOVE_PHYSICS_BOX2D_WRAP_FIXTURE_H
@@ -53,6 +53,15 @@ namespace box2d
luax_newtype(L, "Body", PHYSICS_BODY_T, (void*)body);
return 1;
}
int w_newFixture(lua_State * L)
{
Body * body = luax_checkbody(L, 1);
Shape * shape = luax_checkshape(L, 2);
Fixture * fixture = instance->newFixture(body, shape);
luax_newtype(L, "Fixture", PHYSICS_FIXTURE_T, (void*)fixture);
return 1;
}
int w_newCircleShape(lua_State * L)
{
+1
View File
@@ -52,6 +52,7 @@ namespace box2d
{
int w_newWorld(lua_State * L);
int w_newBody(lua_State * L);
int w_newFixture(lua_State * L);
int w_newCircleShape(lua_State * L);
int w_newRectangleShape(lua_State * L);
int w_newPolygonShape(lua_State * L);