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 & HelpPrograms › Mini Golf/ put putt
Page Index Toggle Pages: 1
Mini Golf/ put putt? (Read 1923 times)
Mini Golf/ put putt?
Mar 22nd, 2009, 1:20am
 
Has anyone made a mini golf game? I'm in the progress of making one, but I am the newest of newbies. I have the course loaded as a jpeg and just used a class for the"wall" to constrain the ball and a seperate classes for the ball and hole. I was wondering what the easiest way to putt the ball would be. Thanks for help and examples in advance!


















PImage b;
Ball ball1;
Hole hole1;
Wall w1,w2,w3,w4,w5,w6;
boolean moving = false;


void setup() {
 size(800, 800);
 smooth();
 b = loadImage("Level1.jpg");//mini golf background



 w1 = new Wall(75,75,645,2);
 w2 = new Wall(75,75,2,195);
 w3 = new Wall(75,270,465,2);
 w4 = new Wall(540,270,2,300);
 w5 = new Wall(540,570,180,2);
 w6 = new Wall(720,75,2,495);







 ball1 = new Ball(10,135,175);
 hole1 = new Hole(632,530,25);


}

void draw (){

 image(b,0,0);


 ball1.move();

 w1.collide();
 w2.collide();
 w3.collide();
 w4.collide();
 w5.collide();
 w6.collide();



 ball1.display();
 hole1.display();


 println(mouseX);
 println(mouseY);
 
 if (moving == false) {
   stroke(255);
   line(mouseX,mouseY,ball1.x,ball1.y);
 }

}


Re: Mini Golf/ put putt?
Reply #1 - Mar 22nd, 2009, 3:42am
 
Maybe i would create 2 maps, the one you already got showing the course and the other one just showing walls, the hole and so one in different colours...  guess this is an easy way to create courses with photoshop... you can then just check the color at the ball point http://processing.org/reference/get_.html and decide what to do... white line=collision, red=putt, and even simulate different undergrounds like sand where the ball behave differently...

http://www.bilder-hochladen.net/files/aaee-1.jpg
( i made a mistake here, you shouldnt use antialiasing)
Re: Mini Golf/ put putt?
Reply #2 - Mar 22nd, 2009, 2:21pm
 
smoth edges are a good Idea, making the edges a bit more predictable
Re: Mini Golf/ put putt?
Reply #3 - Mar 26th, 2009, 8:53am
 
Cedric wrote on Mar 22nd, 2009, 3:42am:
create 2 maps, the one you already got showing the course and the other one just showing walls, the hole and so one in different colours...  guess this is an easy way to create courses with photoshop... you can then just check the color at the ball point http://processing.org/reference/get_.html and decide what to do... white line=collision, red=putt, and even simulate different undergrounds like sand where the ball behave differently...


Ok I think I understand what you mean, but how would I do two maps on top of each other in processing idk....maybe im way off.
Re: Mini Golf/ put putt?
Reply #4 - Mar 26th, 2009, 9:08am
 
you only show one map on screen. the other one is only there to check your course....

this code wont run but just to show you what i mean...

PImage colorMap = loadImage("color.jpg");
PImage bwMap = loadImage("bw.jpg");

image(colorMap, 0, 0); //shows the green course on screen

//check the bw map at the ball position
if( bwMap.get(ballX, ballY) == 255) then ball should deflect off the wall...

if( bwMap.get(ballX, ballY) == (red)) putt ball...

if it checks and the map is black... the ball just rolls as it was shot.

So with this example. you dont need 2 maps... you can just show the bw one instead of the colored one. but your course would look a bit sad then. So you use one map to check and the other one to be shown...
Hope you understand the idea behind it.
if not feel free to keep asking...

Re: Mini Golf/ put putt?
Reply #5 - Mar 26th, 2009, 1:56pm
 
My other screen name is messed up. So I made a new one.

Thank you so much Cedric. Im doing this as part of a school project and my teacher hasn't been much help to me.

The two maps are working (showing only the color map) and Ive gotten as far as checking for the Balls position. This is where I get confused now:

//check the bw map at the ball position
if( bwMap.get(ballX, ballY) == 255) then ball should deflect off the wall...

if( bwMap.get(ballX, ballY) == (red)) putt ball...

