Skip to content

Commit

Permalink
Make entity selection and collision boxes independently settable (#6218)
Browse files Browse the repository at this point in the history
* Make entity selection and collision boxes independently settable
  • Loading branch information
stujones11 authored and nerzhul committed Aug 24, 2017
1 parent 01c319d commit ac4884c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 9 deletions.
3 changes: 3 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -3986,6 +3986,9 @@ Definition tables
collide_with_objects = true, -- collide with other objects if physical = true
weight = 5,
collisionbox = {-0.5, 0.0, -0.5, 0.5, 1.0, 0.5},
selectionbox = {-0.5, 0.0, -0.5, 0.5, 1.0, 0.5},
-- ^ Default, uses collision box dimensions when not set
pointable = true, -- overrides selection box when false
visual = "cube" / "sprite" / "upright_sprite" / "mesh" / "wielditem",
visual_size = {x = 1, y = 1},
mesh = "model",
Expand Down
9 changes: 6 additions & 3 deletions src/content_cao.cpp
Expand Up @@ -346,7 +346,7 @@ GenericCAO::~GenericCAO()
bool GenericCAO::getSelectionBox(aabb3f *toset) const
{
if (!m_prop.is_visible || !m_is_visible || m_is_local_player
|| getParent() != NULL){
|| !m_prop.pointable || getParent() != NULL) {
return false;
}
*toset = m_selection_box;
Expand Down Expand Up @@ -1226,7 +1226,7 @@ void GenericCAO::processMessage(const std::string &data)
if (cmd == GENERIC_CMD_SET_PROPERTIES) {
m_prop = gob_read_set_properties(is);

m_selection_box = m_prop.collisionbox;
m_selection_box = m_prop.selectionbox;
m_selection_box.MinEdge *= BS;
m_selection_box.MaxEdge *= BS;

Expand All @@ -1240,7 +1240,10 @@ void GenericCAO::processMessage(const std::string &data)
if (m_is_local_player) {
LocalPlayer *player = m_env->getLocalPlayer();
player->makes_footstep_sound = m_prop.makes_footstep_sound;
player->setCollisionbox(m_selection_box);
aabb3f collision_box = m_prop.collisionbox;
collision_box.MinEdge *= BS;
collision_box.MaxEdge *= BS;
player->setCollisionbox(collision_box);
}

if ((m_is_player && !m_is_local_player) && m_prop.nametag.empty())
Expand Down
14 changes: 8 additions & 6 deletions src/content_sao.cpp
Expand Up @@ -752,12 +752,12 @@ bool LuaEntitySAO::getCollisionBox(aabb3f *toset) const

bool LuaEntitySAO::getSelectionBox(aabb3f *toset) const
{
if (!m_prop.is_visible) {
if (!m_prop.is_visible || !m_prop.pointable) {
return false;
}

toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
toset->MinEdge = m_prop.selectionbox.MinEdge * BS;
toset->MaxEdge = m_prop.selectionbox.MaxEdge * BS;

return true;
}
Expand Down Expand Up @@ -786,6 +786,8 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id
m_prop.physical = false;
m_prop.weight = 75;
m_prop.collisionbox = aabb3f(-0.3f, 0.0f, -0.3f, 0.3f, 1.77f, 0.3f);
m_prop.selectionbox = aabb3f(-0.3f, 0.0f, -0.3f, 0.3f, 1.77f, 0.3f);
m_prop.pointable = true;
// start of default appearance, this should be overwritten by LUA
m_prop.visual = "upright_sprite";
m_prop.visual_size = v2f(1, 2);
Expand Down Expand Up @@ -1426,12 +1428,12 @@ bool PlayerSAO::getCollisionBox(aabb3f *toset) const

bool PlayerSAO::getSelectionBox(aabb3f *toset) const
{
if (!m_prop.is_visible) {
if (!m_prop.is_visible || !m_prop.pointable) {
return false;
}

toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
toset->MinEdge = m_prop.selectionbox.MinEdge * BS;
toset->MaxEdge = m_prop.selectionbox.MaxEdge * BS;

return true;
}
8 changes: 8 additions & 0 deletions src/object_properties.cpp
Expand Up @@ -61,6 +61,8 @@ std::string ObjectProperties::dump()
os << ", nametag=" << nametag;
os << ", nametag_color=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
<< "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
os << ", selectionbox=" << PP(selectionbox.MinEdge) << "," << PP(selectionbox.MaxEdge);
os << ", pointable=" << pointable;
return os.str();
}

Expand Down Expand Up @@ -99,6 +101,9 @@ void ObjectProperties::serialize(std::ostream &os) const
writeF1000(os, automatic_face_movement_max_rotation_per_sec);
os << serializeString(infotext);
os << serializeString(wield_item);
writeV3F1000(os, selectionbox.MinEdge);
writeV3F1000(os, selectionbox.MaxEdge);
writeU8(os, pointable);

// Add stuff only at the bottom.
// Never remove anything, because we don't want new versions of this
Expand Down Expand Up @@ -142,6 +147,9 @@ void ObjectProperties::deSerialize(std::istream &is)
automatic_face_movement_max_rotation_per_sec = readF1000(is);
infotext = deSerializeString(is);
wield_item = deSerializeString(is);
selectionbox.MinEdge = readV3F1000(is);
selectionbox.MaxEdge = readV3F1000(is);
pointable = readU8(is);
}catch(SerializationError &e){}
}
else
Expand Down
2 changes: 2 additions & 0 deletions src/object_properties.h
Expand Up @@ -33,6 +33,8 @@ struct ObjectProperties
bool collideWithObjects = true;
float weight = 5.0f;
aabb3f collisionbox = aabb3f(-0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f);
aabb3f selectionbox = aabb3f(-0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f);
bool pointable = true;
std::string visual = "sprite";
std::string mesh = "";
v2f visual_size = v2f(1, 1);
Expand Down
11 changes: 11 additions & 0 deletions src/script/common/c_content.cpp
Expand Up @@ -197,6 +197,13 @@ void read_object_properties(lua_State *L, int index,
prop->collisionbox = read_aabb3f(L, -1, 1.0);
lua_pop(L, 1);

lua_getfield(L, -1, "selectionbox");
if (lua_istable(L, -1))
prop->selectionbox = read_aabb3f(L, -1, 1.0);
else
prop->selectionbox = prop->collisionbox;
lua_pop(L, 1);
getboolfield(L, -1, "pointable", prop->pointable);
getstringfield(L, -1, "visual", prop->visual);

getstringfield(L, -1, "mesh", prop->mesh);
Expand Down Expand Up @@ -296,6 +303,10 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
lua_setfield(L, -2, "weight");
push_aabb3f(L, prop->collisionbox);
lua_setfield(L, -2, "collisionbox");
push_aabb3f(L, prop->selectionbox);
lua_setfield(L, -2, "selectionbox");
lua_pushboolean(L, prop->pointable);
lua_setfield(L, -2, "pointable");
lua_pushlstring(L, prop->visual.c_str(), prop->visual.size());
lua_setfield(L, -2, "visual");
lua_pushlstring(L, prop->mesh.c_str(), prop->mesh.size());
Expand Down

0 comments on commit ac4884c

Please sign in to comment.