Car only moving in the y-axis

18 posts

Flag Post

I am trying to make a car move using the arrow keys. So thing is, the car rotates, and gains / loses speed totally fine, but problem is that the car only moves in the y-axis, not the x-axis.

any Ideas?

here is the code;

import flash.events.Event;
import flash.events.KeyboardEvent;

var acceleration:Number=0.4;
var speed_decay:Number=0.96;
var rotation_step:Number=10;
var max_speed:Number=10;
var back_speed:Number=1;
var speed:Number=0;
var accelerate,brake,turn_left,turn_right:Boolean=false;


addEventListener(Event.ENTER_FRAME,init);


function init(e:Event):void {
	addEventListener(Event.ENTER_FRAME,on_enter_frame);
	stage.addEventListener(KeyboardEvent.KEY_DOWN,on_key_down);
	stage.addEventListener(KeyboardEvent.KEY_UP,on_key_up);
}

function on_enter_frame(e:Event):void {
	if (accelerate&&speed<max_speed) {
		speed+=acceleration;
	}
	if (brake&&speed>-1) {
		speed-=back_speed;
	}
	var speed_x:Number=Math.sin(rotation*0.0174532925)*speed;
	var speed_y:Number=- Math.cos(rotation*0.0174532925)*speed;
	if (turn_left) {
		bil.rotation -= rotation_step*(speed/max_speed);
	}
	if (turn_right) {
		bil.rotation += rotation_step*(speed/max_speed);
	}
	bil.y+=speed_y;
	bil.x+=speed_x;
	
	if (Math.abs(speed)>0.3) {
		speed*=speed_decay;
	}
	else {
		speed=0;
	}
}

function on_key_down(e:KeyboardEvent):void {
            if (e.keyCode==38) {
                accelerate=true;
            }
            if (e.keyCode==40) {
                brake=true;
            }
            if (e.keyCode==37) {
                turn_left=true;
            }
            if (e.keyCode==39) {
                turn_right=true;
            }
        }
        function on_key_up(e:KeyboardEvent):void {
            if (e.keyCode==38) {
                accelerate=false;
            }
            if (e.keyCode==40) {
                brake=false;
            }
            if (e.keyCode==37) {
                turn_left=false;
            }
            if (e.keyCode==39) {
                turn_right=false;
            }
        }
 
Flag Post

Please read here on how to format your code properly

If you cannot get it to work, use something like pastebin.

 
Flag Post

Sorry, the code can be found here aswell; http://ideone.com/C1kkF2

 
Flag Post

You calculation of the x, y components of the speed vector are based on the wrong rotation:
var speed_x:Number=Math.sin(rotation*0.0174532925)*speed;

rotation is accessing this property of whatever your code is on, not your “bil” object.
sin(0) is 0 so speed_x is 0, too

Try coding in classes, it helps to think on one problem separated from others.

 
Flag Post

I find this weird, as the y axis works, even if I set speed_y = cos(1), (which is also 0) …
Any ideas on how I can fix this? :S

 
Flag Post
Originally posted by SimonOlsen94:

I find this weird, as the y axis works, even if I set speed_y = cos(1), (which is also 0) …

cos (1) = 0.5403… != 0

 
Flag Post

Sorry, meant cos(90)=0 and it still works for cos(90) …
But do you have any thoughts on how to fix it? :/

 
Flag Post

cos(90) = -0.4480… != 0

Debug your code step by step and see what’s going on.
Especially what values the variables have.

Again, I told you one flaw of your code.

 
Flag Post

I didnt quite understand the flaw :F Did you mean I have to change the value of the vector?

 
Flag Post

Yes, because the vector having the wrong values causes the issue.

Your calculation is based on the wrong rotation.

 
Flag Post

What values should it have? :S

 
Flag Post

So I tried changing it to this
var speed_x = Math.sin(rotation*(Math.PI/180))speed;
var speed_y = Math.cos(rotation
(Math.PI/180))speed-1;
but it didnt have any effect, The exact same thing happened

 
Flag Post

*sigh*

What do you think is rotation?

 
Flag Post
Originally posted by NineFiveThree:

What do you think is rotating?

Fixed. It’s not the concept of rotation that’s the problem; it’s the object being rotated.

 
Flag Post

You need to put your bil in there somewhere. (Hope I didn’t spoil it)

 
Flag Post

Wow, Sorry for being a slow learner! Thanks for the input! ^^,

 
Flag Post

It is not

rotation*(180/3.14159)

But
rotation/(180/3.14159)

You are converting degrees (full circle=360) to radians(full circle=2*pi)

 
Flag Post
Originally posted by JohnnyBohnny:

It is not

rotation*(180/3.14159)

But
rotation/(180/3.14159)

You are converting degrees (full circle=360) to radians(full circle=2*pi)

Which is what he does.