I am sure this is answered somewhere in the forums but I could not find it. Sorry if (when) this is a repeat.
I have been re-jiggering my entire approach to the architecture of my game. I decided to rely more on the built-in/Bullet engine.
I have:
this->terrainNode_ = this->scene_->CreateChild( Terrain );
this->terrainNode_->SetPosition( Urho3D::Vector3::ZERO );
auto* terrain = this->terrainNode_->CreateComponent< Urho3D::Terrain >();
terrain->SetPatchSize( 64 );
terrain->SetSpacing( Urho3D::Vector3( 5.0f, 1.0f, 5.0f ) );
terrain->SetSmoothing( true );
terrain->SetHeightMap( cache->GetResource< Urho3D::Image >( Textures/HeightMap.png ) );
terrain->SetMaterial( cache->GetResource< Urho3D::Material >( Materials/Terrain.xml ) );
terrain->SetOccluder( true );
auto* rigidBody = this->terrainNode_->CreateComponent< Urho3D::RigidBody >();
auto* collisionShape = this->terrainNode_->CreateComponent< Urho3D::CollisionShape >();
collisionShape->SetTerrain();
for my terrain.
My height map is as so:
I have for my player creation:
for ( short int i = 0 ; i < PLAYER_COUNT ; i++ ) {
this->players_[ i ] = this->scene_->CreateChild( "Player" );
Urho3D::Vector3 position( effolkronium::random_static::get( -1000, 1000 ), 0.0f, effolkronium::random_static::get( -1000, 1000 ) );
position.y_ = this->terrainNode_->GetComponent< Urho3D::Terrain >()->GetHeight( position ) + 50.0f;
this->players_[ i ]->SetPosition( position );
this->players_[ i ]->SetScale( Urho3D::Vector3( 100.0f, 50.0f, 100.0f ) );
auto* object = this->players_[ i ]->CreateComponent< Urho3D::StaticModel >();
object->SetModel( cache->GetResource< Urho3D::Model >( "Models/Box.mdl" ) );
object->SetMaterial( cache->GetResource< Urho3D::Material >( "Materials/Stone.xml" ) );
object->SetCastShadows( true );
auto* rigidBody = this->players_[ i ]->CreateComponent< Urho3D::RigidBody >();
rigidBody->SetMass( 5.0f );
rigidBody->SetFriction( 1.0f );
rigidBody->SetCollisionEventMode( Urho3D::COLLISION_ALWAYS );
auto* collisionShape = this->players_[ i ]->CreateComponent< Urho3D::CollisionShape >();
collisionShape->SetBox( Urho3D::Vector3::ONE );
this->players_[ i ]->CreateComponent< Player >();
...
}
I have for my player movement:
for ( short int i = 0 ; i < PLAYER_COUNT ; i++ ) {
auto position = this->players_[ i ]->GetPosition();
position.x_ += this->players_[ i ]->GetComponent< Player >()->GetX();
position.z_ += this->players_[ i ]->GetComponent< Player >()->GetZ();
this->players_[ i ]->SetTransform( position, Urho3D::Quaternion( Urho3D::Vector3( 0.0f, 1.0f, 0.0f ), this->terrainNode_->GetComponent< Urho3D::Terrain >()->GetNormal( position ) ) );
}
for my players.
At the 90° wall, instead of stopping/not being allowed to pass, the player just clips through the wall (after some jittering). Even at the 45° angle (up the hill) the player eventually just falls through the floor. I have tried using ApplyForce()
instead but it does not seem to do anything.
I am pretty sure I am supposed to be using ApplyForce()
instead of manipulating the position myself but I can not make it work. I have been studying 19_VehicleDemo but what works there does not seem to work for me.
If I add position.y_ = this->terrainNode_->GetComponent< Urho3D::Terrain >()->GetHeight( position ) + 25.0f
to the player movement it solves my clipping problem but I lose physics (obviously). Plus the player can now climb vertical walls (obviously, again).
How am I supposed to be moving the player around or is Bullet just not good with this kind of thing?