Skip to content

Commit

Permalink
Add an option to disable object <-> object collision for Lua entities
Browse files Browse the repository at this point in the history
  • Loading branch information
PilzAdam authored and RealBadAngel committed Jul 20, 2013
1 parent 413f0d0 commit 8cae659
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 5 deletions.
1 change: 1 addition & 0 deletions builtin/falling.lua
Expand Up @@ -7,6 +7,7 @@
minetest.register_entity("__builtin:falling_node", {
initial_properties = {
physical = true,
collide_with_objects = false,
collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
visual = "wielditem",
textures = {},
Expand Down
1 change: 1 addition & 0 deletions builtin/item_entity.lua
Expand Up @@ -12,6 +12,7 @@ minetest.register_entity("__builtin:item", {
initial_properties = {
hp_max = 1,
physical = true,
collide_with_objects = false,
collisionbox = {-0.17,-0.17,-0.17, 0.17,0.17,0.17},
visual = "sprite",
visual_size = {x=0.5, y=0.5},
Expand Down
1 change: 1 addition & 0 deletions doc/lua_api.txt
Expand Up @@ -1812,6 +1812,7 @@ Object Properties
{
hp_max = 1,
physical = true,
collide_with_objects = true, -- collide with other objects if physical=true
weight = 5,
collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
visual = "cube"/"sprite"/"upright_sprite"/"mesh",
Expand Down
1 change: 1 addition & 0 deletions src/activeobject.h
Expand Up @@ -62,6 +62,7 @@ class ActiveObject

virtual u8 getType() const = 0;
virtual bool getCollisionBox(aabb3f *toset) = 0;
virtual bool collideWithObjects() = 0;
protected:
u16 m_id; // 0 is invalid, "no id"
};
Expand Down
1 change: 1 addition & 0 deletions src/clientobject.h
Expand Up @@ -56,6 +56,7 @@ class ClientActiveObject : public ActiveObject
virtual v3s16 getLightPosition(){return v3s16(0,0,0);}
virtual core::aabbox3d<f32>* getSelectionBox(){return NULL;}
virtual core::aabbox3d<f32>* getCollisionBox(){return NULL;}
virtual bool collideWithObjects(){return false;}
virtual v3f getPosition(){return v3f(0,0,0);}
virtual scene::IMeshSceneNode *getMeshSceneNode(){return NULL;}
virtual scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode(){return NULL;}
Expand Down
8 changes: 6 additions & 2 deletions src/collision.cpp
Expand Up @@ -196,7 +196,9 @@ bool wouldCollideWithCeiling(
collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
f32 pos_max_d, const aabb3f &box_0,
f32 stepheight, f32 dtime,
v3f &pos_f, v3f &speed_f, v3f &accel_f,ActiveObject* self)
v3f &pos_f, v3f &speed_f,
v3f &accel_f,ActiveObject* self,
bool collideWithObjects)
{
Map *map = &env->getMap();
//TimeTaker tt("collisionMoveSimple");
Expand Down Expand Up @@ -287,6 +289,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
}
} // tt2

if(collideWithObjects)
{
ScopeProfiler sp(g_profiler, "collisionMoveSimple objects avg", SPT_AVG);
//TimeTaker tt3("collisionMoveSimple collect object boxes");
Expand Down Expand Up @@ -334,7 +337,8 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
if (object != NULL)
{
aabb3f object_collisionbox;
if (object->getCollisionBox(&object_collisionbox))
if (object->getCollisionBox(&object_collisionbox) &&
object->collideWithObjects())
{
cboxes.push_back(object_collisionbox);
is_unloaded.push_back(false);
Expand Down
4 changes: 3 additions & 1 deletion src/collision.h
Expand Up @@ -71,7 +71,9 @@ struct collisionMoveResult
collisionMoveResult collisionMoveSimple(Environment *env,IGameDef *gamedef,
f32 pos_max_d, const aabb3f &box_0,
f32 stepheight, f32 dtime,
v3f &pos_f, v3f &speed_f, v3f &accel_f,ActiveObject* self=0);
v3f &pos_f, v3f &speed_f,
v3f &accel_f,ActiveObject* self=0,
bool collideWithObjects=true);

#if 0
// This doesn't seem to work and isn't used
Expand Down
7 changes: 6 additions & 1 deletion src/content_cao.cpp
Expand Up @@ -661,6 +661,10 @@ class GenericCAO : public ClientActiveObject
return false;
}

bool collideWithObjects() {
return m_prop.collideWithObjects;
}

void initialize(const std::string &data)
{
infostream<<"GenericCAO: Got init data"<<std::endl;
Expand Down Expand Up @@ -1152,7 +1156,8 @@ class GenericCAO : public ClientActiveObject
v3f p_acceleration = m_acceleration;
moveresult = collisionMoveSimple(env,env->getGameDef(),
pos_max_d, box, stepheight, dtime,
p_pos, p_velocity, p_acceleration,this);
p_pos, p_velocity, p_acceleration,
this, m_prop.collideWithObjects);
// Apply results
m_position = p_pos;
m_velocity = p_velocity;
Expand Down
22 changes: 21 additions & 1 deletion src/content_sao.cpp
Expand Up @@ -68,6 +68,10 @@ class DummyLoadSAO : public ServerActiveObject
return false;
}

bool collideWithObjects() {
return false;
}

private:
};

Expand Down Expand Up @@ -140,6 +144,10 @@ class TestSAO : public ServerActiveObject
return false;
}

bool collideWithObjects() {
return false;
}

private:
float m_timer1;
float m_age;
Expand Down Expand Up @@ -325,6 +333,9 @@ class ItemSAO : public ServerActiveObject
return false;
}

bool collideWithObjects() {
return false;
}

private:
std::string m_itemstring;
Expand Down Expand Up @@ -500,7 +511,8 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
v3f p_acceleration = m_acceleration;
moveresult = collisionMoveSimple(m_env,m_env->getGameDef(),
pos_max_d, box, stepheight, dtime,
p_pos, p_velocity, p_acceleration,this);
p_pos, p_velocity, p_acceleration,
this, m_prop.collideWithObjects);
// Apply results
m_base_position = p_pos;
m_velocity = p_velocity;
Expand Down Expand Up @@ -905,6 +917,10 @@ bool LuaEntitySAO::getCollisionBox(aabb3f *toset) {
return false;
}

