hello all,
I am working on a steering simulation for a car.
So far the car moves (cursor up and down) and the tires turn (cursor left and right)
But I don't know how to apply the angle of my wheel car.wheelAngle to the cars movement?
Any hints? Important parts are bold in the code below.
At the moment, I apply the cars gobal angle car.angleCar (compared to north (or east)) to its movement but I don't know how to integrate car.wheelAngle into this.
Thanks!!!
Greetings, Chrisir
// see http://www.asawicki.info/Mirror/Car%20Physics%20for%20Games/Car%20Physics%20for%20Games.html
//
// one car of class Car
Car car;
//
// ---------------------------------------------------------------
//
void setup()
{
// init
size(800, 600);
car = new Car();
} // func
//
//
void draw()
{
background(255);
car.display();
car.drive();
text("cursor: drive and steer, space: stop, mouse: steer", 10, height-20);
} // func
//
// =====================================================================
void keyPressed () {
if (key==CODED)
keyPressedCoded();
else
keyPressedUnCoded();
}// func
void keyPressedCoded() {
switch (keyCode) {
case UP:
println ("up");
car.xspeed=1;
break;
case DOWN:
car.xspeed=-1;
break;
case LEFT:
car.wheelAngle++;
break;
case RIGHT:
car.wheelAngle--;
break;
} // switch
}// func
void keyPressedUnCoded() {
//
switch (key) {
case ' ':
// spacee = stop
car.xspeed=0;
// car.yspeed=0;
break;
default:
break;
} // switch
} // func
// =====================================================================
void mouseMoved() {
float value = mouseX;
car.wheelAngle = map(mouseX, 0, width, -60, 60);
if (value > 255) {
value = 0;
}
}
// =====================================================================
class Car {
// the car itself
float xpos;
float ypos;
float w, h;
float xspeed;
//float yspeed;
color c;
float angleCar=12;
// 4 tires
float tireOffsetX, tireOffsetY; // position as offset from car center
float tireW, tireH; // size
float L = tireOffsetX*2; // the dist between tires front and rear
// wheel
//PImage imgWheel;
float wheelAngle = 0;
//
Car() {
// constr
xpos = width/2;
ypos = height/2;
w=70;
h=25;
// speed = stop
xspeed = 0;
//yspeed = 0;
c = color(255);
tireW=19;
tireH=11;
tireOffsetX=w/2;
tireOffsetY=h/2+5;
//imgWheel=loadImage("wheel.jpg");
} // constr
void display() {
// display car
pushMatrix();
rectMode(CENTER);
translate(xpos, ypos);
rotate(radians (angleCar)); //
// the car itself
fill(c);
rect(0, 0, w, h);
//
//
// the tires
// rear tires
fill(255, 2, 2);
//rect(xpos-tireOffsetX, ypos-tireOffsetY, tireW, tireH);
oneTire(-tireOffsetX, tireOffsetY, 0 );
oneTire(-tireOffsetX, -tireOffsetY, 0 );
// front tires
oneTire(tireOffsetX, tireOffsetY, -radians (wheelAngle)/3);
oneTire(tireOffsetX, -tireOffsetY, -radians (wheelAngle)/3);
//rect(xpos-tireOffsetX, ypos+tireOffsetY, tireW, tireH);
popMatrix();
//
// wheel - upper left corner
pushMatrix();
imageMode(CENTER);
translate(120, 120);
rotate(-radians (wheelAngle));
// image (imgWheel, 11, 11);
popMatrix();
}
void oneTire ( float xpos1, float ypos1, float angleTire ) {
// front tire with steering
pushMatrix();
translate(xpos1, ypos1);
rotate(angleTire);
rect(0, 0, tireW, tireH);
popMatrix();
}
//
void drive() {
xpos = xspeed * cos (radians(angleCar)) + xpos;
ypos = xspeed * sin (radians(angleCar)) + ypos;
if (xpos > width) {
xpos = 0;
} // if
//
// check walls
if (xpos < 0) {
xpos = width;
} // if
if (ypos > height) {
ypos = 0;
} // if
if (ypos < 0) {
ypos = height;
} // if
} // func
//
} // class
// =====================================================================
1