Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

How to avoid lag when using parallax effect in fullscreen ?

$
0
0

Hi,

I'm making a 2D game with, hopefully, side-scrolling. Following a discussion with Chrisir, I'm posting my lag issue here with a MCVE. I run the following code and when I add the parallax function in fullscreen, I get intense lag. When I run it at 960x540, its fine. My code is far from perfect this stage so if you see anything to improve, please let me know.

//I wont use all of these for this MCVE,
//but its usually what I import for Box2d and collisions.
import shiffman.box2d.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.joints.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.contacts.*;

// A reference to our box2d world
Box2DProcessing box2d;

//Declaring my hero
Hero hero;

PImage img;
PVector vBG;

void setup() {
  //fullScreen();
  size(960, 540);

  // Initialize box2d physics and create the world
  box2d = new Box2DProcessing(this);
  box2d.createWorld();

  // We are setting a custom gravity
  box2d.setGravity(0, -0.0005);

  //Initializing hero
  hero = new Hero(88, 88, 20);

  img = loadImage("Starsinthesky.jpg");

  vBG = new PVector(0, 0);
}

void draw() {

  imageMode(CORNER);
  parallaxe(img, vBG, hero.velocity().x);

  // Thru time
  box2d.step();

  //Show and move Hero
  hero.display();
  hero.mover();
  hero.boundary(); //This is what I call to limit the range of my here

}

Here is the parallax function :

void parallaxe(PImage img, PVector pos, float vit) {
  pos.sub(vit, 0, 0);

  if(pos.x < width-img.width) {
    image(img, pos.x+img.width, pos.y);
  }

  if(pos.x > 0) {
    image(img, pos.x-img.width, pos.y);
  }

  if(pos.x < -img.width) {
    pos.x = width;
  }

  if (pos.x > width) {
    pos.x = width-img.width;
  }

  image(img, pos.x, pos.y);
}

And here is my movable hero class :

class Hero {
  Body body;
  float r;

  Hero(float x, float y, float r_) {
    r = r_;
    // This function puts the hero in the Box2d world
    makeBody(x, y, r);
    body.setUserData(this);
  }

  void display() {
    Vec2 pos = box2d.getBodyPixelCoord(body);
    pushMatrix();
    translate(pos.x, pos.y);
    fill(0, 44, 188);
    noStroke();
    ellipse(0, 0, r*2, r*2);
    popMatrix();
  }

  void mover() {
    Vec2 move = new Vec2(0, 0);
    Vec2 pos = body.getWorldCenter();

    if ((keyPressed == true) && ((key == 'w') || (key == 'W'))) {
      move.y += 100;
    }
    if ((keyPressed == true) && ((key == 's') || (key == 'S'))) {
      move.y -= 100;
    }
    if ((keyPressed == true) && ((key == 'a') || (key == 'A'))) {
      move.x -= 100;
    }
    if ((keyPressed == true) && ((key == 'd') || (key == 'D'))) {
      move.x += 100;
    }
    body.applyForce(move, pos);
  }

  Vec2 velocity() {
    return body.getLinearVelocity();
  }

  //Here is the method that would limit the movement of my hero if it worked.
  //Box2d coordinate system is in the middle of the screen.
  void boundary() {
    Vec2 pos = box2d.getBodyPixelCoord(body);
    if (pos.x < r) {
    pos.x = r;
    }
    if (pos.x > width-r) {
    pos.x = width-r;
    }
    if (pos.y < r) {
    pos.y = r;
    }
    if (pos.y > height-r) {
    pos.y = height-r;
    }
  }

  // Here's our function that adds the particle to the Box2D world
  void makeBody(float x, float y, float r) {
    // Define a body
    BodyDef bd = new BodyDef();
    // Set its position
    bd.position = box2d.coordPixelsToWorld(x, y);
    bd.type = BodyType.DYNAMIC;
    body = box2d.createBody(bd);

    // Make the body's shape a circle
    CircleShape cs = new CircleShape();
    cs.m_radius = box2d.scalarPixelsToWorld(r);

    FixtureDef fd = new FixtureDef();
    fd.shape = cs;
    // Parameters that affect physics
    fd.density = 1;
    fd.friction = 0.01;
    fd.restitution = 0.3;

    // Attach fixture to body
    body.createFixture(fd);
  }

}

I hope you won't mind that it uses Box2D, its part of the game I make. I'm not sure if this is a lag factor. At the moment, I have no guess as to what causes this. Any hint is welcomed.


Viewing all articles
Browse latest Browse all 2896

Trending Articles