if it checks and the map is black... the ball just rolls as it was shot.

[/quote]


Re: Mini Golf/ put putt?
Reply #6 - Mar 26th, 2009, 2:28pm
 
like i said, that was just pseudo code... wont work...
did you write a ball class or do you allready have any code where you can move your ball?
Re: Mini Golf/ put putt?
Reply #7 - Mar 26th, 2009, 2:43pm
 
Cedric wrote on Mar 26th, 2009, 2:28pm:
like i said, that was just pseudo code... wont work...
did you write a ball class or do you allready have any code where you can move your ball


I have a Ball class, but no I haven't figured out how to make my ball move yet.
I was hoping to do a click,drag, and release type movement.
Re: Mini Golf/ put putt?
Reply #8 - Mar 26th, 2009, 2:49pm
 
not sure if i have enough time to do that. and as it is a homework you maybe should come up with some solution on your own, but maybe i or some other people can help if you post what youve allready got
Re: Mini Golf/ put putt?
Reply #9 - Mar 26th, 2009, 3:03pm
 
I understand. I wouldn't expect you to do it. I've found the code I need here: http://www.skanaar.com/applets/billiards/ .

I've just spent 2 days trying to take the pieces I need and get them to fit my code; all to no avail. Ive pretty much started over:


Ball ball1;
Hole hole1;

void setup() {
 size(800, 800);
 smooth();
 PImage colorMap = loadImage("Level1.jpg");//level1 course jpg image.
 PImage bwMap = loadImage("Level1bw.jpg");//black and white map of the image.

 image(colorMap, 0, 0); //shows the color course on screen


 ball1 = new Ball(10,135,175);
 hole1 = new Hole(632,530,25);
}

void draw (){

 ball1.move();

 ball1.display();
 hole1.display();


 println(mouseX);
 println(mouseY);

}



_______________________



class Ball {

 //variables
 float r; // radius
 float x,y,xpos,ypos;
 float xspeed,yspeed;
 color c = color(255);


 // Constructor
 Ball(float tempR, float tempX, float tempY) {
   r = tempR;
   x = tempX;
   y = tempY;
   xspeed = 0;
   yspeed = 0;    
 }


 void move() {

   x += xspeed; // Increment x
   y += yspeed; // Increment y

   // Check horizontal edges
   if (x > width || x < 0) {
     xspeed *= - 1;
   }

   // Check vertical edges
   if (y > height || y < 0) {
     yspeed *= - 1;
   }
 }


 // Draw the ball
 void display() {
   {
     noStroke();
     fill(c);
     ellipse(x,y,r*2,r*2);

   }
 }
}

_____________________
class Hole {

 float r;  
 float x,y;
 color c2 = color(0,0,0);
 boolean displaying;

 Hole(float tempX,float tempY,float tempR) {

   r = tempR;
   x = tempX;
   y = tempY;
 }

 void display() {
   noStroke();
   fill(c2);    
   ellipse(x,y,r,r);
 }
}
Re: Mini Golf/ put putt?
Reply #10 - Mar 26th, 2009, 4:49pm
 
Allright, i thought a while about showing this code. cause its a real bad job. I just copy and pasted some stuff together from the example you showed me. there are alot of things you can remove and that dont need to be there and should be redone. it should just work as anothe example to show the idea of 2 maps... you see it kinda works... collision is pretty bad cause i just invert the direction. But you see, if you got it running its pretty easy to create more maps for it.

Btw, you should use gifs or pngs instead jpgs cause jpgs can change their color because of compression and the color check wont work then.

So like i said, take it as a bad example. maybe it helps you but you should try to rewrite it on your own.

http://dec32.de/public/level1.gif
http://dec32.de/public/level1bw.gif
Re: Mini Golf/ put putt?
Reply #11 - Mar 26th, 2009, 4:50pm
 
