2D racing game help, thx!

Topics: Developer Forum
Aug 22, 2011 at 11:25 AM
Edited Aug 22, 2011 at 12:23 PM

Hi all, i am both XNA and Farseer beginner, trying to make top-down 2D perspective racing game ( like MicroMachines, SkidRaces etc ),

so i've made color key for collision detection, everything works fine, but game lacks solid, real life physics..

Q1; since it's top down perspective gravity is out of the picture for this type of game? I guess physics is transported to friction?

Q2; i have my racing tracks created in color gray (RGB = 128, what is the best way to surround my gray racing track image with some kind of Farseer body(bodies?) so i could benefit from the Farseer engine. Of course, my cars need to be inside that body (bodies?). Is this kind of thing possible to do?

EDIT: is "Texture to polygon" exactly what i need here??

 

Can you please explain where and how to start,

thank you in advance for your help!

JuZZiO

p.s. Sorry for posting like this, but i really didn't find any topic that would be helpfull to me, thx

Aug 22, 2011 at 8:28 PM

Please, any help would be appreciated, thank you.

Aug 23, 2011 at 10:29 AM

yes texture to polygon is what you need (anyway it would be better if you store your tracks / cars as a xml or something like that)

here is a good tutorial: http://www.emanueleferonato.com/2009/04/06/two-ways-to-make-box2d-cars/

Aug 23, 2011 at 11:48 PM

Wow, thx man, that's exactly what i had in mind, but until now i worked only with C#,

i hope i will be able to port it, if not, i hope u'll give me some kind of a clue, thanks for the help.

Sep 17, 2011 at 11:11 PM
Edited Sep 18, 2011 at 12:30 AM

I've been really busy past few weeks, "real life" is a b***h. :/

So i just got the time to get around this.. this Flash..  is just plain ugly..

//This function applies a "friction" in a direction orthogonal to the body's axis.
function killOrthogonalVelocity(targetBody:b2Body){
var localPoint = new b2Vec2(0,0);
var velocity:b2Vec2 = targetBody.GetLinearVelocityFromLocalPoint(localPoint);
 
var sidewaysAxis = targetBody.GetXForm().R.col2.Copy();
sidewaysAxis.Multiply(b2Math.b2Dot(velocity,sidewaysAxis))
 
targetBody.SetLinearVelocity(sidewaysAxis);//targetBody.GetWorldPoint(localPoint));
}

Can u give me the C# code for this method,i tried like this, but it's a no go..

        //This function applies a "friction" in a direction orthogonal to the body's axis.
        private void KillOrthogonalVelocity(Body targetBody)
        {
            Vector2 localPoint = new Vector2(0,0);
            Vector2 velocity = targetBody.GetLinearVelocityFromLocalPoint(localPoint);

            Transform tmp;
            targetBody.GetTransform(out tmp);
            Vector2 sidewaysAxis = tmp.R.Col2;
            sidewaysAxis = Vector2.Multiply(velocity, sidewaysAxis);

            targetBody.LinearVelocity = sidewaysAxis;//targetBody.GetWorldPoint(localPoint));
        }

Can you tell me what does transform does, it gets the body's position and orientation in the world?

Then why not use position and angle, what is R (Mat22) and Col2, can u plase explain, i tried to google this, still nothing. :(

Thank you all in advance!

Sep 18, 2011 at 12:36 AM
Edited Sep 18, 2011 at 1:50 AM

Apparently R is rotation matrix and colX are its columns, coincidentally forward and sideways directions (of the wheel).

non-uniform aka "tensor" damping means different damping along these directions. in case of wheel, you would want little damping in forward direction and larger damping in sideways direction.

-----------------------------------------------------------------------

So.. i don't get it.. obviously i'm new with this stuff, but ih wanted to simulate friction when car is turning,

could he just usesomething like this?

targetBody.LinearDamping =1;

targetBody.AngularDamping = 10;

 

Give me the meaning of that method, line by line, i'll post my code tomorrow, it's 3.am, i'm gonna faint, but meanwhile please explain it..

because either using that method, or by increasing angular damping, car moves like a giant squid :) , thank you.

Sep 18, 2011 at 10:49 AM

sidewaysAxis = Vector2.Multiply(velocity, sidewaysAxis);

->

sidewaysAxis.Multiply(b2Math.b2Dot(velocity,sidewaysAxis))

where is the Dot? i think it should be something like


sidewaysAxis.Multiply(Vector2.Dot(velocity, sidewaysAxis));

also you allready get friction at turning i think. because you kill the normal velocity, means the velocity of the wheel gets smaller

if you want more friction you have to do this with

targetBody.LinearDamping =1;

targetBody.AngularDamping = 10;

Sep 18, 2011 at 12:04 PM

Thank you for your reply, it pointed me to the right direction..

Just one detail, ur line produced compiler error, because instanced Vector2 doesn't have

Multiply method, it's static method of the Vector2 class, unlike Box2D version, and method ctor is slightly different.

So i've tried

 Vector2 v2tmp;
 Vector2.Multiply(ref sidewaysAxis, Vector2.Dot(velocity, sidewaysAxis), out v2tmp);

The motion is much more believable now.

 

Hellgate, i've sent u a message, can u please reply to me via e-mail, thanx!

Sep 22, 2011 at 9:45 PM

I've been trying to switch power to the rear tires and get a drift motion going on, but for the love of God, i can't make it work for hours (and than some) now. :( .....

Can you Hellgate please (or anyone else who can contribute), break down this method and explain to me, what it does, line by line, i'm obviously missing a point. :(

Thank you all in advance!

       //This function applies a "friction" in a direction orthogonal to the body's axis.
        private void KillOrthogonalVelocity(Body targetBody)
        {
            Vector2 localPoint = new Vector2(0,0);
            Vector2 velocity = targetBody.GetLinearVelocityFromLocalPoint(localPoint);

            Transform tmp;
            targetBody.GetTransform(out tmp);
            Vector2 sidewaysAxis = tmp.R.Col2;

            Vector2 v2tmp;
            Vector2.Multiply(ref sidewaysAxis, Vector2.Dot(velocity, sidewaysAxis), out v2tmp);

            targetBody.LinearVelocity = v2tmp;
        }

Any tips on how to make car slide / drift in code above?

I've tried of lot of stuff and none of them works..

On Box2D forum i've found a comment that if you need to get it drift, you need to add a Col1 (from transformation), but where and how? :(

Sep 23, 2011 at 11:14 AM
Edited Sep 23, 2011 at 11:15 AM

you neet to multiply it with 0.5f or something instead of kill the speed. also a little performance thing:

Vector2 velocity = targetBody.GetLinearVelocityFromLocalPoint(Vector2.Zero);

and Vector2.Multiply to => v2tmp = sidewaysAxis * Vector2.Dot(velocity, sidewaysAxis)

the best way to add the drift effect is geting the sideway axis (may tmp.R.Col1 ?) (your sidewaysAxis is the front axis i think)

and do the same as before but add 0.5f or something like that to the Vector2.Dot

Vector2.Dot(vel,axis) *0.5f and add it to the targetBody.LinearVelocity

 

btw i didn't get your message

Sep 24, 2011 at 12:14 AM

You mean alteration like this;

Vector2.Multiply(ref sidewaysAxis, Vector2.Dot(velocity, sidewaysAxis) * 0.5f, out v2tmp);

Unfortunately it doesn't work, i end up with car that is moving half of a previous speed..

Please, any other suggestions?

 

p.s. i'm confused, I begin to suspect Col1 is orthogonal (sideways), and Col2 is front axis from these examples, what is right definition in the end?

Sep 24, 2011 at 9:00 AM
Edited Sep 24, 2011 at 7:04 PM

no i mean add this lines not edit the old ones

private void KillOrthogonalVelocity(Body targetBody)
{
Vector2 velocity = targetBody.GetLinearVelocityFromLocalPoint(Vector2.Zero);

Transform tmp;
targetBody.GetTransform(out tmp);
Vector2 frontAxis = tmp.R.Col2;

Vector2 sidewaysAxis = tmp.R.Col1;

Vector2 v2tmp = frontAxis * Vector2.Dot(velocity, frontAxis);

Vector2 v2tmp2 = sidewaysAxis * Vector2.Dot(velocity, sidewaysAxis)*0.5f; // slow down the sideways speed instaed of killing it. set 0.5f to whatever you need

targetBody.LinearVelocity = v2tmp + v2tmp2;
}

i never really worked with this but i can see that he gets the velocity facting the wheel or car and set the velocity to this. means he kills the sideways velocity

Sep 24, 2011 at 2:34 PM

Thank you man a lot, this really helped!

I feel awesome and angry at the same time, i can't believe that answer was sitting in front of my nose the whole time,

i'm new at this, so his naming variable "sidewaysAxis" from Col2, while actually it is front axis, led me completely in wrong direction.. :(

That's why i have you, thx! :)