Jump to content

Vehicle like collisions, any easy way?


BGerman
 Share

Recommended Posts

I've gotten to a point where i need vehicle-like entities, such as an elevator or a boat. I've had no problem implementing an elevator, but i do have a problem with boat and car-like entities.

 

The problem is, when an entity moves on either X or Z axis (sides), any object on top, or any character controller, does not move along with it, it stays in the same position it was before.

 

I know it can be done with a collision callback function and i do know how it is done, but, is there any easy, fast way to achieve this effect?

 

Edit:

 

Ok, so i went ahead and did it the callback way, added a callback function:

int _stdcall moverCollision( TEntity entity0, TEntity entity1, byte* position, byte* normal, byte* force, flt speed )
{
AddBodyForce(entity1,GetBodyVelocity(entity0,1));
return 0;
}

 

and set the callback function:

SetEntityCallback(elevatorBody,(BP)moverCollision,ENTITYCALLBACK_COLLISION);

 

And it works GREAT with normal bodies, but not with character controllers... any suggestion? I already tried with UpdateController to no avail.

 

Will i have to create my own character controller?

Link to comment
Share on other sites

This will work for some of those situations:

http://www.leadwerks...rence/vehicles/

 

Yes, those commands do help in a normal vehicle situation, but what i'm trying to do, is for a person who does not "Get in" the vehicle, to also be able to get a ride, like standing on top of the vehicle's roof, and get carried along. I have replicated the effect for normal objects, but not with character controllers.

Link to comment
Share on other sites

I concur with Josh. There are many ways to skin a cat and I achieved this in the following way.

 

If you're wanting to jump from vehicle to vehicle, you'll want to code around that by adding 'hooks' to your different vehicle models that you parent your player to (hooking them on like coats of a coat rack). When you exit the vehicle you can un-parent and position your player at an entry/exit hook (and if your vehicle is on it's side, upside down or against a wall you'll need to compensate for that by checking for the first valid exit hook position).

 

A hook can be some dummy entity, or pivot parented to a vehicle, a good idea is to use an entity assigned a common name e.g. "player_hook_1". That way when you run the action to 'mount' a vehicle you run a function to look for the nearest player hook entity and parent the player to it.

 

In practice I hide the player TController in these circumstances because it's not needed (and I had collision issues between the TController and the different vehicle entities).

6600 2.4G / GTX 460 280.26 / 4GB Windows 7

Author: GROME Terrain Modeling for Unity, UDK, Ogre3D from PackT

Tricubic Studios Ltd. ~ Combat Helo

Link to comment
Share on other sites

Guys guys, you are explaining it right and i know what you mean, something like a seat in a vehicle a static object linked to other, that is not what i mean or want, Here is a video that ilustrates what i am trying to acomplish:

 

 

One player gets IN the vehicle to pilot it, (No character controller as you all are saying, static, inside the vehicle) BUT, the other player gets ON the vehicle (On top, not on a seat, and can move freely on top of it dynamically and can simply walk or jump off, nothing scripted, but the vehicle still moves him). THAT is what i want to achieve.

 

As an experiment, open up the editor, spawn a viperscout vehicle with physics paused, press the "switch to game" button while on top of it, and if the vehicle starts moving, you will stay in the same spot as the vehicle slides below you. this is the default behavior and is not what i want.

 

Here is my progress so far:

http://www.youtube.com/watch?v=5mmUf2fwsEs

 

As you can see, the character controller just slides, but the box does move around with the platform. And this does not happen with default settings! go ahead and try it and see what i am talking about.

Link to comment
Share on other sites

Apply the vehicles velocity to your model then, so its always moving with the vehicle, then add any local movements as additions to this vector.

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

Yes, that is the solution, but i was getting some weird bugs. This is the way I am doing it right now and it works... Kinda...

 

I added the callback function:

int _stdcall moverCollision( TEntity entity0, TEntity entity1, byte* position, byte* normal, byte* force, flt speed )
{
flag = GetEntityType(entity1);
PositionEntity(entity1,
 (EntityPosition(entity1,1)-EntityPosition(entity0,1))+
 EntityPosition(entity0,1),1);

   return 0;
}

 

And it works Great with normal bodies. But it does not work with character controllers unless you rotate it on X or Z axis by at least one degree....

Link to comment
Share on other sites

One player gets IN the vehicle to pilot it, (No character controller as you all are saying, static, inside the vehicle) BUT, the other player gets ON the vehicle (On top, not on a seat, and can move freely on top of it dynamically and can simply walk or jump off, nothing scripted, but the vehicle still moves him). THAT is what i want to achieve.
The charactet controller does move with any object it is standing on, but if the movement is too sudden it will slide off. This is realistic...imagine standing on a car's roof when it suddenly stops. You would fly off it.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Fellow developers, I have made this LEO example to illustrate what is wrong:

