Gravity and platforms
in
Programming Questions
•
4 months ago
Hello everyone, I have a little question about basic gravity and platforms.
In my programs, there's two squares : player and enemy. There is also a bunch of platforms, "solid" structures that no object is supposed to pass by.
Here are my problems :
- I just don't know how to come up with a basic gravity feature. I guess there is a force that pulls down the objets and jumping, for example, is just "going against" that force?
- I managed to "detect" if one square is over a platform, but I don't know how to make the platform "solid" as in : the animated objets cannot go into that part of the screen.
Here's my code, I hope you guys can help me.
Also, any comment on my code or problems with it will be more than welcome.
Thanks!
The code is divided in two parts. Main and Classes.
- Player myPlayer;
- Enemy myEnemy;
- Platform [] myPlats = new Platform [6];
- void setup() {
- //boolean lost = false;
- noSmooth();
- size(600, 600);
- frameRate(60);
- background(255);
- myPlayer = new Player(300, 570);
- myEnemy = new Enemy(100, 100, 20, 20);
- myPlats [0] = new Platform(0, 170, 220, 10);
- myPlats [1] = new Platform(380, 170, 220, 10);
- myPlats [2] = new Platform(150, 290, 300, 10);
- myPlats [3] = new Platform(0, 410, 220, 10);
- myPlats [4] = new Platform(380, 410, 220, 10);
- myPlats [5] = new Platform(0, 590, 600, 10);
- }
- void draw() {
- background(255);
- for (int i = 0; i < myPlats.length; i++) {
- myPlats[i].drawPlatform();
- }
- myEnemy.run();
- myPlayer.run();
- }
- //Classes
- //Classes
- //The Player and the enemy are a class
- float posX = 0;
- float posY = 0;
- float wX = 20;
- float hX = 20;
- float speed = 5;
- float gravity = 0.2;
- class Player {
- //GLOBAL VARIABLES
- //CONSTRUCTOR
- Player(float _posX, float _posY) {
- posX = _posX;
- posY = _posY;
- }
- //FUNCTIONS
- void run() {
- display();
- //collision();
- //vibration();
- System();
- pMovement();
- }
- void display() {
- noStroke();
- fill(0);
- rect(posX, posY, wX, hX);
- }
- //Ugghh...
- void vibration() {
- if (!keyPressed) {
- posY = posY + random(-1, 1);
- }
- }
- void pMovement() {
- //First condition, not sure about it
- // if ((((posX <=width) && posX >= 0)) && ((posY <=height) && (posY >=0))) {
- {
- if (keyPressed && keyCode == LEFT) {
- posX = (constrain(posX-speed, 0, 600));
- }
- if (keyPressed && keyCode == RIGHT) {
- posX = (constrain(posX+speed, 0, 580));
- }
- }
- }
- // }
- void System() {
- boolean collision = rectangle_collision(posX, posY, wX, hX, posEx, posEy, wEx, hEy);
- if (collision) {
- posX +=random(-3, 3);
- fill(255, 0, 0, 125);
- rect(0, 0, 1000, 1000);
- fill(255);
- stroke(255);
- text("---------------------", width/2, height/2, 100, 100);
- ;
- }
- }
- boolean rectangle_collision(float posX, float posY, float wX, float hX, float posEx, float posEy, float wEx, float hEy)
- {
- return !(posX > posEx+wX || posX+wX < posEx || posY > posEy+hEy || posY+hX < hEy);
- }
- }
- //Enemy
- float posEx = 0;
- float posEy = 150;
- float wEx = 20;
- float hEy = 20;
- class Enemy {
- // Constructor
- Enemy(float _posEx, float _posEy, float _wEx, float _hEy) {
- posEx = _posEx;
- posEy = _posEy;
- wEx = _wEx;
- hEy = _hEy;
- }
- float enSpeed = 5;
- float gravity = 1;
- void run() {
- display();
- movement();
- // gravity();
- }
- void display() {
- fill(255, 20, 20);
- rect(posEx, posEy, 20, 20);
- }
- void movement() {
- gravity();
- posEx += enSpeed;
- if ((((posEx >=585) || posEx <= 0)))
- enSpeed = 0;
- if (((posEy >=570) || (posEy <=0))) {
- enSpeed = 0;
- }
- }
- void gravity() {
- posEy += gravity;
- }
- }
- //Controls of enemy, if needed
- /*
- if (keyPressed && keyCode == LEFT) {
- posEx = (constrain(posEx-enSpeed, 0, 600));
- }
- if (keyPressed && keyCode == RIGHT) {
- posEx = (constrain(posEx+enSpeed, 0, 580));}
- if (keyPressed && keyCode == UP) {
- posEy = posEy-enSpeed;}
- if (keyPressed && keyCode == DOWN) {
- posEy = posEy+enSpeed;}
- */
- // CHANGE CONTROLS FOR ENEMY.
- // CHECK BOUNDERIES, SINCE THEY'RE NOT WORKING AT ALL FOR ENEMY
- class Platform {
- float xPos;
- float yPos;
- float platWidth;
- float platHeight;
- Platform(float x, float y, float w, float h) {
- xPos = x;
- yPos = y;
- platWidth = w;
- platHeight = h;
- }
- void drawPlatform() {
- collisionEn();
- displayPlat();
- // collisionPlay();
- }
- void displayPlat() {
- fill(0);
- rect(xPos, yPos, platWidth, platHeight);
- }
- void collisionEn() {
- for (int i = 0; i < myPlats.length; i++) {
- if (posEx > myPlats[i].xPos
- && posEx+wEx > myPlats[i].xPos
- && posEx+wEx < myPlats[i].xPos + myPlats[i].platWidth
- && posEx < myPlats[i].xPos + myPlats[i].platWidth
- && posEy > myPlats[i].yPos
- && posEy < myPlats[i].yPos + myPlats[i].platHeight
- && posEy+hEy > myPlats[i].yPos
- && posEy+hEy < myPlats[i].yPos + myPlats[i].platHeight)
- rect(0, 0, 1000, 1000);
- }
- }
- // void collisionPlay(){
- // for (int i = 0; i < myPlats.length; i++) {
- // if (((posX > myPlats[i].xPos) && (posX < myPlats[i].xPos + myPlats[i].platWidth)) && ((posY > myPlats[i].yPos) && (posY < myPlats[i].yPos + myPlats[i].platHeight)))
- // rect(0,0,1000,1000);}}
- }
1