I am working on ne for collisions.All work ok, before i need to call object function.Getting variable work.I don’t know c++, and i don’t have any idea what i am doing wrong.Can some help me.
I think that problem is with push_shape or check_shape function. Maybe with casting userdata to object.
Programm is stopping on line shape->updateBbox();.
static Shape *pushRectShape (lua_State *L, RectangleShape *im){
RectangleShape *pi = (RectangleShape *)lua_newuserdata(L, sizeof(RectangleShape));
dmLogInfo("3")
im->updateBbox();
dmLogInfo("4")
*pi = *im;
dmLogInfo("4.2")
Shape *shape = dynamic_cast<Shape*>(pi);
dmLogInfo("4.3");
dmLogInfo("%f", shape->x);
shape->updateBbox();
dmLogInfo("4.5")
pi->updateBbox();
dmLogInfo("5")
luaL_getmetatable(L, SHAPE);
lua_setmetatable(L, -2);
return pi;
}
#pragma once
#include <unordered_map>
#include <set>
#include <dmsdk/dlib/log.h>
#define CIRCLE 1
#define hash(x, y) (((int)x/cell_size)+ ((int)y/cell_size)*256)
#define SHAPE "Shape"
class Shape
{
public:
double x,y;
double bboxX1, bboxX2, bboxY1, bboxY2;
double rotation;
double scale;
int group;
int mask; //collision mask
int type;
Shape(double x1, double y1, int type1){
x = x1;
y = y1;
scale = 1;
type = type1;
}
virtual void updateBbox(){
//bboxX1 = x;
//bboxX2 = x;
//bboxY1 = y;
//bboxY2 = y;
}
virtual void setScale(double newScale){
scale = newScale;
}
virtual void setRotation(double newRotation, double x, double y){
rotation = newRotation;
}
virtual void setPosition(double newX, double newY){
x = newX;
y = newY;
}
};
class RectangleShape: public Shape {
public:
double width, height;
RectangleShape(double x1, double y1, double width1, double height1): Shape(x1,y1,1){
width = width1;
height = height1;
}
void setScale(double newScale){
width = width * newScale/scale;
height = height * newScale/scale;
scale = newScale;
}
void updateBbox(){
//bboxX1 = x-width/2;
//bboxX2 = x+width/2;
//bboxY1 = y-height/2;
//bboxY2 = y+height/2;
}
};
class CircleShape: public Shape {
public:
double rad;
CircleShape(double x1, double y1, double radd): Shape(x1,y1,2){
rad = radd;
}
void setScale(double newScale){
rad = rad * newScale/scale;
scale = newScale;
}
void updateBbox(){
bboxX1 = x-rad/2;
bboxX2 = x+rad/2;
bboxY1 = y-rad/2;
bboxY2 = y+rad/2;
}
};
static Shape* checkShape (lua_State *L, int index){
luaL_checktype(L, index, LUA_TUSERDATA);
//void* storage = lua_touserdata(L, 1);
Shape *shape = static_cast<Shape*>(luaL_checkudata(L, index, SHAPE));
shape->updateBbox();
//Shape *shape = *static_cast<Shape**>(storage);
if (shape == NULL) luaL_typerror(L, index, SHAPE);
return shape;
}
static Shape *pushRectShape (lua_State *L, RectangleShape *im){
RectangleShape *pi = (RectangleShape *)lua_newuserdata(L, sizeof(RectangleShape));
dmLogInfo("3");
im->updateBbox();
dmLogInfo("4");
*pi = *im;
dmLogInfo("4.2");
Shape *shape = dynamic_cast<Shape*>(pi);
dmLogInfo("4.3");
dmLogInfo("%f", shape->x);
shape->updateBbox();
dmLogInfo("4.5");
pi->updateBbox();
dmLogInfo("5");
luaL_getmetatable(L, SHAPE);
lua_setmetatable(L, -2);
return pi;
}
static Shape *pushCircleShape (lua_State *L, CircleShape *im){
CircleShape *pi = (CircleShape *)lua_newuserdata(L, sizeof(CircleShape));
*pi = *im;
luaL_getmetatable(L, SHAPE);
lua_setmetatable(L, -2);
return pi;
}
static int ShapeRect_new (lua_State *L){
double x = luaL_checknumber(L, 1);
double y = luaL_checknumber(L, 2);
double width = luaL_checknumber(L, 3);
double height = luaL_checknumber(L, 4);
RectangleShape rect = RectangleShape(x, y, width, height);
dmLogInfo("1")
rect.updateBbox();
dmLogInfo("2");
(&rect)->updateBbox();
dmLogInfo("2.4");
pushRectShape(L, &rect);
return 1;
}
static int ShapeCircle_new (lua_State *L){
double x = luaL_checknumber(L, 1);
double y = luaL_checknumber(L, 2);
double rad = luaL_checknumber(L, 3);
CircleShape circle = CircleShape(x, y, rad);
pushCircleShape(L, &circle);
return 1;
}
static int Shape_get_position (lua_State *L){
Shape *im = checkShape(L, 1);
lua_pushnumber(L, im->x);
lua_pushnumber(L, im->y);
return 2;
}
static int Shape_set_position (lua_State *L){
Shape *shape = checkShape(L, 1);
double x = luaL_checknumber(L, 2);
double y = luaL_checknumber(L, 3);
shape->x = x;
shape->y = y;
return 0;
}
static int Shape_get_bbox (lua_State *L){
Shape *im = checkShape(L, 1);
lua_pushnumber(L, im->bboxX1);
lua_pushnumber(L, im->bboxY1);
lua_pushnumber(L, im->bboxX2);
lua_pushnumber(L, im->bboxY2);
return 4;
}
static int Shape_set_rotation (lua_State *L){
Shape *shape = checkShape(L, 1);
double rot = luaL_checknumber(L, 2);
shape->rotation = rot;
return 0;
}
static int Shape_get_rotation (lua_State *L){
Shape *im = checkShape(L, 1);
lua_pushnumber(L, im->rotation);
return 1;
}
static int Shape_get_type (lua_State *L){
Shape *im = checkShape(L, 1);
lua_pushnumber(L, im->type);
return 1;
}
static int Shape_get_size(lua_State *L){
Shape* shape = checkShape(L, 1);
if (shape->type == 1){
RectangleShape* rect = (RectangleShape*)(shape);
lua_pushnumber(L, rect->width);
lua_pushnumber(L, rect->height);
return 2;
}else if (shape->type == 2){
CircleShape* circle = (CircleShape*)(shape);
lua_pushnumber(L, circle->rad);
return 1;
}
return 0;
}
int cell_size;
int width;
std::unordered_map<long, std::set<Shape*>> MAP;
std::set<Shape*> SHAPES;
void init(int cell_size){
cell_size = cell_size;
MAP.clear();
SHAPES.clear();
}
static void World_add(Shape *shape){
SHAPES.insert(shape);
}
static void World_remove(Shape *shape){
SHAPES.erase(shape);
}
static int World_add_lua(lua_State *L){
Shape* shape = checkShape(L, 1);
World_add(shape);
return 0;
}
static int World_remove_lua(lua_State *L){
Shape* shape = checkShape(L, 1);
World_remove(shape);
return 0;
}
static int World_update(lua_State *L){
for(Shape* shape : SHAPES) {
dmLogInfo("here")
dmLogInfo("x:%f", shape->x)
dmLogInfo("before update BBox")
shape->updateBbox();
dmLogInfo("after update BBox")
}
return 0;
}
static int World_get_shapes_size(lua_State *L){
lua_pushnumber(L, SHAPES.size());
return 1;
}
static int Shape_dispose(lua_State *L){
Shape *shape = checkShape(L, 1);
lua_pushnil(L);
lua_setmetatable(L,1);
World_remove(shape);
return 0;
}
static int Shape_gc (lua_State *L){
Shape *shape = checkShape(L, 1);
World_remove(shape);
return 0;
}
static int Shape_tostring (lua_State *L){
Shape *shape = checkShape(L, 1);
lua_pushfstring(L, "Shape(%f %f)", shape->x, shape->y);
return 1;
}
static const luaL_reg Shape_methods[] = {
{"new_rect", ShapeRect_new},
{"new_circle", ShapeCircle_new},
{"get_size", Shape_get_size},
{"get_position", Shape_get_position},
{"set_position", Shape_set_position},
{"get_rotation", Shape_get_rotation},
{"set_rotation", Shape_set_rotation},
{"get_type", Shape_get_type},
{"update",World_update},
{"remove", World_remove_lua},
{"add", World_add_lua},
{"dispose", Shape_dispose},
{"get_shapes_size", World_get_shapes_size},
{0,0}
};
static const luaL_reg Shape_meta[] = {
{"__gc", Shape_gc},
{"__tostring", Shape_tostring},
{0, 0}
};
int Shape_register (lua_State *L)
{
luaL_register(L, SHAPE, Shape_methods); // create methods table,
luaL_newmetatable(L, SHAPE);
luaL_register(L, NULL, Shape_meta);
lua_pushliteral(L, "__index");
lua_pushvalue(L, -3);
lua_rawset(L, -3);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, -3);
lua_rawset(L, -3);
lua_pop(L, 1);
lua_pop(L, 1);
return 0;
}