Skip to content

Commit 0b6580e

Browse files
committed
Merge branch 'shinejotaro' into 'master'
Re-enable rotation for thrown projectiles See merge request OpenMW/openmw!5110
2 parents 680ae4d + 23ad774 commit 0b6580e

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

apps/openmw/mwworld/projectilemanager.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
#include "../mwmechanics/combat.hpp"
5050
#include "../mwmechanics/creaturestats.hpp"
5151
#include "../mwmechanics/spellcasting.hpp"
52-
#include "../mwmechanics/weapontype.hpp"
5352

5453
#include "../mwrender/animation.hpp"
5554
#include "../mwrender/renderingmanager.hpp"
@@ -152,6 +151,29 @@ namespace
152151

153152
return lightDiffuseColor;
154153
}
154+
155+
osg::Quat lookAt(const osg::Vec3f& pos)
156+
{
157+
// Rotate the forward vector towards the position (used for gravity-affected projectiles)
158+
// Can't use Quat::makeRotate as the shortest angle contains undesirable local roll
159+
const float dist = pos.length();
160+
if (dist < 1e-4f)
161+
return {};
162+
163+
const osg::Vec3f dir = pos / dist;
164+
osg::Vec3f right = dir ^ osg::Z_AXIS;
165+
if (right.normalize() < 1e-4f)
166+
right = osg::X_AXIS;
167+
168+
const osg::Vec3f up = right ^ dir;
169+
170+
osg::Matrixf mat(right.x(), right.y(), right.z(), 0.f, dir.x(), dir.y(), dir.z(), 0.f, up.x(), up.y(), up.z(),
171+
0.f, 0.f, 0.f, 0.f, 1.f);
172+
173+
osg::Quat orient;
174+
orient.set(mat);
175+
return orient;
176+
}
155177
}
156178

157179
namespace MWWorld
@@ -349,8 +371,6 @@ namespace MWWorld
349371
state.mIdArrow = projectile.getCellRef().getRefId();
350372
state.mCasterHandle = actor;
351373
state.mAttackStrength = attackStrength;
352-
int type = projectile.get<ESM::Weapon>()->mBase->mData.mType;
353-
state.mThrown = MWMechanics::getWeaponType(type)->mWeaponClass == ESM::WeaponType::Thrown;
354374

355375
MWWorld::ManualRef ref(*MWBase::Environment::get().getESMStore(), projectile.getCellRef().getRefId());
356376
MWWorld::Ptr ptr = ref.getPtr();
@@ -493,14 +513,7 @@ namespace MWWorld
493513

494514
projectile->setVelocity(projectileState.mVelocity);
495515

496-
// rotation does not work well for throwing projectiles - their roll angle will depend on shooting
497-
// direction.
498-
if (!projectileState.mThrown)
499-
{
500-
osg::Quat orient;
501-
orient.makeRotate(osg::Vec3f(0, 1, 0), projectileState.mVelocity);
502-
projectileState.mNode->setAttitude(orient);
503-
}
516+
projectileState.mNode->setAttitude(lookAt(projectileState.mVelocity));
504517

505518
update(projectileState, duration);
506519

@@ -710,8 +723,6 @@ namespace MWWorld
710723
MWWorld::ManualRef ref(*MWBase::Environment::get().getESMStore(), esm.mId);
711724
MWWorld::Ptr ptr = ref.getPtr();
712725
model = ptr.getClass().getCorrectedModel(ptr);
713-
int weaponType = ptr.get<ESM::Weapon>()->mBase->mData.mType;
714-
state.mThrown = MWMechanics::getWeaponType(weaponType)->mWeaponClass == ESM::WeaponType::Thrown;
715726

716727
state.mProjectileId
717728
= mPhysics->addProjectile(state.getCaster(), osg::Vec3f(esm.mPosition), model, false);

apps/openmw/mwworld/projectilemanager.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ namespace MWWorld
121121

122122
osg::Vec3f mVelocity;
123123
float mAttackStrength;
124-
bool mThrown;
125124
};
126125

127126
std::vector<MagicBoltState> mMagicBolts;

0 commit comments

Comments
 (0)