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
using constrain (Read 961 times)
using constrain
Dec 28th, 2009, 2:28pm
 
I'm working on a game and I was wondering how I would have my character (a panda) land on a platform that I load in as an image, as opposed to going through the image as though it were just the background, and not a separate object. I was wondering if I could do this using "constrain"? I'm not sure how I should calculate when the panda is on the platform, but when it is on the platform i can just constrain its position to be stuck to it, correct? Using the y value of the platform as it's constraint. I have this as my code for the panda class:

class Panda{
 float x = 50;
 float y = 250;
 float w;
 float h;
 float velocityX = 0.0;
 float velocityY = 0.0;
 float acceleration = 0.0;
 PImage Panda;
 
 Panda (String pandain){
   panda = loadImage (pandain);
   w = panda.width / 4;
   h = panda.height / 4;
 }
 void restart(){
   x = 50;
   y = 250;
   velocityX = 0.0;
   velocityY = 0.0;
   acceleration = 0.0;
 }
 void show(){
   image (panda, x, y, w, h);
 }
 void move(){
   if ((keyPressed == true)&&(key == ' ')&&(y > 100)){
     velocityY = -3.5;
     acceleration = 1;
   }

   x = x + velocityX;
   x = constrain (x, 0, 598);
   
   y = y + velocityY;
   y = constrain (y, 0, 250);
   
   velocityY = velocityY + acceleration;
 }
}


but how do I go about constraining it while it's on a platform of some sort?
Re: using constrain
Reply #1 - Dec 28th, 2009, 4:17pm
 
Didn't you already ask this?  What was wrong with the suggested collision detection routine?
Re: using constrain
Reply #2 - Dec 28th, 2009, 4:34pm
 
I was able to implement the collision detection for gathering the items, which has worked out just fine (thanks, by the way). But for this would I use collision detection for between the platform and the panda then use an if statement to say if it makes contact with the platform than the panda is constrained to certain y values? Like:

constrain(panda, 0, platform.y);

would something along the lines of that work? I guess I just wanted a direct example, sorry.
Re: using constrain
Reply #3 - Dec 29th, 2009, 1:00am
 
I don't think the answer is as simple as just using constrain: you'd first need to determine if the panda is above or below the platform.  To achieve that you'd need a secondary test within the collision detection.  See the previously linked example for an implementation applied to a ball...  Once you've determined if the panda is above or below the platform I think you could use constrain: so if it's above the platform minimum would be platform.y and maximum would be panda.y; if it's below the platform minimum would be panda.y (or perhaps ground level) and maximum would be platform.y (Note: you'd also need to factor in the height of the platform).

BTW - this is a continuation of your previous question so you would have done better to post this as a follow-up on that thread, so your 'new' question is in context of answers previously given...  Anyway - why not have a go at implementing this - I get the impression you should be able to manage it Wink - and if you get stuck post your code for us to look at.
Re: using constrain
Reply #4 - Dec 29th, 2009, 10:02am
 
The code I made for the Platform is:

class Platform{
 float x;
 float y;
 float w;
 float h;
 float speed;
 PImage thisPlatform;
 boolean collect = false;
 
 Platform (float inx, float iny, float inspeed, String platform){
   x = inx;
   y = iny;
   speed = inspeed;
   thisPlatform = loadImage(platform);
   w = thisPlatform.width;
   h = thisPlatform.height;
 }
 void show(){
   image (thisPlatform, x, y);
 }
 void move(){
   x = x-speed;
   if (x < -w){
     x = width;
     speed = speed + 0.03;
   }
 }
 void restart (float inx, float iny){
   x = inx;
   y = iny;
   image(thisPlatform, x, y);
 }
 void gather(){
   if (collect == false){ //where p1 is panda and pf is platform
     if ((p1.x + p1.w) > pf.x && p1.x < (pf.x + pf.w) && (p1.y + p1.h) > pf.y && p1.y < (pf.y + pf.h)){ //land on platform
     collect = true;
     panda = constrain(panda, 0, pf.y);
     }
   }
 }
}


So this will calculate if the panda has made contact with the platform, but would it take into consideration if the panda is above the platform, or I think this would just constrain it if the panda touches it, right? I saw the post about the ball stuck to the pad; would I be able to use something similar to that except replace the rectangle dimensions with the platform's, and have the panda replace the ball? =/
Re: using constrain
Reply #5 - Dec 29th, 2009, 11:13am
 
Untested, but you should get the general idea...

Code:
//where p1 is panda and pf is platform 
if ((p1.x + p1.w) > pf.x && p1.x < (pf.x + pf.w) && (p1.y + p1.h) > pf.y && p1.y < (pf.y + pf.h)){
 // panda is above platform
 if(p1.y < pf.y) {
   panda = constrain(panda, pf.y, p1.y);
 }
 // panda is below platform
 else if(p1.y > pf.y) {
   panda = constrain(panda, 0, pf.y);
 }
    collect = true;    
}
Re: using constrain
Reply #6 - Dec 29th, 2009, 11:29am
 
Well, i've yet to test that, but i think i get the idea of how to check for the collision.. i'll just play around with that for a bit and see how it turns out.
Thanks for all the help, I appreciate it! xD;
Page Index Toggle Pages: 1