I am working on my first Urho3D game using the Character Demo as the starting point.
I have the basic scene setup and some logic to shoot some table tennis balls to the table tennis table surface. The problem is, some balls just get stuck on the tables, some balls can bounce over to the other side of the table and some just pass through the table surface and drop tot he floor.
It seems like the collision mesh of the table is not setup correctly but I could not figure where is the problem. Any advise here is appreciated.
Below are the source code for the setup:
// set this one up manually to explore the parameters of these functions
Node* tableNode = scene_->CreateChild("Table");
tableNode->SetPosition(Vector3(0.0f, 0.8f, 0.0f));
tableNode->SetRotation(Quaternion(0.0f, 90.0f, 90.0f));
tableNode->SetScale(1.0f);
auto* tableObject = tableNode->CreateComponent<StaticModel>();
tableObject->SetModel(cache->GetResource<Model>("Models/Table3D.mdl"));
tableObject->SetMaterial(cache->GetResource<Material>("Materials/Table.xml"));
auto* tableBody = tableNode->CreateComponent<RigidBody>();
tableBody->SetMass(10.0f);
tableBody->SetRestitution(0.6f);
tableBody->SetKinematic(false);
auto* tableShape = tableNode->CreateComponent<CollisionShape>();
tableShape->SetTriangleMesh(tableObject->GetModel(), 0);
The function which instantiates the balls and shoot at the table based on the camera direction:
void TableTennisDemo::SpawnObject()
{
auto* cache = GetSubsystem<ResourceCache>();
Node* boxNode = scene_->CreateChild("Ball");
boxNode->SetPosition(cameraNode_->GetPosition());
boxNode->SetRotation(cameraNode_->GetRotation());
boxNode->SetScale(1.00f);
auto* boxObject = boxNode->CreateComponent<StaticModel>();
boxObject->SetModel(cache->GetResource<Model>("Models/ball.mdl"));
boxObject->SetMaterial(cache->GetResource<Material>("Materials/Ball.xml"));
boxObject->SetCastShadows(true);
auto* body = boxNode->CreateComponent<RigidBody>();
body->SetMass(0.027f);
body->SetRollingFriction(0.05f);
body->SetRestitution(0.8f);
body->SetLinearDamping(0.2f);
auto* shape = boxNode->CreateComponent<CollisionShape>();
shape->SetSphere(0.04f);
const float OBJECT_VELOCITY = 6.0f;
// Set initial velocity for the RigidBody based on camera forward vector. Add also a slight up component
// to overcome gravity better
body->SetLinearVelocity(cameraNode_->GetRotation() * Vector3(0.0f, 0.25f, 1.0f) * OBJECT_VELOCITY);
}
A snapshot of the game scene: