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.
Page Index Toggle Pages: 1
loading image (Read 532 times)
loading image
Apr 27th, 2009, 2:39am
 
hello!

I'm very new to Processing and even to programming at all.
I was wondering how in the collision example the white ball can be replaced by a picture that is (100 x 73)?

It would be wonderful if someone could help me with this!

Thank you,
Luc
Re: loading image
Reply #1 - Apr 27th, 2009, 2:55am
 
write a image on the place where the ball should be.

look here http://processing.org/reference/PImage.html

If you can give a link to the collision example or tell me which page it is in the book if it is from the book then I can help u more.
Re: loading image
Reply #2 - Apr 27th, 2009, 3:50am
 
hello and thanks for your quick answer.
It's the example in Processing itself, choose: file/examples/topics/motion and there it is.

I tried this and in the section "draw ball" I put:

PImage ball;
ball = loadImage("myimage.jpg");
image(ball, ball_x, ball_y);

But this doesn't work (I get a NullPointerException) although my picture is in the correct file.

I thought hat maybe it is because of the size. Therefore I changed ball_size into 100 or 73 (my picture is 100x73). I also deleted "float ball_size = 15;  // Radius"

This is what I have now (as a slight variation of the Processing example):

/**
* Collision (Pong).
*
* Move the mouse up and down to move the paddle.  
*/

// Global variables for the ball
float ball_x;
float ball_y;
float ball_dir = 1;
float dy = 0;  // Direction

// Global variables for the paddle
int paddle_width = 10;
int paddle_height = 60;

int dist_wall = 15;

void setup()
{
 size(640, 360);;
 noStroke();
 smooth();
 ball_y = height/2;
 ball_x = 1;
}

void draw()
{
 background(51);
 
 ball_x += ball_dir * 1.0;
 ball_y += dy;
 if(ball_x > width + 100) {
   ball_x = -width/2 - 100;
   ball_y = random(0, height);
   dy = 0;
 }
 
 // Constrain paddle to screen
 float paddle_y = constrain(mouseY, paddle_height, height-paddle_height);

 // Test to see if the ball is touching the paddle
 float py = width-dist_wall-paddle_width-73;
 if(ball_x == py
    && ball_y > paddle_y - paddle_height - 73
    && ball_y < paddle_y + paddle_height + 73) {
   ball_dir *= -1;
   if(mouseY != pmouseY) {
     dy = (mouseY-pmouseY)/2.0;
     if(dy >  5) { dy =  5; }
     if(dy < -5) { dy = -5; }
   }
 }
 
 // If ball hits paddle or back wall, reverse direction
 if(ball_x < 100 && ball_dir == -1) {
   ball_dir *= -1;
 }
 
 // If the ball is touching top or bottom edge, reverse direction
 if(ball_y > height-73) {
   dy = dy * -1;
 }
 if(ball_y < 73) {
   dy = dy * -1;
 }

 // Draw ball
 PImage ball;
 ball = loadImage("flyingcarpet");
 image(ball, ball_x, ball_y);
 
 // Draw the paddle
 fill(153);
 rect(width-dist_wall, paddle_y, paddle_width, paddle_height);  
}

Thanks for helping me out!

Luc
Re: loading image
Reply #3 - Apr 27th, 2009, 7:01am
 
Argh!  Roll Eyes
Never use loadImage() in draw(), unless you really want to read a file 60 times per second! (or unless you know what you do and have proper conditionals, etc.).

Put the PImage ball; line before setup() (so both functions can access it), the loadImage line in setup, and leave the image() call where it is now.

"my picture is in the correct file"
Is it in the data folder in the sketch folder?
Re: loading image
Reply #4 - Apr 29th, 2009, 7:43am
 
Hello and thank you for your reply.
Using my image worked very well now thanks to your advice! Now I have to make sure that the collision with the bar works fine...

Greetings
luc
Page Index Toggle Pages: 1