Solved

Very simple car-physics question

Posted on 2007-11-15
2
994 Views
Last Modified: 2013-11-23
First I want to say. Dont be scared of the length of this question. Just take the code and paste it into a
a frame in flash and it´ll start running. Its just there to show you the problem.

I´m trying to make a simple flashgame with cars. I found this code snippet from an earlier post here on Experts-Exchange that works well for driving a movieclip around on the screen. But it lacks skidding when turning the car.

I suck at math, so I´m looking for help with calculating the new x and y values for the car each round.

I think this should be a simple calculation in just one line of code here, but as I said, I´m really lousy at math. Its a few lines of code to read through, but its very straightforward, so I´m sure you will have no problem understand it.

After the code example I show you what I added to make it skid. And it looks a little better, but It´s not the right way to do it.




this.onEnterFrame = function() {
     
      var StageWidth:Number = 600;
      var StageHeight:Number = 600;
     
      // This determines what these variables do
      keyLeft = Key.isDown(Key.LEFT);
      keyRight = Key.isDown(Key.RIGHT);

      //the reason why these two are not capitilized any is because "keyUp" and"keyDown" are      commands in flash

      keyup = Key.isDown(Key.UP);
      keydown = Key.isDown(Key.DOWN);
      // By pushing on certain keys, you get acceleration and decceleration
      if (keyup) {
              this.tspeed += .25;
      }
      if (keydown) {
              this.tspeed -= 1.5;
              if (this.tspeed<-7.5) {
                        this.tspeed = -7.5;
              }
      }
      // Friction from ground; this can be any small number so that the car doesn't keep going
      // if a key isn't pressed
      this.tspeed *= .92;
      // Velocity Change
      idealxvelocity = this.tspeed*Math.cos(this.angle*Math.PI/180);
      idealyvelocity = this.tspeed*Math.sin(this.angle*Math.PI/180);
      // By taking the difference between the current velocity and ideal velocity,
      // you get the actual velocity
      // When you apply the friction, you get the actual velocities.
      this.xvelocity += ((idealxvelocity-this.xvelocity)*this.tfriction);
      this.yvelocity += ((idealyvelocity-this.yvelocity)*this.tfriction);
      // By adding the velocities, you can get the new x and y positions
      this._x += this.xvelocity;
      this._y += this.yvelocity;
      // By pushing the Left and Right keys, the car steers in that direction
      if (keyLeft || keyRight) {
             this.steering += (keyRight-keyLeft);
      } else {
              // When you let go of the Left and Right keys, the car goes back to going straight
              this.steering += ((this.steering<0)-(this.steering>0));
      }
      // You want to make sure that when you steer that it doesn't start turning faster and faster
      if (this.steering<-5) {            
              this.steering = -5;
      }
      if (this.steering>5) {
              this.steering = 5;
      }
      // Now you can make the car turn based on actual speed and turning angle
      actualSpeed = Math.sqrt((this.xvelocity*this.xvelocity)+(this.yvelocity*this.yvelocity));
      this.angle += (( (this.steering*.80)*actualSpeed)*.15);
      trace("steering " + this.steering);
      this._angle = (this.angle+360)%360-180;
     
      // Hit outside
      if (this._x>=StageWidth) {
              trace("Hit Wall");            
      }
     
      if (this._x<0) {
              trace("Hit Wall");      
      }
      //if (this.x<=10) {
              //this.x = 10;
      //}
      if (this._y>=StageHeight) {
              trace("Hit Wall");
      }
     
      if (this._y<0) {
              trace("Hit Wall");
      }
     
      //if (this.y<=-10) {
              //this.y = -10;
      //}
      // You need to update the angles from radians to degrees so that it can turn
      this._rotation = this.angle+90;
      this._x = this.x;
      this._y = this.y;
     
};



this is what I added. I marked the rows I added with a few ***. This makes it skid a little when turning, but not very good.

      if (keyLeft || keyRight) {
             *** if(this.xvelocity > 0) this._x += 0.5; ***
            ***  if(this.xvelocity < 0) this._x -= 0.5; ***
             this.steering += (keyRight-keyLeft);
      } else {
              // When you let go of the Left and Right keys, the car goes back to going straight
              this.steering += ((this.steering<0)-(this.steering>0));
      }