bool LuaEntitySAO::collideWithObjects(){
return m_prop.collideWithObjects;
}

/*
PlayerSAO
*/
Expand Down Expand Up @@ -1496,3 +1512,7 @@ bool PlayerSAO::getCollisionBox(aabb3f *toset) {

return true;
}

bool PlayerSAO::collideWithObjects(){
return true;
}
2 changes: 2 additions & 0 deletions src/content_sao.h
Expand Up @@ -79,6 +79,7 @@ class LuaEntitySAO : public ServerActiveObject
bool select_horiz_by_yawpitch);
std::string getName();
bool getCollisionBox(aabb3f *toset);
bool collideWithObjects();
private:
std::string getPropertyPacket();
void sendPosition(bool do_interpolate, bool is_movement_end);
Expand Down Expand Up @@ -238,6 +239,7 @@ class PlayerSAO : public ServerActiveObject
}

bool getCollisionBox(aabb3f *toset);
bool collideWithObjects();

private:
std::string getPropertyPacket();
Expand Down
4 changes: 4 additions & 0 deletions src/object_properties.cpp
Expand Up @@ -29,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
ObjectProperties::ObjectProperties():
hp_max(1),
physical(false),
collideWithObjects(true),
weight(5),
collisionbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5),
visual("sprite"),
Expand All @@ -49,6 +50,7 @@ std::string ObjectProperties::dump()
std::ostringstream os(std::ios::binary);
os<<"hp_max="<<hp_max;
os<<", physical="<<physical;
os<<", collideWithObjects="<<collideWithObjects;
os<<", weight="<<weight;
os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
os<<", visual="<<visual;
Expand Down Expand Up @@ -97,6 +99,7 @@ void ObjectProperties::serialize(std::ostream &os) const
for(u32 i=0; i<colors.size(); i++){
writeARGB8(os, colors[i]);
}
writeU8(os, collideWithObjects);
// Add stuff only at the bottom.
// Never remove anything, because we don't want new versions of this
}
Expand Down Expand Up @@ -129,6 +132,7 @@ void ObjectProperties::deSerialize(std::istream &is)
for(u32 i=0; i<color_count; i++){
colors.push_back(readARGB8(is));
}
collideWithObjects = readU8(is);
}catch(SerializationError &e){}
}
else
Expand Down
1 change: 1 addition & 0 deletions src/object_properties.h
Expand Up @@ -31,6 +31,7 @@ struct ObjectProperties
// Values are BS=1
s16 hp_max;
bool physical;
bool collideWithObjects;
float weight;
core::aabbox3d<f32> collisionbox;
std::string visual;
Expand Down
1 change: 1 addition & 0 deletions src/script/common/c_content.cpp
Expand Up @@ -123,6 +123,7 @@ void read_object_properties(lua_State *L, int index,
prop->hp_max = getintfield_default(L, -1, "hp_max", 10);

getboolfield(L, -1, "physical", prop->physical);
getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);

getfloatfield(L, -1, "weight", prop->weight);

Expand Down
1 change: 1 addition & 0 deletions src/script/cpp_api/s_entity.cpp
Expand Up @@ -169,6 +169,7 @@ void ScriptApiEntity::luaentity_GetProperties(u16 id,
prop->hp_max = getintfield_default(L, -1, "hp_max", 10);

getboolfield(L, -1, "physical", prop->physical);
getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);

getfloatfield(L, -1, "weight", prop->weight);

Expand Down

0 comments on commit 8cae659

Please sign in to comment.