We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Using PImage as map
Page Index Toggle Pages: 1
Using PImage as map (Read 418 times)
Using PImage as map
Dec 12th, 2008, 1:11am
 
Hey guys,

My next programmer's goal is to make a very, very small side-scrolling level. The objective is to have a simple Object called a Bot (rectangle) move using the arrow keys. It can fall and jump. And most importantly, it treats a .GIF as the "world space" where 100% opacity is solid ground, and 0% opacity is open free space. Finally, the level "scrolls" horizontally based on whether or not the Bot.x is lesser than 1/3 width or greater than 2/3 width.

Some pseudocode:
----
   -Globals: PImage background, Robot Class. World Class? float friction? float gravity?(ironic)
   -Setup: Screensize. load World. load Bot.
   -Draw: Draw Everything. Move World based on Bot.x
   -keyPressed: Move Bot.
----

   -Robot: Contains x,y and w.(PVector?)
   ---Build a Bot that spawns at leftside of level, with a Width of 15 pixels.
   -----Bot should Compare its x,y coordinates against the coordinates of the PImage and Save that location as CurrentLocation. On next frame, currentLocation becomes pastLocation. Compare again. If currentLocation coordinates share the same space as an RGB 100% space, currentLocation is changed back to pastLocation.
    -------Bot should move based on keyPressed functions that send values to either x or y.
    ---------display Bot as a semi-transparent Rectangle.
----
    -Worldspace Class.
    --world consists of a PImage and a 2D Array.
    ---World is built with an Image and the 2D Array is equal to the pixel dimensions of the image, with each Array storing the Opacity value of each pixel.
    ----World should have a function that returns the Opacity value of a given pixel or MULTIPLE pixels.

Here's what little I have so far (but it works):
Code:

//Globals
PImage demobg;
int x = 0;
//Robot bot;

void setup() {

size(400,400);

// bot = new Robot();
//load our image
demobg = loadImage("demo_bg.gif");
}


void draw() {
background(255);
image(demobg, x, 0);
println(x);
}


//Scroll Logic
void keyPressed() {
switch(keyCode) {
case RIGHT:
x -= 5;
if (x < (-demobg.width + width)) {
x = (-demobg.width + width);
}
break;

case LEFT:
x += 5;
if (x > 0) {
x = 0;
}
break;
}
}


and the background:
...

Any help/pointers or things I should research would be appreciated. I already looked at the Off-Screen Buffer hack but that only helped me for directional keyPresses, really.
Re: Using PImage as map
Reply #1 - Dec 12th, 2008, 2:04pm
 
You're going to need to put a collision routine in that pseudo code of yours.

Collision should be resolved after movement is processed and before drawing occurs.
Re: Using PImage as map
Reply #2 - Dec 12th, 2008, 8:52pm
 
I figured I would mimic Shiffman's "Catcher" class in the raindrop program for collision. Is there some other, Generally Accepted method of collison?
Page Index Toggle Pages: 1