Thanks in advance

/Fredrik
0
Comment
Question by:otroligafredde
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 20296912
maybe try something like

this.onEnterFrame = function() {
     
      var StageWidth:Number = 600;
      var StageHeight:Number = 600;
     
      // This determines what these variables do
      keyLeft = Key.isDown(Key.LEFT);
      keyRight = Key.isDown(Key.RIGHT);

      //the reason why these two are not capitilized any is because "keyUp" and"keyDown" are      commands in flash

      keyup = Key.isDown(Key.UP);
      keydown = Key.isDown(Key.DOWN);
      // By pushing on certain keys, you get acceleration and decceleration
      if (keyup) {
              this.tspeed += .25;
      }
      if (keydown) {
              this.tspeed -= 1.5;
              if (this.tspeed<-7.5) {
                        this.tspeed = -7.5;
              }
      }
      // Friction from ground; this can be any small number so that the car doesn't keep going
      // if a key isn't pressed
      this.tspeed *= .92;
      // Velocity Change
      idealxvelocity = this.tspeed*Math.cos(this.angle*Math.PI/180);
      idealyvelocity = this.tspeed*Math.sin(this.angle*Math.PI/180);
      // By taking the difference between the current velocity and ideal velocity,
      // you get the actual velocity
      // When you apply the friction, you get the actual velocities.
      this.xvelocity += ((idealxvelocity-this.xvelocity)*this.tfriction);
      this.yvelocity += ((idealyvelocity-this.yvelocity)*this.tfriction);

      deltax = this.xvelocity - this._xvelocity;
      deltay = this.yvelocity - this._yvelocity;
 
      deltav = Math.sqrt((deltax*deltax)+(deltay*deltay));
 
      skid = skidv/(skidv+deltav);
   
      this.xvelocity = this._xvelocity + skid*deltax;
      this.yvelocity = this._yvelocity + skid*deltay;

      // By adding the velocities, you can get the new x and y positions
      this._x += this.xvelocity;
      this._y += this.yvelocity;
      // By pushing the Left and Right keys, the car steers in that direction
      if (keyLeft || keyRight) {
             this.steering += (keyRight-keyLeft);
      } else {
              // When you let go of the Left and Right keys, the car goes back to going straight
              this.steering += ((this.steering<0)-(this.steering>0));
      }
      // You want to make sure that when you steer that it doesn't start turning faster and faster
      if (this.steering<-5) {            
              this.steering = -5;
      }
      if (this.steering>5) {
              this.steering = 5;
      }
      // Now you can make the car turn based on actual speed and turning angle
      actualSpeed = Math.sqrt((this.xvelocity*this.xvelocity)+(this.yvelocity*this.yvelocity));
      this.angle += (( (this.steering*.80)*actualSpeed)*.15);
      trace("steering " + this.steering);
      this._angle = (this.angle+360)%360-180;
     
      // Hit outside
      if (this._x>=StageWidth) {
              trace("Hit Wall");            
      }
     
      if (this._x<0) {
              trace("Hit Wall");      
      }
      //if (this.x<=10) {
              //this.x = 10;
      //}
      if (this._y>=StageHeight) {
              trace("Hit Wall");
      }
     
      if (this._y<0) {
              trace("Hit Wall");
      }
     
      //if (this.y<=-10) {
              //this.y = -10;
      //}
      // You need to update the angles from radians to degrees so that it can turn
      this._rotation = this.angle+90;
      this._x = this.x;
      this._y = this.y;
      this._xvelocity = this.xvelocity;
      this._yvelocity = this.yvelocity;
     
}
0
 
LVL 3

Author Closing Comment

by:otroligafredde
ID: 31409507
Thanks

great!!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java 8 lambda expresssions exception handling 3 88
egit plugin on eclipse 8 62
tomcat not starting 6 45
eclipse apache tomcat admin console 52 94
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
Have you ever thought of installing a power system that generates solar electricity to power your house? Some may say yes, while others may tell me no. But have you noticed that people around you are now considering installing such systems in their …
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now