mirror of
https://github.com/love2d/love.git
synced 2026-08-16 08:11:02 +02:00
making a stab at updating Shapes
--HG-- branch : box2d-update
This commit is contained in:
@@ -30,41 +30,28 @@ namespace physics
|
||||
{
|
||||
namespace box2d
|
||||
{
|
||||
CircleShape::CircleShape(Body * body, b2CircleDef * def)
|
||||
: Shape(body)
|
||||
CircleShape::CircleShape(b2CircleShape * c)
|
||||
{
|
||||
def->localPosition = body->world->scaleDown(def->localPosition);
|
||||
def->radius = body->world->scaleDown(def->radius);
|
||||
radius = def->radius;
|
||||
this->localPosition = def->localPosition;
|
||||
|
||||
def->userData = (void*)data;
|
||||
shape = body->body->CreateShape(def);
|
||||
shape = c;
|
||||
}
|
||||
|
||||
CircleShape::~CircleShape()
|
||||
{
|
||||
shape = NULL;
|
||||
}
|
||||
|
||||
float CircleShape::getRadius() const
|
||||
{
|
||||
return body->world->scaleUp(radius);
|
||||
}
|
||||
|
||||
void CircleShape::getLocalCenter(float & x, float & y) const
|
||||
{
|
||||
x = localPosition.x;
|
||||
y = localPosition.y;
|
||||
body->world->scaleUp(x, y);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
float CircleShape::getRadius() const
|
||||
{
|
||||
return shape->m_radius;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the radius for the circle.
|
||||
**/
|
||||
void CircleShape::setRadius(float r)
|
||||
{
|
||||
shape->m_radius = r;
|
||||
}
|
||||
|
||||
} // box2d
|
||||
} // physics
|
||||
|
||||
@@ -41,15 +41,6 @@ namespace box2d
|
||||
**/
|
||||
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:
|
||||
|
||||
/**
|
||||
@@ -58,7 +49,7 @@ namespace box2d
|
||||
* @param body The parent body.
|
||||
* @param def The CircleShape definition.
|
||||
**/
|
||||
CircleShape(Body * body, b2CircleDef * def);
|
||||
CircleShape(b2CircleShape * c);
|
||||
|
||||
virtual ~CircleShape();
|
||||
|
||||
@@ -66,11 +57,11 @@ namespace box2d
|
||||
* Gets the radius for the circle.
|
||||
**/
|
||||
float getRadius() const;
|
||||
|
||||
// There is no support for setting the radius.
|
||||
|
||||
void getLocalCenter(float & x, float & y) const;
|
||||
void getWorldCenter(float & x, float & y) const;
|
||||
|
||||
/**
|
||||
* Gets the radius for the circle.
|
||||
**/
|
||||
void setRadius(float r);
|
||||
};
|
||||
|
||||
} // box2d
|
||||
|
||||
@@ -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"
|
||||
@@ -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
|
||||
@@ -78,41 +78,40 @@ namespace box2d
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
def.friction = 0.5f;
|
||||
def.restitution = 0.1f;
|
||||
def.density = 1.0f;
|
||||
def.SetAsBox(w/2.0f, h/2.0f, b2Vec2(x, y), angle);
|
||||
return new PolygonShape(body, &def);
|
||||
b2PolygonShape s;
|
||||
s.SetAsBox(w/2.0f, h/2.0f, b2Vec2(x, y), angle);
|
||||
return new PolygonShape(&s);
|
||||
}
|
||||
|
||||
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 argc = lua_gettop(L);
|
||||
int vcount = (int)(argc-1)/2;
|
||||
// 1 body + 3 vertices
|
||||
love::luax_assert_argc(L, 1 + (2 * 3));
|
||||
int vcount = (int)argc/2;
|
||||
// 3 vertices
|
||||
love::luax_assert_argc(L, 2 * 3);
|
||||
|
||||
Body * b = luax_checkbody(L, 1);
|
||||
b2PolygonShape s;
|
||||
|
||||
b2PolygonDef def;
|
||||
def.friction = 0.5f;
|
||||
def.restitution = 0.1f;
|
||||
def.density = 1.0f;
|
||||
|
||||
std::vector<point2d> points(def.vertexCount);
|
||||
std::vector<point2d> points(s.m_vertexCount);
|
||||
std::vector<point2d> convex_hull;
|
||||
|
||||
for(int i = 0;i<vcount;i++)
|
||||
@@ -126,16 +125,22 @@ namespace box2d
|
||||
|
||||
// Compute convex hull.
|
||||
GrahamScanConvexHull()(points, convex_hull);
|
||||
|
||||
int count = convex_hull.size();
|
||||
|
||||
def.vertexCount = (int32)convex_hull.size();
|
||||
|
||||
if(def.vertexCount < 3)
|
||||
if(count < 3)
|
||||
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++)
|
||||
def.vertices[def.vertexCount-i-1].Set((float)convex_hull[i].x, (float)convex_hull[i].y);
|
||||
|
||||
PolygonShape * p = new PolygonShape(b, &def);
|
||||
PolygonShape * p = new PolygonShape(&s);
|
||||
delete[] vecs;
|
||||
|
||||
luax_newtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, (void*)p);
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ namespace box2d
|
||||
* @param body The Body to create the Shape on.
|
||||
* @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.
|
||||
@@ -101,7 +101,7 @@ namespace box2d
|
||||
* @param y The offset along the y-axis.
|
||||
* @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
|
||||
@@ -109,7 +109,7 @@ namespace box2d
|
||||
* @param w The width 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
|
||||
@@ -119,7 +119,7 @@ namespace box2d
|
||||
* @param w The width 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
|
||||
@@ -130,7 +130,18 @@ namespace box2d
|
||||
* @param h The height of the rectangle.
|
||||
* @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.
|
||||
|
||||
@@ -30,14 +30,9 @@ namespace physics
|
||||
{
|
||||
namespace box2d
|
||||
{
|
||||
PolygonShape::PolygonShape(Body * body, b2PolygonDef * def)
|
||||
: Shape(body)
|
||||
PolygonShape::PolygonShape(b2PolygonShape * p)
|
||||
{
|
||||
for(int i = 0; i<def->vertexCount; i++)
|
||||
def->vertices[i] = body->world->scaleDown(def->vertices[i]);
|
||||
|
||||
def->userData = (void*)data;
|
||||
shape = body->body->CreateShape(def);
|
||||
shape = p;
|
||||
}
|
||||
|
||||
PolygonShape::~PolygonShape()
|
||||
@@ -48,11 +43,10 @@ namespace box2d
|
||||
{
|
||||
love::luax_assert_argc(L, 0);
|
||||
b2PolygonShape * p = (b2PolygonShape *)shape;
|
||||
const b2Vec2 * vertices = p->GetVertices();
|
||||
int count = p->GetVertexCount();
|
||||
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.y);
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace box2d
|
||||
* @param body The parent Body.
|
||||
* @param def The polygon definition.
|
||||
**/
|
||||
PolygonShape(Body * body, b2PolygonDef * def);
|
||||
PolygonShape(b2PolygonShape * p);
|
||||
|
||||
virtual ~PolygonShape();
|
||||
|
||||
|
||||
@@ -33,265 +33,29 @@ namespace physics
|
||||
{
|
||||
namespace box2d
|
||||
{
|
||||
Shape::Shape(Body * body)
|
||||
: body(body), shape(NULL)
|
||||
Shape::Shape()
|
||||
: shape(NULL)
|
||||
{
|
||||
body->retain();
|
||||
data = new shapeudata();
|
||||
data->ref = 0;
|
||||
}
|
||||
|
||||
Shape::~Shape()
|
||||
{
|
||||
if(data->ref != 0)
|
||||
delete data->ref;
|
||||
|
||||
delete data;
|
||||
data = 0;
|
||||
|
||||
if (shape)
|
||||
body->body->DestroyShape(shape);
|
||||
shape = 0;
|
||||
|
||||
body->release();
|
||||
}
|
||||
|
||||
Shape::Type Shape::getType() const
|
||||
{
|
||||
switch(shape->GetType())
|
||||
{
|
||||
case e_circleShape:
|
||||
case b2Shape::e_circle:
|
||||
return SHAPE_CIRCLE;
|
||||
case e_polygonShape:
|
||||
case b2Shape::e_polygon:
|
||||
return SHAPE_POLYGON;
|
||||
default:
|
||||
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
|
||||
} // physics
|
||||
} // love
|
||||
|
||||
@@ -35,20 +35,9 @@ namespace physics
|
||||
{
|
||||
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 Shape's geometry will be affected by the parent
|
||||
* body's transformation.
|
||||
@@ -57,26 +46,15 @@ namespace box2d
|
||||
{
|
||||
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.
|
||||
b2Shape * shape;
|
||||
|
||||
// Our shape data, to be stored in
|
||||
// the Box2D shape.
|
||||
shapeudata * data;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Creates a Shape attatched to the specified
|
||||
* Body.
|
||||
* Creates a Shape.
|
||||
**/
|
||||
Shape(Body * body);
|
||||
Shape();
|
||||
|
||||
virtual ~Shape();
|
||||
|
||||
@@ -85,118 +63,6 @@ namespace box2d
|
||||
* debug drawing.
|
||||
**/
|
||||
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
|
||||
|
||||
@@ -38,54 +38,19 @@ namespace box2d
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_CircleShape_getLocalCenter(lua_State * L)
|
||||
{
|
||||
CircleShape * c = luax_checkcircleshape(L, 1);
|
||||
float x, y;
|
||||
c->getLocalCenter(x, y);
|
||||
lua_pushnumber(L, x);
|
||||
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;
|
||||
int w_CircleShape_setRadius(lua_State * L)
|
||||
{
|
||||
CircleShape * c = luax_checkcircleshape(L, 1);
|
||||
float r = (float)luaL_checknumber(L, 2);
|
||||
c->setRadius(r);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const luaL_Reg functions[] = {
|
||||
{ "getRadius", w_CircleShape_getRadius },
|
||||
{ "getLocalCenter", w_CircleShape_getLocalCenter },
|
||||
{ "getWorldCenter", w_CircleShape_getWorldCenter },
|
||||
{ "setRadius", w_CircleShape_setRadius },
|
||||
// From Shape.
|
||||
{ "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 },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
@@ -34,8 +34,7 @@ namespace box2d
|
||||
{
|
||||
CircleShape * luax_checkcircleshape(lua_State * L, int idx);
|
||||
int w_CircleShape_getRadius(lua_State * L);
|
||||
int w_CircleShape_getLocalCenter(lua_State * L);
|
||||
int w_CircleShape_getWorldCenter(lua_State * L);
|
||||
int w_CircleShape_setRadius(lua_State * L);
|
||||
int luaopen_circleshape(lua_State * L);
|
||||
|
||||
} // box2d
|
||||
|
||||
@@ -42,28 +42,6 @@ namespace box2d
|
||||
{ "getPoints", w_PolygonShape_getPoints },
|
||||
// From Shape.
|
||||
{ "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 },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
@@ -41,180 +41,6 @@ namespace box2d
|
||||
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)
|
||||
{
|
||||
Proxy * p = (Proxy *)lua_touserdata(L, 1);
|
||||
@@ -227,28 +53,6 @@ namespace box2d
|
||||
|
||||
static const luaL_Reg functions[] = {
|
||||
{ "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 },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
@@ -33,28 +33,6 @@ namespace box2d
|
||||
{
|
||||
Shape * luax_checkshape(lua_State * L, int idx);
|
||||
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 luaopen_shape(lua_State * L);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user