• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
prushik

Tractor Trailers with Rigid Bodies in 2D (hinge constraints)

1 post in this topic

Basically what I want to do is implement some basic tractor trailer physics using rigid bodies. Right now I have rigid body physics that look decent (good enough for me right now) for a single segment vehicle (car, bus, etc...), however, tractor trailers are important in my game. I've been thinking about this problem for some time and searching the internet, and have come up with so much information, but nothing that really solves the problem. It seems it should be easy, this is just a simple 2d game.
Should I create a point on both rigid bodies that acts similar to a wheel and applies force to the rigid body equivalent to what is needed to keep the bodies connected? or is there some other way that this should be done?
Both segments will need to exert forces on each other.

I found a guy who wrote an algorithm here: [url="http://gamedev.stackexchange.com/questions/18151/how-can-i-implement-revolute-hinge-joints-in-a-2d-physics-system"]http://gamedev.stack...-physics-system[/url]

It seems to be what I was looking for, but from the page it seems there may be some problems with it. And the author seems unsure of himself. So, is that the right approach to take?

(I'm using C and SDL2, if you want to know)

EDIT: Also note that my simulation should be simpler than the article I linked to, since I only have one hinge point where it looks like the article actually has 2. Edited by prushik
0

Share this post


Link to post
Share on other sites
UPDATE:
Ok, so I have implemented something based on what I was thinking. Both the tractor and the trailer are rigid bodies. I calculate forces generated by the wheels in both objects, then integrate the forces. Then I calculate the speed difference between the attachment points on both objects and multiply by mass to get the force vector. Then I add that force vector to one object and an opposite response force vector to the other. Then integrate the forces again.
Does this make sense?

The result is something that looks almost right. They stay (almost) together, and pivot around the hinge point. However, sometimes weird problems occur. The trailer drifts off the hinge point (very slowly) after substantial use, and sometimes the trailer pulls the tractor into unexpected high speed turns, not sure why.


I'll post the relevant code:

[source lang="cpp"]
struct image
{
const char *fname;
void *texture; //SDL_Texture
int w,h;
double angle;
};

struct rigidBody
{
VEC2 coord;
VEC2 speed;
VEC2 force;
double mass;

double angle;
double angleV;
double torque;
double inertia;
};

struct wheel
{
VEC2 coord, forward, side;
double angle, speed, radius;
double inertia, torque;

double steer,throttle,brake;
};

struct vehicle
{
struct rigidBody chassis;
struct wheel wheel[4];

struct vehicle *trailer;
VEC2 hinge,pin;

struct image img;
};

void updateHinge(struct vehicle *obj)
{
//Don't apply forces from trailer if there is no trailer
if (obj->trailer==NULL)
return;

VEC2 hoffset,poffset,hspeed,pspeed,wspeed,force;

relativeToWorld(obj,&obj->hinge,&hoffset);
relativeToWorld(obj->trailer,&obj->trailer->pin,&poffset);
pointVel(obj,&hoffset,&hspeed);
pointVel(obj->trailer,&poffset,&pspeed);
subtractVEC(&hspeed,&pspeed,&wspeed);

reverseVEC(&wspeed,&wspeed);
multiplyVEC(&wspeed,obj->chassis.mass,&force);
addForce(&obj->chassis,&force,&hoffset);

reverseVEC(&wspeed,&wspeed);
multiplyVEC(&wspeed,obj->trailer->chassis.mass,&force);
addForce(&obj->trailer->chassis,&force,&poffset);

return;
}

int main()
{
struct vehicle user,trail;
initVehicle(&user,20.0);
user.chassis.coord.x=100.0;
user.chassis.coord.y=200.0;
user.chassis.angle=0.0;
initWheel(&user,0,30.0,15.0,2.0);//FL
initWheel(&user,1,30.0,-15.0,2.0);//FR
initWheel(&user,2,-30.0,15.0,2.0);//RL
initWheel(&user,3,-30.0,-15.0,2.0);//RR
user.wheel[0].throttle=0;
user.wheel[1].throttle=0;

//Initialize test trailer
initVehicle(&trail,40.0);
trail.chassis.coord.x=100.0-32;
trail.chassis.coord.y=200.0;
trail.chassis.angle=0.0;
initWheel(&trail,0,-25.0,15.0,5.0);//FL
initWheel(&trail,1,-25.0,-15.0,5.0);//FR
initWheel(&trail,2,-30.0,15.0,5.0);//RL
initWheel(&trail,3,-30.0,-15.0,5.0);//RR
trail.wheel[0].throttle=0;
trail.wheel[1].throttle=0;
trail.wheel[2].throttle=0;
trail.wheel[3].throttle=0;

//Attach tractor and trailer
user.trailer=&trail;
user.hinge.x=-32+14;
user.hinge.y=0;
trail.pin.x=14;
trail.pin.y=0;

//Set more stuff up...

while (1)
{
//......Other stuff
updateWheels(&user);
updateWheels(&trail);
updateRigidBody(&user.chassis);
updateRigidBody(&trail.chassis);

updateHinge(&user);
updateHinge(&trail);

updateRigidBody(&user.chassis);
updateRigidBody(&trail.chassis);
//....Draw the stuff here
}
}
[/source]

Now, obviously, there is a lot of code left out, but this should be the relevant parts. If there is something else that you need to see then let me know and I will provide. I intend to release my project open source, so I won't be hiding anything.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0