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 › Problem moving objects
Page Index Toggle Pages: 1
Problem moving objects (Read 865 times)
Problem moving objects
Mar 29th, 2006, 2:19pm
 
I am moving objects using the syntax

Code:

b[15].setYPos(b[15].getYPos() + b[15].getSpeedY() * b[15].getDirectionY());
b[15].setXPos(b[15].getXPos() + b[15].getSpeedX() * b[15].getDirectionX());


for some reason the object moves on the x axis but not the y?

the x and y values are identical throughout the whole program so far
YDirection and X Direction are -1
the GetSpeed() methods are initially set to 0 but increment when the spacebar is pressed

any ideas why this object keeps sticking to the X axis?
Re: Problem moving objects
Reply #1 - Mar 29th, 2006, 2:33pm
 
I can't spot anything obviously wrong with that.

You don't have to give away all your secrets(!), but a little more code might help us see what's going wrong.

For instance, what kind of object is the b array? What do the get/set XPos/SpeedX/DirectionX YPos/SpeedY/DirectionY functions look like?  Are you sure there aren't any x's and y's mixed up in there?

The general advice here is that you'll get more help if you post enough code so that other people can replicate the problem.  
Re: Problem moving objects
Reply #2 - Mar 29th, 2006, 9:04pm
 
I cant post all my code as it is very lengthy (its a pool game), but here is the ball (the object) class

Code:

class Ball
{
private float xCoord = 0;
private float yCoord = 0;
private float directionX = -1.0;
private float directionY = -1.0;
private int ballColour;
private float speed;
private float radius = 15;
private boolean potted = false;
private boolean black = false;
private boolean white = false;

Ball(float xPos, float yPos, int colour)
{
ballColour = colour;
xCoord = xPos;
yCoord = yPos;
}

float getXPos ()
{
return xCoord;
}

void setXPos (float xCoord)
{
this.xCoord = xCoord;
}

float getYPos ()
{
return yCoord;
}

void setYPos (float xCoord)
{
this.yCoord = yCoord;
}

float getSpeed()
{
return speed;
}

void setSpeed (float speed)
{
this.speed = speed;
}

float getDirectionY()
{
return directionY;
}

void setDirectionY(float directionY)
{
this.directionY = directionY;
}

float getDirectionX()
{
return directionX;
}

void setDirectionX(float directionX)
{
this.directionX = directionX;
}

float getRadius()
{
return radius;
}

void draw()
{
pushMatrix();

noStroke();

ellipse (xCoord, yCoord, radius * 2, radius * 2);

if (!potted)
{
if ( (yCoord + directionY + radius > 165) || (yCoord + directionY - radius < -125 ) )
{
directionY *= -1;
}
if ( (xCoord + directionX + radius > 300) || (xCoord + directionX - radius < -300 ) )
{
directionX *= -1;
}
}
//bottom left pocket
if (xCoord > -300 && xCoord < -280 && yCoord < 165 && yCoord > 145)
{
if ((!black) && (!white))
{
potted = true;
pottedNumber = pottedNumber + 1;
pottedBall(pottedNumber);
}
if (black)
{
potted = true;
pottedBall (15);
}
if (white)
{
pottedBall (16);
}
speed = 0;
}
//top left pocket
if (xCoord > -300 && xCoord < -280 && yCoord < -105 && yCoord > -125)
{
if ((!black) && (!white))
{
potted = true;
pottedNumber = pottedNumber + 1;
pottedBall(pottedNumber);
}
if (black)
{
pottedBall (15);
}
if (white)
{
potted = true;
pottedBall (16);
}
speed = 0;
}
//bottom right pocket
if (xCoord < 300 && xCoord > 280 && yCoord < 165 && yCoord > 145)
{
if ((!black) && (!white))
{
potted = true;
pottedNumber = pottedNumber + 1;
pottedBall(pottedNumber);
}
if (black)
{
potted = true;
pottedBall (15);
}
if (white)
{
pottedBall (16);
}
speed = 0;
}
//bottom middle pocket
if (xCoord > -20 && xCoord < 20 && yCoord < 165 && yCoord > 145)
{
if ((!black) && (!white))
{
potted = true;
pottedNumber = pottedNumber + 1;
pottedBall(pottedNumber);
}
if (black)
{
potted = true;
pottedBall (15);
}
if (white)
{
pottedBall (16);
}
speed = 0;
}
//top middle pocket
if (xCoord > -20 && xCoord < 20 && yCoord > -125 && yCoord < -105)
{
if ((!black) && (!white))
{
potted = true;
pottedNumber = pottedNumber + 1;
pottedBall(pottedNumber);
}
if (black)
{
potted = true;
pottedBall (15);
}
if (white)
{
pottedBall (16);
}
speed = 0;
}
//top right
if (xCoord < 300 && xCoord > 280 && yCoord > -125 && yCoord < -105)
{
if ((!black) && (!white))
{
potted = true;
pottedNumber = pottedNumber + 1;
pottedBall(pottedNumber);
}
if (black)
{
potted = true;
pottedBall (15);
}
if (white)
{
pottedBall (16);
}
speed = 0;
}

if (!potted)
{
if (xCoord < -300 + radius)
{
xCoord = radius;
}
if (xCoord > 300 - radius)
{
xCoord = 300 - radius;
}
if (yCoord < -125 + radius)
{
yCoord = -125 + radius;
}
if (yCoord > 165 - radius)
{
yCoord = 165 - radius;
}
}
popMatrix();
}
}


