3
\$\begingroup\$

This question already has an answer here:

I'm making a competitive 2-player 2D racing game for a school project. I'm struggling with the movements of my car: I made it a square for testing purposes and it can move left, right, up or down, but it can't make smooth turns around corners.

How can I make my car (currently a square) make smooth movements? I've heard vectors might help, but I don't know how to use them.

I'm writing this in C# with Visual Studio.


My car control code looks like this right now:

private void Form1_KeyDown(object sender, KeyEventArgs key) {
    // player1 controls
    if (key.KeyCode == Keys.Up)     { Player1.Top -= 5; }
    if (key.KeyCode == Keys.Down)   { Player1.Top += 5; }

    if (key.KeyCode == Keys.Left)   { Player1.Left -= 5; }
    if (key.KeyCode == Keys.Right)  { Player1.Left += 5; }

    // player2 controls
    if (key.KeyCode == Keys.W) { Player2.Top -= 5; }
    if (key.KeyCode == Keys.S) { Player2.Top += 5; }

    if (key.KeyCode == Keys.A) { Player2.Left -= 5; }
    if (key.KeyCode == Keys.D) { Player2.Left += 5; }
}
\$\endgroup\$

marked as duplicate by congusbongus, Anko, Seth Battin, Josh Oct 27 '14 at 16:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    \$\begingroup\$ You need to rotate the car while it's moving and then move it in the direction it facing. The keys should affect velocity and not movements directly in the sense that keys control acceleration. \$\endgroup\$ – wolfdawn Oct 23 '14 at 10:41
  • \$\begingroup\$ We heared that, only the code of that is so hard to figure out :s \$\endgroup\$ – elipoo Oct 23 '14 at 12:22
  • 1
    \$\begingroup\$ @elipoo X = speed * cos( rotation ), Y = speed * sin( rotation ). It's that easy. \$\endgroup\$ – Ben Oct 23 '14 at 14:48
  • \$\begingroup\$ This question has some good information, specifically in this answer. \$\endgroup\$ – XNargaHuntress Oct 23 '14 at 16:15
  • \$\begingroup\$ @Ben still figuring out how.. \$\endgroup\$ – elipoo Oct 24 '14 at 8:00
5
\$\begingroup\$

Start by adding a variable for the angle that the car is moving at.

float angle = Math.Pi / 2;

Then add a variable for the current speed.

float speed = 0.0;

Now create three constants:

public static final float acceleration = 0.1; public static final float maxSpeed = 5.0; public static final float rotationRate = Math.Pi / 50;

For starters, get your controls somewhat right:

Do this every frame:

private void everyTick()
{
    player.top -= Math.sin(Player1.angle) * Player1.speed;
    player.left += Math.cos(Player1.angle) * Player1.speed;
}

Do this on key press:

private void Form1_KeyDown(object sender, KeyEventArgs key)
{
    //player1 controls
    if (key.KeyCode == Keys.Up)
    {
        Player1.speed += accelaration;
        Player1.speed = Math.min(Player1.speed,maxSpeed);
    }
    if (key.KeyCode == Keys.Down)
    {
        Player1.speed -= accelaration;
        Player1.speed = Math.max(Player1.speed, -maxSpeed);
    }
    if (key.KeyCode == Keys.Left)
    {
        Player1.angle += rotationRate;
    }
    if (key.KeyCode == Keys.Right)
    {
        Player1.angle -= rotationRate;
    }

}

This should get you started.

\$\endgroup\$
  • \$\begingroup\$ private void Player(string[] args ) { double angle = Math.Pi / 2; double speed = 0.0; } public void final(float[ ]args) { const double acceleration = 0.1; } public void final(float[ ]args) { const double maxSpeed = 5.0; } public void final(float[ ]args) { double rotationRate = Math.Pi / 50; } \$\endgroup\$ – elipoo Oct 24 '14 at 10:37
  • \$\begingroup\$ this is youre 2 first steps, dont know if i'm doing it right but it gives a lot of errors right now. \$\endgroup\$ – elipoo Oct 24 '14 at 10:40
1
\$\begingroup\$

First use Zehelvion answer to make the car turn correctly, then:

Use the timestamp, you measure how much time it took for your last iteration and use that to modulate the speed at which you are changing things.

void RunGame(float deltatime)
{
    if (key.KeyCode == Keys.Up)
    {
        Player1.speed += accelaration * deltatime
        Player1.speed = Math.min(Player1.speed,maxSpeed)
...

This will make it much smoother even if the framerate goes up or down (why accelerate twice as fast if the framerate goes from 30 to 60 in some area?).

\$\endgroup\$
  • \$\begingroup\$ i'm still trying to use the code of Zehelvion only the variable and constant part isnt really working so have to fix that. After that i will try this :). \$\endgroup\$ – elipoo Oct 24 '14 at 15:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.