PImage colorMap ;
PImage bwMap;
boolean showball = true;
Table game;
//--------------
void setup(){
 size(800,800);
 smooth();
 game = new Table(1,1,799,799);
 game.startGame();
 colorMap = loadImage("level1.gif");//level1 course jpg image.
 bwMap = loadImage("level1bw.gif");//black and white map of the image.
}
//--------------
void draw(){
 background(32);
 image(colorMap, 0, 0); //shows the color course on screen
 game.update();
 game.visualize();
}
//--------------
void keyPressed(){
 println( game.kineticEnergy() );
}
//--------------
void mousePressed(){
 game.mousePressed(mouseX-game.x,mouseY-game.y);
}
//--------------
void mouseReleased(){
 game.mouseReleased(mouseX-game.x,mouseY-game.y);
}
//=============================
class Table{
 float drag = 0.985;
 float elasticity = 0.9;
 float wallElasticity = 1.1;
 float pushFactor = 0.05;
 float maxPush = 10;

 Ball[] balls;
 Ball selectedBall;
 int x,y,width,height;
 //--------------
 Table(int x, int y, int w, int h){
   this.x = x;
   this.y = y;
   width = w;
   height = h;
 }
 //--------------
 void startGame(){
   buildBalls(1);
 }
 //--------------
 void buildBalls(int count){
   balls = new Ball[count];
   for(int i=0;i<count;i++)
     balls[i] = new Ball( 120,730,1, this);
 }
 //--------------
 void update(){
   //simulation
   for(int i=0;i<balls.length;i++)
     balls[i].update();


 }

 //--------------
 void visualize(){

   translate(x,y);
   noStroke();


   //draw que
   stroke(255);
   if(mousePressed && selectedBall != null)
     line(selectedBall.x,selectedBall.y,mouseX-x,mouseY-y);

   //drawing
   for(int i=0;i<balls.length;i++)
     if(showball) balls[i].visualize();
 }
 //--------------
 float kineticEnergy(){
   float energy=0;
   for(int i=0;i<balls.length;i++)
     energy += mag( balls[i].vx, balls[i].vy );
   return energy;
 }
 //--------------
Re: Mini Golf/ put putt?
Reply #12 - Mar 26th, 2009, 4:50pm
 
void mousePressed(int mx, int my){
   for(int i=0;i<balls.length;i++)
     if( dist(balls[i].x,balls[i].y,mx,my) < balls[i].radius) {
       selectedBall = balls[i];
       break;
     }
 }
 //--------------
 void mouseReleased(int mx, int my){
   if(selectedBall != null){
     float px = (selectedBall.x-mx) * pushFactor;
     float py = (selectedBall.y-my) * pushFactor;
     float push = mag(px,py);
     if( push > maxPush ){
       px = maxPush*px/push;
       py = maxPush*py/push;
     }
     selectedBall.push(px,py);
   }
   selectedBall = null;
 }
}

class Ball{
 float x,y,vx,vy,radius,diameter,prevy,prevx;
 int type;
 Table table;
 color ballColor = color(255);
 //--------------
 Ball(float x, float y, int type, Table t){
   this.x = x;
   this.y = y;
   this.type = type;
   diameter = 12;
   radius = diameter/2;
   table = t;

 }
 //--------------
 void update(){
   vx *= table.drag;
   vy *= table.drag;

   x += vx;
   y += vy;

   wallBounce();
   putt();

 }
 //--------------
 void wallBounce(){

   if(bwMap.get(int(x),int(y))== color(255)){

     vx = -table.wallElasticity*vx;
     vy = -table.wallElasticity*vy;
   }

 }

 void putt(){

   if(bwMap.get(int(x),int(y))== color(255,0,0)){

     x=120;
     y=730;
     vx=0;
     vy=0;
   }

 }
 //--------------
 void visualize(){
   fill(ballColor);

   noStroke();
   ellipse(x,y,diameter,diameter);
 }
 //--------------
 void push(float dx, float dy){
   vx += dx;
   vy += dy;
 }
 //--------------

}
Re: Mini Golf/ put putt?
Reply #13 - Mar 26th, 2009, 6:07pm
 
Thank you so much. Ill keep busy trying to figure out what doesnt belong and then redo it.
Re: Mini Golf/ put putt?
Reply #14 - Mar 26th, 2009, 6:15pm
 
for example. i just created 1 ball, but there is no need for an array at all as you only need 1 ball, so you can delete things like

 for(int i=0;i<count;i++) ,    for(int i=0;i<balls.length;i++) etc...

Page Index Toggle Pages: 1