If there any bits of code you need, let me know
Re: Problem moving objects
Reply #3 - Mar 29th, 2006, 9:10pm
 
Also, heres the method that should move the ball one of 8 directions (i cant think of a way to move it any other way) when it gets hit on a certain side (please be aware that 0 angle is at the bottom of the circle and then works its way around anti-clockwise, but no matter what direction the cue is, the ball always goes along the x axis
Code:

void takeShot(int cPower)
{
 if (angle >= 331 || angle <= 25)
 {
   b[15].setDirectionX(0);
   b[15].setDirectionY(-1);
 }
 else if (angle >= 26 && angle <= 65)
 {
   b[15].setDirectionX(1);
   b[15].setDirectionY(1);
 }
 else if (angle >= 66 && angle <= 115)
 {
   b[15].setDirectionX(-1);
   b[15].setDirectionY(0);
 }
 else if (angle >= 116 && angle <= 155)
 {
   b[15].setDirectionX(1);
   b[15].setDirectionY(-1);
 }
 else if (angle >= 156 && angle <= 205)
 {
   b[15].setDirectionX(0);
   b[15].setDirectionY(1);
 }
 else if (angle >= 206 && angle <= 245)
 {
   b[15].setDirectionX(-1);
   b[15].setDirectionY(1);
 }
 if (angle >= 246 && angle <= 295)
 {
   b[15].setDirectionX(1);
   b[15].setDirectionY(0);
 }
 if (angle >= 296 && angle <= 330)
 {
   b[15].setDirectionX(0);
   b[15].setDirectionY(-1);
 }

 b[15].setSpeed(b[15].getSpeed() + cPower);
 if (myGame.getTurn() == 1)
 {
   p1.setShotTimeLeft(0);
   p2.setShotTimeLeft(120);
   myGame.setTurn(2);
 }
 else if (myGame.getTurn() == 2)
 {
   p1.setShotTimeLeft(120);
   p2.setShotTimeLeft(0);
   myGame.setTurn(1);
 }
 shot = true;
}
Re: Problem moving objects
Reply #4 - Mar 30th, 2006, 1:58am
 
adzer2 wrote on Mar 29th, 2006, 9:04pm:
Code:

void setYPos (float xCoord)
{
this.yCoord = yCoord;
}




* TomC squints at xCoord

Should say yCoord for the argument name, no?
Re: Problem moving objects
Reply #5 - Mar 30th, 2006, 2:23am
 
Oh my god, I cant believe I missed that one, thanks a million Tom, too many late nights working on this I think, lol

Thanks alot
Page Index Toggle Pages: 1