A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Car physics

  1. #1
    Senior Member
    Join Date
    Aug 2003
    Posts
    438

    Car physics

    I used an open-source engine, and refined it to create this:
    http://www.geocities.com/nmgwebsite/car.html

    http://www.geocities.com/nmgwebsite/car.fla - source

    It works great, but the only problem is, I have no idea how it works. I tried sticking the right triangle in order to find a relation, but I still don't get it.

    Does anybody mind explaining to me how this works?
    I am very good a algebra and math in general, but I have never learned geometry apart from simple shapes .

    Also.. What does Sin and Cos have anything to do with this?
    Ug.. I'm so confused..

    thanks!
    Last edited by Scorpion990; 04-24-2004 at 05:44 PM.

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    It's not so much geometry as trigonometry that would benefit you here... Trigonometry is extremely useful for programming video games (and/or guided missiles), as it teaches you to move in particular directions, to target objects and to measure distance.

    Geometry (Euclidean geometry) is more concerned with logic & proof.

    Let's look at the code:

    code:


    onClipEvent (enterFrame) {
    //DECLARATIONS
    degrees = this._rotation;
    radians = (degrees*(Math.PI/180));
    //ROTATION
    if (Key.isDown(Key.LEFT)) {
    _rotation -= 4;
    }
    if (Key.isDown(Key.RIGHT)) {
    _rotation += 4;
    }
    //INCREASE SPEED
    if (Key.isDown(Key.UP)) {
    speed += .2;
    }
    if (Key.isDown(Key.DOWN)) {
    speed -= .2;
    }
    //MAX SPEED
    if (Math.abs(speed)>50) {
    speed *= .94;
    }
    // FRICTION
    speed *= .97;
    //CALCULATES VECTORS
    speedx = Math.sin(radians)*speed;
    speedy = Math.cos(radians)*speed*-1;
    //CHANGES POSITION
    _x += speedx;
    _y += speedy;
    }




    Okay, basically what this script does is get the car to travel in the direction that the movie is pointing.

    Forgetting the right triangle for a moment, it's helpful to know that you can draw a circle like this:

    code:

    // center of circle
    cx = 100;
    cy = 100;

    // radius (width from center to rim) of circle
    rad = 50;

    moveTo(cx+rad,cy);

    for (a = 0; a < 2*Math.PI; a += .1)
    {
    x = cx + Math.sin(a)*rad;
    y = cy - Math.cos(a)*rad;
    lineTo(x,y);
    }




    So sin and cos basically describe a circle around a center point. Why do they do this? Read a book on Trig. For now, let's treat them as magic 'circle-making' functions.

    The code converts the current rotation of the movie (which goes from 0 to 360) into a value which goes from 0 to 2*PI
    (which is what sin and cosine want).

    code:

    degrees = this._rotation;
    radians = (degrees*(Math.PI/180));




    So we are converting from degrees (which go from 0-360) to radians (which go from 0 to 2pi).

    By the way, it's interesting to know that if the radius of a circle is 1, it's circumfrence happens to be 2*PI. So radians are a kind of 'natural' unit for functions that draw circles, like sin and cosine.

    The rotation is modified if the key is held down.

    code:

    //ROTATION
    if (Key.isDown(Key.LEFT)) {
    _rotation -= 4;
    }
    etc.



    The speed is modified as well. I'll skip that bit and move on to the part with sin/cos.

    We treat the car as if it is the center of a circle. Note that 0,0 is the center of the car.

    We find the point on the circle directly in front of the car (based on it's angle of rotation in radians).

    code:

    speedx = Math.sin(radians)*speed;
    speedy = Math.cos(radians)*speed*-1;



    Note that this is equivalent to my circle drawing code, in which cx,cy are 0,0.

    code:

    x = 0 + Math.sin(a)*rad;
    y = 0 - Math.cos(a)*rad;



    Then we go in that direction.

    code:

    _x += speedx;
    _y += speedy;



    If you manage to get a little more comfortable with sin and cos (and playing with drawing circles is a good way to start), then I suggest you visit my website (link below). The section 'Circles and Kaleidoscopes' contains a number of movies which exploit these functions to make pretty pictures.
    Last edited by jbum; 04-25-2004 at 04:48 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center