We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello hello people on this forum!
I have just started programming (in processing) 2 weeks ago, and I have messed around a bit and I am pleased with how far I've come so far! I do have a question though. I want to know how I can apply gravity to my simulation. And I also would like to know how to add a timer, to trigger gravity once a certain amount of time has passed. I was wondering if anyone would mind to take a look and maybe help me with what I would like to achieve.
//Autor @ Tedisvet //This project creates a simulation of a square which you can move around by using the ASWD keys. // To do list: // - Create more obstacles // - Load textures // - Watch tutortials by Jose Sanches
float xpos = 0; //Variable for the X position of the rectangle
float ypos = 0; //Variable for the Y position of the rectangle
float xc = 2; //Variable for the speed of the player rectangle (X direction)
float yc = 2; //Variable for the speed of the player rectangle (Y direction)
void setup() {
size(500, 500); //Size of the window
background(100);
frameRate(200);
smooth();
gravity = 0.1;
}
void draw() {
background(100);
fill(255);
rect(xpos, ypos, 50, 50); //This is the rectangle the player is controlling
rect(200, 200, 100, 100); //This is the big rectangle in the middle
if (keyPressed) {
if (key == 'w' || key == 'W') { //Makes the rectangle go up when you press the W key
if ( ypos <= 0) {
} else {
ypos -= yc; //Prevents the player from going off the screen
}
if (xpos <= 300 && xpos >= 150 && ypos >= 150 && ypos <= 300 ) { //Sets a border when the big square is approached from below, it will stop at its edge
ypos += yc;
println("-----Hit-----");
}
}
}
if (keyPressed) {
if (key == 's' || key == 'S') { //Makes the rectangle go down when you press the S key
if ( ypos >= 450) {
} else {
ypos += yc; //Prevents the player from going off the screen
}
if (xpos >= 150 && xpos <= 300 && ypos >= 150 && ypos <= 300) { //Sets a border when the big square is approached from above, it will stop at its edge
ypos -= yc;
println("-----Hit-----");
}
}
}
if (keyPressed) {
if (key == 'a' || key == 'A') { //Makes the rectangle go left when you press the A key
if ( xpos <= 0) {
} else
{
xpos -= xc; //Prevents the player from going off the screen
}
if (ypos <= 300 && ypos >= 150 && xpos >= 150 && xpos <= 300) { //Sets a border when the big square is approached from the right, it will stop at its edge
xpos += xc;
println("-----Hit-----");
}
}
}
if (keyPressed) {
if (key == 'd' || key == 'D') { //Makes the rectangle go right when you press D
if ( xpos >= 450) {
} else
{
xpos += xc; //Prevents the player from going off the screen
}
if (ypos >= 150 && ypos <= 300 && xpos >= 150 && xpos <= 300) { //Sets a border when the big square is approached from the left, it will stop at its edge
xpos -= xc;
println("-----Hit-----");
}
}
}
}
Answers
Why exactly do you mean by gravity? Do you want a sun/planets type situation where certain objects orbit another one? Do you want all of your objects to affect all of the other objects? Do you just want all of the objects to fall down? Something else?
What have you tried? What exactly are you confused about?
Thanks for the response :) I would like a gravity "downwards". I have tried some things but every time the block either got stuck on the bottom or fall through the bottom of the screen.
Oops I accedentally accepted your response as an answer xD
I'm pretty surprised your searches on google and in this forum haven't turned up any posts or tutorials on this subject, since it's been covered so many times.
I did write a tutorial on how to do this here, if you're interested: http://staticvoidgames.com/tutorials/basicConcepts/animation
You might also want to check out the examples under the Motion category in File -> Examples from the Processing IDE.