A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [F8] Perfecting car physics

  1. #1
    Junior Member
    Join Date
    Jun 2007
    Posts
    7

    [F8] Perfecting car physics

    I'm trying to perfect my car's physics and am running into a little bit of trouble.

    1- Crashing into a bar - When the car crashes into a car at an angle, it looks as if it is bouncing off. If it hits it strait on or at an angle to quickly, it gets jammed into the bar and either goes through it very very slowly, or backs out of it very very slowly, either way you have to keep pressing the up/down key to get out.

    2- Gears - I've already perfected this, the car has four gears, and when it hits each gear, how fast it accelerates changes. The cars don't have a max speed per say, but 4th gears acceleration is very low.

    3- Braking - I'm still working on this one, I've got a very simple slow down, but I am still working on a drifting style brake, where if the car turnes too much durring brake, it starts spinning out/fishtails.

    4- turning - perfected, I was going to add a drifting style of turning but said forget that. The turning point is on the frontwheel axis so backing up is realistic and turning isn't like in those kiddie games where it turnes from the center of the car (rofl).

    5 - speed transfer - I'm a little confused on this one, I'm not sure how to transfer variables from one object to the next. I think it might be _root.varname.objectname but probably not, help would be nice there. I want to be able to have a speed transfer, so basically ({Ms * [(Mw - Cw)/Mw]} + {Cs * [(Cw - Mw)/Cw]})/2 = Ms
    Ms is my speed, Cs is Crash speed (the other objects speed), Mw is my weight, Cw is Crash's weight (the other objects weight), and that is basically what it would be, if two different weighted objects crashed into eachother.
    This formula would work both ways for being hit, and hitting the other, where the speeds basically transfer at an equal weight, or are altered if they are different before transfer.

    If you can help, please give a a tip or send back altered source code.
    Code:
    onClipEvent (load) {
    	_rotation = 0;
    	var justhit = 0;
    	var rot:Number = 0;
    	var speed:Number = 0;
    	var maxSpeed:Number = 19;
    	var minSpeed:Number = -10;
    	var setSpeed:Number = maxSpeed;
    	var acc:Number = .3;
    	var setAcc:Number = acc;
    	var maxNos:Number = 500;
    	var nos:Number = maxNos;
    	var slow:Number = .98;
    	var brake:Number = .9;
    	var rev:Number = .2;
    }
    onClipEvent (enterFrame) {
    	_x += x;
    	_y += y;
    	rot = (speed/2);
    	_root.rotaters.text = rot;
    	_root.exp._x = 25;
    	_root.exp._y = 25;
    	
    	if(this.hitTest(_root.ground) && speed > 1 || speed < -1){
    	speed = -speed/3;
    		
    	}
    	
    	if (Key.isDown(Key.RIGHT) && (speed != 0)) {
    		_rotation += rot;
    		speed -= .05
    	} else if (Key.isDown(Key.LEFT) && speed != 0) {
    		_rotation -= rot;
    		speed -= .05
    	}
    	if (Key.isDown(Key.SPACE)) {
    		speed *= brake;
    		if (speed < 1 && speed > -1){
    			speed = 0;
    		}
    	} 	
           //&copy;BoredPortal.com
    	if (Key.isDown(Key.UP) && speed < 19) {
    		speed += acc;
    	} else if (Key.isDown(Key.UP) && (speed < 25 && speed > 19)) {
    		speed += .05
    	} else if (Key.isDown(Key.UP) && (speed > 25)) {
    		speed += .01
    	} else if (Key.isDown(Key.DOWN) && (speed<1)) {
    		speed -= rev;
    	} else if (Key.isDown(Key.DOWN) && (speed>=1)) {
    		speed *= brake;
    	} else {
    		speed *= slow;
    	}
    	if (speed>50) {
    		speed = 50;
    	}
    	if (speed<minSpeed) {
    		speed = minSpeed;
    	}
    	x = Math.sin(_rotation*(Math.PI/180))*speed;
    	y = Math.cos(_rotation*(Math.PI/180))*-speed;
    
    
    }
    This is MY code, don't take it and use what I made without consent from me and without giving credit where credit is due.

  2. #2
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    Is this all of your code?

    Could you post the rest, if you have any more?

    What code are you using for collision test for crashing?
    Z¡µµ¥ D££

    Soup In A Box

  3. #3
    Junior Member
    Join Date
    Jun 2007
    Posts
    7
    that is all of it, the very basic
    Code:
    	if(this.hitTest(_root.ground) && speed > 1 || speed < -1){
    	speed = -speed/3;
    is collision test, which I worked out a lot more so it is perfect for head on collision. I don't feel like opening that up though so I'm not posting that.

    Now I'm trying to think of how to have it so if the car hits the object at an angle, it rotates toward the object to become aligned with it. It would have to be above 0 degrees and below 45 degrees and I'm thinking it rotates half the speed in degrees untill it hits 0 or untill the hit test is false. I will work on that all today and maybe post my progress if nobody cares to add on.

  4. #4
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    well, you would have to get the angle that the car is facing and then the angle between the car and the object it hits. then take the difference of the two angles.

    use something like

    Code:
    //detect angle between two objects
    distOpp = Math.sqrt(Math.pow(mc2._x - mc2._x, 2)+Math.pow(mc2._y-mc1._y,2));
    distAdj = Math.sqrt(Math.pow(mc2._x - mc1._x,2)+Math.pow(mc1._y-mc1._y,2));
    hitLine2 = Math.atan2(distOpp, distAdj);
    hitLine = (Math.PI*hitLine2)/180;
    //that converts hitLine2 from radians to degrees
    that uses the atan2 function to find the angle of the arc tangent of the two objects.
    in other words...it finds the angle...

    the problem is that the atan2 function only returns the angle from 0 - 90, so in each quadrant (saying that the x and y of mc1 are 0,0 on the coordinate plane) it only returns positive angles. You need to tell it to make the angle negative in the other quadrants.

    2|1
    -+-
    3|4

    vertical is 0 degrees and horizontal is 90 degrees, so in quadrant 1, 89 degrees is just above horizontal, and in quadrant 2, 89 degrees is just below horizontal.
    Keep that in mind.

    Then you can get the angle of the car using either the same formula...or just get the rotation.

    I can't figure it all out right now, but play with that information until you get something...or don't get something.

    Hope it helps...or doesn't help!

    ~Zippy Dee
    Ted Newman
    Z¡µµ¥ D££

    Soup In A Box

  5. #5
    Junior Member
    Join Date
    Jun 2007
    Posts
    7
    Thanks, that may help. I'll try to plug it into the actionscript and test it out with a few objects.

  6. #6
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    use some trace actions to get used to what angles things are at

    try it with an object and the _xmouse and _ymouse so you can see how angles work.

    That's what I had to do before I fully understood it.
    Z¡µµ¥ D££

    Soup In A Box

  7. #7
    Junior Member
    Join Date
    Jun 2007
    Posts
    7
    I've actually got a protracter at drag so I can measure angles, but I'll do x and y untill I get whats going on. thx

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