Hello, I am trying to make a rectangle move like a car. I want the red rectangle to move the same way the three green circles are moving.
In the first picture the three circles are aligned with the rectangle. In the second picture the three circles have changed orientation and turned. The rectangle has moved but It hasn’t changed orientation or turned like the circles did. I want the rectangle to move like the circles are moving. From my code the rectangle is suppose to turn in the direction of carHeading on the angle steerAngle (which the circles are doing). My idea is that I draw the rectangle with the angle carHeading or I use ofNode to make the rectangle a child of the circles, however I do not know how to either of these things. Any help would be much appreciated.
Code:
#pragma once
#include "ofMain.h"
class ofApp : public ofBaseApp {
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y);
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofVec2f backWheel;
ofVec2f frontWheel;
ofVec2f carLocation;
float wheelBase;
float steerAngle;
float carSpeed;
float carHeading;
float turn;
};
// ofApp.cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup()
{
ofSetFrameRate(60);
carLocation.set(100, 100);
carHeading = 0;
wheelBase = 90;
carSpeed = 0;
}
//--------------------------------------------------------------
void ofApp::update()
{
//position of front wheel
frontWheel = carLocation + wheelBase / 2 * ofVec2f(cos(carHeading), sin(carHeading));
//position of back wheel
backWheel = carLocation - wheelBase / 2 * ofVec2f(cos(carHeading), sin(carHeading));
//movement of front wheel
frontWheel += carSpeed * 1/60 * ofVec2f(cos(carHeading + steerAngle), sin(carHeading + steerAngle));
//movement of back wheel
backWheel += carSpeed * 1/60 * ofVec2f(cos(carHeading), sin(carHeading));
//new car location
carLocation = (frontWheel + backWheel) / 2;
//new car heading
carHeading = atan2(frontWheel.y - backWheel.y, frontWheel.x - backWheel.x);
}
//--------------------------------------------------------------
void ofApp::draw()
{
ofFill();
ofSetColor(255, 0, 0);
ofSetRectMode(OF_RECTMODE_CENTER);
ofDrawRectangle(carLocation, 100, 60);
ofFill();
ofSetColor(0, 255, 0);
ofDrawCircle(carLocation, 10);
ofDrawCircle(frontWheel, 10);
ofDrawCircle(backWheel, 10);
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key)
{
if (key == 'd') //right
{
steerAngle = +15 * 180 / PI;
}
if (key == 'a')//left
{
steerAngle = -15 * 180 / PI;
}
if (key == 'w')//accelerate
{
carSpeed = (1+ carSpeed)* 5.0;
}
if (key == 's') //slow down
{
carSpeed = carSpeed / 5.0;
}
if (key == 'f') //go straghit
{
steerAngle = 0;
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key)
{
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button)
{
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button)
{
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button)
{
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
#include "ofMain.h"
#include "ofApp.h"
//========================================================================
int main( ){
ofSetupOpenGL(1024,768,OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new ofApp());
}