// ====================================================================
// This file was generated by LEBuilder
// http://leadwerks.com/werkspace
// ====================================================================
#include "leo.h"
#include <string>
#include <iostream>
using namespace std;
using namespace LEO;
const int ScreenWidth = 800;
const int ScreenHeight = 600;
const char* MediaDir = "C:/Leadwerks Engine SDK";
const char* AppTitle = "Character controller bug?";
const int Group_Normal = 1;
const int Group_Mover = 2;
const int Group_Player = 3;
void ErrOut( const string& message ) { cerr << message << endl; }
// --------------------------------------------
int main( int argc, char* argv[] )
{
// Set graphics mode
Engine engine(AppTitle,ScreenWidth,ScreenHeight);
if( !engine.IsValid() )
{
ErrOut( "Failed to set graphics mode.");
return 1;
}
engine.AddAbstractPath( MediaDir );
// Create framework object and set it to a global object so other scripts can access it
Framework fw;
if( NULL == fw )
{
ErrOut( "Failed to initialize engine." );
return 1;
}
// Set Lua framework object
engine.SetObject( "fw", fw );

// Set Lua framework variable
Lua lua;
lua.PushObject( fw );
lua.SetGlobal( "fw" );
lua.Pop( 1 );
// Get framework main camera
fw.main.GetCamera().SetPosition( Vec3(0,20,-20));
fw.main.GetCamera().SetRotation( Vec3(45,0,0));
// Create cube
TBody cubeBody=CreateBodyBox(2,2,2);
PositionEntity(cubeBody,Vec3(-3,5,-3));
// Create ground
TBody groundBody=CreateBodyBox(20,2,20);
PositionEntity(groundBody,Vec3(0,-3,0));
//Create elevator
TBody elevatorBody=CreateBodyBox(8,2,8);
PositionEntity(elevatorBody,Vec3(0,0,0));
SetBodyGravityMode(elevatorBody,false);
//Create player
TController playerController=CreateController(7.5,1.5,0.5,45,5);
PositionEntity(playerController,Vec3(3,5,3),1);
//Assign types
EntityType(cubeBody,Group_Normal);
EntityType(groundBody,Group_Normal);
EntityType(elevatorBody,Group_Mover);
EntityType(playerController,Group_Player);
//Assign mass
SetBodyMass(groundBody,0);
SetBodyMass(cubeBody,1);
SetBodyMass(elevatorBody,1000);
SetBodyMass(playerController,5);
//Set collisions
SetWorldGravity(Vec3(0,-15,0));
Collisions::Set(Group_Normal,Group_Normal,SLIDINGCOLLISION);
Collisions::Set(Group_Normal,Group_Mover,SLIDINGCOLLISION);
Collisions::Set(Group_Normal,Group_Player,SLIDINGCOLLISION);
Collisions::Set(Group_Mover,Group_Mover,SLIDINGCOLLISION);
Collisions::Set(Group_Mover,Group_Player,SLIDINGCOLLISION);
Collisions::Set(Group_Player,Group_Player,SLIDINGCOLLISION);
DebugPhysics();
// Spin cube until user hits Escape
while( !Keyboard::I****() && !engine.IsTerminated() )
{
if( !engine.IsSuspended() )
{
UpdateWorld();
//Update character controller
float move=(KeyDown(KEY_W)-KeyDown(KEY_S))*10;
float strafe=(KeyDown(KEY_D)-KeyDown(KEY_A))*10;
float jump=0.0;
if (KeyHit(KEY_LSHIFT)) {
if (!ControllerAirborne(playerController)) {
 jump=15.0;
}
}
UpdateController(playerController,0.0,move,strafe,jump,20);
//Add mover velocity
if(KeyDown(KEY_LCONTROL)){
AddBodyForce(elevatorBody,Vec3(0,4000,0),1);
}
if(KeyDown(KEY_SPACE)){
AddBodyForce(elevatorBody,Vec3(0,-4000,0),1);
}
if(KeyDown(KEY_RIGHT)){
AddBodyForce(elevatorBody,Vec3(4000,0,0),1);
}
if(KeyDown(KEY_LEFT)){
AddBodyForce(elevatorBody,Vec3(-4000,0,0),1);
}
if(KeyDown(KEY_UP)){
AddBodyForce(elevatorBody,Vec3(0,0,4000),1);
}
if(KeyDown(KEY_DOWN)){
AddBodyForce(elevatorBody,Vec3(0,0,-4000),1);
}
//This fixes the controller
if(KeyDown(KEY_X)){
RotateEntity(playerController,Vec3(0.5,0,0),1);
}
SetBodyTorque(elevatorBody,Vec3(0,0,0),1);
RotateEntity(elevatorBody,Vec3(0,0,0),1);
fw.Update();
fw.Render();
DrawText(20,20,"Use Arrow keys to move platform around, use Ctrl and Space to move up and down");
DrawText(20,40,"Use W,A,S,D to move character controller around. Use LShift to jump");
DrawText(20,60,"Character controller does not move along, press X to apply solution");
engine.Flip(1);
}
}

return engine.Free();
}

 

Run it and see it yourselves. The character controller does not move accordingly unless it is rotated slightly. dont know if there is another way but that works, for now...

 

Video of this program in action, I press X on second 60, and you can see it now behaves the way it should. All pressing x does is:

//This fixes the controller
if(KeyDown(KEY_X)){
RotateEntity(playerController,Vec3(0.5,0,0),1);
}

 

 

Can this be considered a real bug?

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...