Skip to content

Commit aa8df11

Browse files
committedDec 7, 2019
Attachments: Fix interpolation from (0,0,0) after detach
GenericCAO::getPosition() did not take the camera offset into account LocalPlayer attachment cleanup: Use sane getParent() function Make that getPosition() (GenericCAO and LocalPlayer) always return the absolute position
1 parent 51f2308 commit aa8df11

File tree

7 files changed

+57
-61
lines changed

7 files changed

+57
-61
lines changed
 

‎src/client/camera.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,13 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_r
285285
// Smooth the movement when walking up stairs
286286
v3f old_player_position = m_playernode->getPosition();
287287
v3f player_position = player->getPosition();
288-
if (player->isAttached && player->parent)
289-
player_position = player->parent->getPosition();
290-
//if(player->touching_ground && player_position.Y > old_player_position.Y)
288+
289+
// This is worse than `LocalPlayer::getPosition()` but
290+
// mods expect the player head to be at the parent's position
291+
// plus eye height.
292+
if (player->getParent())
293+
player_position = player->getParent()->getPosition();
294+
291295
if(player->touching_ground &&
292296
player_position.Y > old_player_position.Y)
293297
{

‎src/client/clientobject.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ class ClientActiveObject : public ActiveObject
4949
virtual bool getSelectionBox(aabb3f *toset) const { return false; }
5050
virtual bool collideWithObjects() const { return false; }
5151
virtual const v3f getPosition() const { return v3f(0.0f); }
52-
virtual scene::ISceneNode *getSceneNode() { return NULL; }
53-
virtual scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode() { return NULL; }
52+
virtual scene::ISceneNode *getSceneNode() const
53+
{ return NULL; }
54+
virtual scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode() const
55+
{ return NULL; }
5456
virtual bool isLocalPlayer() const { return false; }
5557

5658
virtual ClientActiveObject *getParent() const { return nullptr; };

‎src/client/content_cao.cpp

+30-42
Original file line numberDiff line numberDiff line change
@@ -403,21 +403,25 @@ bool GenericCAO::getSelectionBox(aabb3f *toset) const
403403

404404
const v3f GenericCAO::getPosition() const
405405
{
406-
if (getParent() != nullptr) {
407-
if (m_matrixnode)
408-
return m_matrixnode->getAbsolutePosition();
406+
if (!getParent())
407+
return pos_translator.val_current;
409408

410-
return m_position;
409+
// Calculate real position in world based on MatrixNode
410+
if (m_matrixnode) {
411+
v3s16 camera_offset = m_env->getCameraOffset();
412+
return m_matrixnode->getAbsolutePosition() +
413+
intToFloat(camera_offset, BS);
411414
}
412-
return pos_translator.val_current;
415+
416+
return m_position;
413417
}
414418

415419
const bool GenericCAO::isImmortal()
416420
{
417421
return itemgroup_get(getGroups(), "immortal");
418422
}
419423

420-
scene::ISceneNode* GenericCAO::getSceneNode()
424+
scene::ISceneNode *GenericCAO::getSceneNode() const
421425
{
422426
if (m_meshnode) {
423427
return m_meshnode;
@@ -437,7 +441,7 @@ scene::ISceneNode* GenericCAO::getSceneNode()
437441
return NULL;
438442
}
439443

440-
scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
444+
scene::IAnimatedMeshSceneNode *GenericCAO::getAnimatedMeshSceneNode() const
441445
{
442446
return m_animated_meshnode;
443447
}
@@ -576,11 +580,15 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
576580
video::E_MATERIAL_TYPE material_type = (m_prop.use_texture_alpha) ?
577581
video::EMT_TRANSPARENT_ALPHA_CHANNEL : video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
578582

579-
if (m_prop.visual == "sprite") {
580-
infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
583+
auto grabMatrixNode = [this] {
584+
infostream << "GenericCAO::addToScene(): " << m_prop.visual << std::endl;
581585
m_matrixnode = RenderingEngine::get_scene_manager()->
582586
addDummyTransformationSceneNode();
583587
m_matrixnode->grab();
588+
};
589+
590+
if (m_prop.visual == "sprite") {
591+
getMatrixNode();
584592
m_spritenode = RenderingEngine::get_scene_manager()->addBillboardSceneNode(
585593
m_matrixnode, v2f(1, 1), v3f(0,0,0), -1);
586594
m_spritenode->grab();
@@ -601,6 +609,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
601609
txs, tys, 0, 0);
602610
}
603611
} else if (m_prop.visual == "upright_sprite") {
612+
getMatrixNode();
604613
scene::SMesh *mesh = new scene::SMesh();
605614
double dx = BS * m_prop.visual_size.X / 2;
606615
double dy = BS * m_prop.visual_size.Y / 2;
@@ -655,9 +664,6 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
655664
mesh->addMeshBuffer(buf);
656665
buf->drop();
657666
}
658-
m_matrixnode = RenderingEngine::get_scene_manager()->
659-
addDummyTransformationSceneNode();
660-
m_matrixnode->grab();
661667
m_meshnode = RenderingEngine::get_scene_manager()->
662668
addMeshSceneNode(mesh, m_matrixnode);
663669
m_meshnode->grab();
@@ -666,11 +672,8 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
666672
// This is needed for changing the texture in the future
667673
m_meshnode->setReadOnlyMaterials(true);
668674
} else if (m_prop.visual == "cube") {
669-
infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
675+
getMatrixNode();
670676
scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
671-
m_matrixnode = RenderingEngine::get_scene_manager()->
672-
addDummyTransformationSceneNode(nullptr);
673-
m_matrixnode->grab();
674677
m_meshnode = RenderingEngine::get_scene_manager()->
675678
addMeshSceneNode(mesh, m_matrixnode);
676679
m_meshnode->grab();
@@ -685,12 +688,9 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
685688
m_meshnode->setMaterialType(material_type);
686689
m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
687690
} else if (m_prop.visual == "mesh") {
688-
infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
691+
getMatrixNode();
689692
scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh, true);
690693
if (mesh) {
691-
m_matrixnode = RenderingEngine::get_scene_manager()->
692-
addDummyTransformationSceneNode(nullptr);
693-
m_matrixnode->grab();
694694
m_animated_meshnode = RenderingEngine::get_scene_manager()->
695695
addAnimatedMeshSceneNode(mesh, m_matrixnode);
696696
m_animated_meshnode->grab();
@@ -713,8 +713,8 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
713713
} else
714714
errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
715715
} else if (m_prop.visual == "wielditem" || m_prop.visual == "item") {
716+
getMatrixNode();
716717
ItemStack item;
717-
infostream << "GenericCAO::addToScene(): wielditem" << std::endl;
718718
if (m_prop.wield_item.empty()) {
719719
// Old format, only textures are specified.
720720
infostream << "textures: " << m_prop.textures.size() << std::endl;
@@ -728,12 +728,8 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
728728
infostream << "serialized form: " << m_prop.wield_item << std::endl;
729729
item.deSerialize(m_prop.wield_item, m_client->idef());
730730
}
731-
m_matrixnode = RenderingEngine::get_scene_manager()->
732-
addDummyTransformationSceneNode(nullptr);
733-
m_matrixnode->grab();
734731
m_wield_meshnode = new WieldMeshSceneNode(
735732
RenderingEngine::get_scene_manager(), -1);
736-
m_wield_meshnode->setParent(m_matrixnode);
737733
m_wield_meshnode->setItem(item, m_client,
738734
(m_prop.visual == "wielditem"));
739735

@@ -751,6 +747,9 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
751747

752748
scene::ISceneNode *node = getSceneNode();
753749

750+
if (node && m_matrixnode)
751+
node->setParent(m_matrixnode);
752+
754753
if (node && !m_prop.nametag.empty() && !m_is_local_player) {
755754
// Add nametag
756755
v3f pos;
@@ -884,7 +883,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
884883

885884
// Apply animations if input detected and not attached
886885
// or set idle animation
887-
if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached) {
886+
if ((new_anim.X + new_anim.Y) > 0 && !getParent()) {
888887
allow_update = true;
889888
m_animation_range = new_anim;
890889
m_animation_speed = new_speed;
@@ -946,12 +945,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
946945
m_velocity = v3f(0,0,0);
947946
m_acceleration = v3f(0,0,0);
948947
pos_translator.val_current = m_position;
949-
950-
if(m_is_local_player) // Update local player attachment position
951-
{
952-
LocalPlayer *player = m_env->getLocalPlayer();
953-
player->overridePosition = getParent()->getPosition();
954-
}
948+
pos_translator.val_target = m_position;
955949
} else {
956950
rot_translator.translate(dtime);
957951
v3f lastpos = pos_translator.val_current;
@@ -975,16 +969,14 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
975969

976970
bool is_end_position = moveresult.collides;
977971
pos_translator.update(m_position, is_end_position, dtime);
978-
pos_translator.translate(dtime);
979-
updateNodePos();
980972
} else {
981973
m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
982974
m_velocity += dtime * m_acceleration;
983975
pos_translator.update(m_position, pos_translator.aim_is_end,
984976
pos_translator.anim_time);
985-
pos_translator.translate(dtime);
986-
updateNodePos();
987977
}
978+
pos_translator.translate(dtime);
979+
updateNodePos();
988980

989981
float moved = lastpos.getDistanceFrom(pos_translator.val_current);
990982
m_step_distance_counter += moved;
@@ -1348,7 +1340,8 @@ void GenericCAO::updateAttachments()
13481340

13491341
if (!parent) { // Detach or don't attach
13501342
if (m_matrixnode) {
1351-
v3f old_pos = m_matrixnode->getAbsolutePosition();
1343+
v3f old_pos = getPosition();
1344+
13521345
m_matrixnode->setParent(m_smgr->getRootSceneNode());
13531346
getPosRotMatrix().setTranslation(old_pos);
13541347
m_matrixnode->updateAbsolutePosition();
@@ -1372,11 +1365,6 @@ void GenericCAO::updateAttachments()
13721365
m_matrixnode->updateAbsolutePosition();
13731366
}
13741367
}
1375-
if (m_is_local_player) {
1376-
LocalPlayer *player = m_env->getLocalPlayer();
1377-
player->isAttached = parent;
1378-
player->parent = parent;
1379-
}
13801368
}
13811369

13821370
void GenericCAO::processMessage(const std::string &data)

‎src/client/content_cao.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ class GenericCAO : public ClientActiveObject
165165

166166
const bool isImmortal();
167167

168-
scene::ISceneNode *getSceneNode();
168+
scene::ISceneNode *getSceneNode() const;
169169

170-
scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode();
170+
scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode() const;
171171

172172
// m_matrixnode controls the position and rotation of the child node
173173
// for all scene nodes, as a workaround for an Irrlicht problem with

‎src/client/localplayer.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
184184
v3f position = getPosition();
185185

186186
// Copy parent position if local player is attached
187-
if (isAttached) {
188-
setPosition(overridePosition);
187+
if (getParent()) {
188+
setPosition(m_cao->getPosition());
189189
added_velocity = v3f(0.0f); // ignored
190190
return;
191191
}
@@ -474,7 +474,7 @@ void LocalPlayer::applyControl(float dtime, Environment *env)
474474
setYaw(control.yaw);
475475

476476
// Nullify speed and don't run positioning code if the player is attached
477-
if (isAttached) {
477+
if (getParent()) {
478478
setSpeed(v3f(0.0f));
479479
return;
480480
}
@@ -706,6 +706,11 @@ v3f LocalPlayer::getEyeOffset() const
706706
return v3f(0.0f, BS * eye_height, 0.0f);
707707
}
708708

709+
ClientActiveObject *LocalPlayer::getParent() const
710+
{
711+
return m_cao ? m_cao->getParent() : nullptr;
712+
}
713+
709714
bool LocalPlayer::isDead() const
710715
{
711716
FATAL_ERROR_IF(!getCAO(), "LocalPlayer's CAO isn't initialized");
@@ -764,8 +769,8 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
764769
v3f position = getPosition();
765770

766771
// Copy parent position if local player is attached
767-
if (isAttached) {
768-
setPosition(overridePosition);
772+
if (getParent()) {
773+
setPosition(m_cao->getPosition());
769774
m_sneak_node_exists = false;
770775
added_velocity = v3f(0.0f);
771776
return;

‎src/client/localplayer.h

+2-5
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,9 @@ class LocalPlayer : public Player
4747
LocalPlayer(Client *client, const char *name);
4848
virtual ~LocalPlayer() = default;
4949

50-
ClientActiveObject *parent = nullptr;
51-
5250
// Initialize hp to 0, so that no hearts will be shown if server
5351
// doesn't support health points
5452
u16 hp = 0;
55-
bool isAttached = false;
5653
bool touching_ground = false;
5754
// This oscillates so that the player jumps a bit above the surface
5855
bool in_liquid = false;
@@ -72,8 +69,6 @@ class LocalPlayer : public Player
7269
// Temporary option for old move code
7370
bool physics_override_new_move = true;
7471

75-
v3f overridePosition;
76-
7772
void move(f32 dtime, Environment *env, f32 pos_max_d);
7873
void move(f32 dtime, Environment *env, f32 pos_max_d,
7974
std::vector<CollisionInfo> *collision_info);
@@ -112,6 +107,8 @@ class LocalPlayer : public Player
112107

113108
GenericCAO *getCAO() const { return m_cao; }
114109

110+
ClientActiveObject *getParent() const;
111+
115112
void setCAO(GenericCAO *toset)
116113
{
117114
assert(!m_cao); // Pre-condition

‎src/script/lua_api/l_localplayer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ int LuaLocalPlayer::l_is_attached(lua_State *L)
7878
{
7979
LocalPlayer *player = getobject(L, 1);
8080

81-
lua_pushboolean(L, player->isAttached);
81+
lua_pushboolean(L, player->getParent() != nullptr);
8282
return 1;
8383
}
8484

@@ -157,7 +157,7 @@ int LuaLocalPlayer::l_get_override_pos(lua_State *L)
157157
{
158158
LocalPlayer *player = getobject(L, 1);
159159

160-
push_v3f(L, player->overridePosition);
160+
push_v3f(L, player->getPosition());
161161
return 1;
162162
}
163163

0 commit comments

Comments
 (0)
Please sign in to comment.