We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone, first post here. Just started learning processing and i'd like to ask you something about the code i'm writing. I've spent quite sometime figuring out how to move the rectangle across the screen using WASD and i think i've solved it pretty flawless, now i'm stuck trying to make it rotate left and right by using Q-E. The problem is that i'm actually drawing another rectangle when pressing E instead of modifying the parameter of the existing one, and i can't fathom how to do it. Also i don't know if it's possible to make it rotate left using rotate(radians(frameCount));
Here's the code i wrote:
object myObject;
boolean [] keys = new boolean[128];
void setup(){
size(600, 600);
smooth();
myObject = new object();
}
void draw(){
background(0);
myObject.run();
}
void keyPressed(){
keys[key] = true;
}
void keyReleased(){
keys[key] = false;
}
and that's the object class with the problematic function
class object {
float x = width/2;
float y = height/2;
int speed = 5;
object() {
}
void run() { //generic function container
display();
move();
}
void display() {
x=constrain(x, 25, width-25);
y=constrain(y, 25, height-25);
rectMode(CENTER);
rect(x, y, 50, 50);
}
void move() {
if (keys['a']) //move left
x= x-speed;
if (keys['d']) //move right
x= x+speed;
if (keys['w']) //move up
y= y-speed;
if (keys['s']) //move down
y= y+speed;
if (keys['e']) //rotate right (calls for another function)
rotateR();
if (keys['q']) //rotate left (unwritten code)!!!
rotateL();
}
void rotateR() { //PROBLEMATIC FUNCTION HOW DO I FIX IT?
pushMatrix();
translate(x, y);
rotate(radians(frameCount));
rect(0,0,50,50);
popMatrix();
}
void rotateL(){ // TODO
}
}
any help would be appreciated. ty
Answers
First note: Change the name of your class. Object is very generic. Also there is a class in Java called Object and this class is a very important one. All instances from any classes inherits from this class directly or indirectly. When you create a class called "object", you are just looking for trouble :-B Easy fix, name it differently like ShipObject or MonsterObj. Btw, notice it is a common (and recommended) practice to write class's names using the TitleSchema, first letter always on uppercase. For variables and functions, first letter is always in lowercase.
Related to your rotation question, I don't think frameCount should be use here at all. Instead, create a variable called rotation inside your class and modify this variable based on Q/E inputs. A brief layout below. It might need minor tweaking but it shows the concept.
Kf
ty very much kfrajer, that's what i was looking for, i rewrote the code to use PVectors instead of a bunch of floats, by adding a rotation variable i don't even need rotateR() and rotateL() anymore. Also thanks for the "style" advices, better be precise from the start to avoid developing bad habits.
here's the updated code for future references:
and the Ship class
now onto the next step, i want it to rotate left and right and move forward based on it's facing direction. Probably i'll need to write another help me post in a couple of days :D