図0306a
コントローラのAボタンを押すと、左側のボールが動き、右のボールに当たります。すると、左のボールは止まって、右側だけが動きます。
void Collision::AfterCollision(const shared_ptr<Collision>& DestColl,
const bsm::Vec3& SrcVelocity, const bsm::Vec3& DestVelocity,
const bsm::Vec3& HitNormal,float AfterHitTime) {
//中略
auto PtrRigid = GetGameObject()->GetComponent<Rigidbody>(false);
if (PtrRigid) {
switch (GetIsHitAction()) {
case IsHitAction::Slide:
{
auto DestVelocity = PtrDestTransform->GetVelocity();
Slide = pImpl->Slide(SrcVelocity, HitNormal);
PtrRigid->SetVelocity(Slide);
//重力は0にする
PtrRigid->SetGravityVelocityZero();
}
break;
case IsHitAction::Auto:
if (horizontal) {
//乗っているときはスライドさせる
Slide = pImpl->Slide(SrcVelocity, HitNormal);
PtrRigid->SetVelocity(Slide);
//何かに乗ったときは重力は0にする
PtrRigid->SetGravityVelocityZero();
}
else {
//乗ってないときは反発
auto DestRigid = DestColl->GetGameObject()->GetComponent<Rigidbody>(false);
float ResultPower = -(1.0f + PtrRigid->GetReflection());
if (DestRigid) {
bsm::Vec3 RelativeVelo = SrcVelocity - DestVelocity;
ResultPower = (-(1.0f + PtrRigid->GetReflection()) * bsm::dot(RelativeVelo, HitNormal)) /
(bsm::dot(HitNormal, HitNormal) * (1 / PtrRigid->GetMass() + 1 / DestRigid->GetMass()));
}
else {
bsm::Vec3 RelativeVelo = SrcVelocity;
ResultPower = (-(1.0f + PtrRigid->GetReflection()) * bsm::dot(RelativeVelo, HitNormal)) /
(bsm::dot(HitNormal, HitNormal) * (1 / PtrRigid->GetMass()));
}
auto Velo = PtrRigid->GetVelocity();
Velo += (HitNormal * ResultPower) / PtrRigid->GetMass();
PtrRigid->SetVelocity(Velo);
}
break;
case IsHitAction::Stop:
{
//速度は0にする
PtrRigid->SetVelocityZero();
//重力は0にする
PtrRigid->SetGravityVelocityZero();
}
break;
}
}
}
//ボールの作成
void GameStage::CreateBalls() {
//ボールの作成
auto Ball1 = AddGameObject<Ball>(Vec3(-2.5f, 0.125f, 5.1f), true);
//シェア配列にプレイヤーを追加
SetSharedGameObject(L"Ball1", Ball1);
auto Ball2 = AddGameObject<Ball>(Vec3(2.5f, 0.125f, 5.0f), false);
SetSharedGameObject(L"Ball2", Ball2);
}
//初期化
void Ball::OnCreate() {
//中略
//Rigidbodyをつける
auto PtrRedid = AddComponent<Rigidbody>();
PtrRedid->SetReflection(0.5f);
//衝突判定をつける
auto PtrCol = AddComponent<CollisionSphere>();
//中略
}