Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Getting a bit of a spin out of Java

Options
  • 24-07-2006 11:57am
    #1
    Closed Accounts Posts: 28


    Hi all,
    I decided this year that instead of wasting my summer watching tv and such sorts that I'd do something constructive.
    I decided to look into Java MIDlets (i.e. mobile phone java games), and I started into a nice little PONG game.

    Its worked out pretty well but I'm just having dificulty figuring out how to get some spin on the ball. Well not really spin but the illusion of spin....

    For example, if the ball is moving downwards towards your paddle/racket and your moving downwards to get to it, and your still moving downwards when it hits you the the balls Y speed should increase. And in the same situation and you were moving upwards the Y speed would decrease.
    And vica versa.

    Now I was using some booleans to try and get that going but its proving a little elusive.

    Heres the code (for those of you who have never looked at midlets they're quite like applets, I've left out anything that you won't need, just everytime the game moves on a step advance() is called...)
    
        /**
         * This method is called everytime the game moves a step.
         * @returns myScores Returns the array of the scores.
         */
        public int[] advance() {
    	if(myMoving) {
    	    checkWalls();
    	    moveEnemy();
    	}
    	return myScores;
        }
    
        /**
         * Checks of the Ball hits a wall or not.
         * If so, it reverses the ball's Y direction and moves the ball on a step.
         * Otherwise it checks if the ball is hitting a Racket.
         */
        public void checkWalls() {
    	if((myBallY + myBallSpeedY < myBorderTop) || ((myBallY + myBallDiameter + myBallSpeedY) > myBorderBottom)) {
    	    myBallSpeedY *= -1;
    	    moveBall();
    	}
    	else {
    	    checkRackets(); 
    	}
        }
        
        /**
         * Checks of the Ball hits a Racket or not.
         * If so, it reverses the ball's X direction, increases it X Speed and moves the ball on a step.
         * Y speed should increase or decrease depending if the racket is moving, i.e. spin
         * Otherwise it checks if the ball is in a Goal.
         */
        public void checkRackets() {
    	if(myBallX + myBallDiameter + myBallSpeedX > myRacketX0) {
    	    if(myBallY < myRacketY0 + myRacketY1 && myBallY > myRacketY0) {
    		if(myBallSpeedX > 0) myBallSpeedX++;
    		if(myBallSpeedX < 0) myBallSpeedX--;
    		if(myIsGoingUp) myBallSpeedY -= 2;
    		if(myIsGoingDown) myBallSpeedY += 2;
    		myBallSpeedX *= -1;
    		moveBall();
    	    }
    	    else {
    		myIsGoingUp = false;
    		myIsGoingDown = false;
    		checkGoals();
    	    }
    	}
    	
    	else if(myBallX + myBallSpeedX < myEnemyRacketX1) {
    	    if(myBallY < myEnemyRacketY0 + myEnemyRacketY1 && myBallY > myEnemyRacketY0) {
    		if(myBallSpeedX > 0) myBallSpeedX++;
    		if(myBallSpeedX < 0) myBallSpeedX--;
    		if(myEnemyIsGoingUp) myBallSpeedY -= 2;
    		if(myEnemyIsGoingDown) myBallSpeedY += 2;
    		myBallSpeedX *= -1;
    		moveBall();
    
    	    }
    	    else {
    		myEnemyIsGoingUp = false;
    		myEnemyIsGoingDown = false;
    		checkGoals();
    	    }
    	}
    	else {
    	    myEnemyIsGoingUp = false;
    	    myEnemyIsGoingDown = false;
    	    checkGoals();
    	}
        }
        
        /**
         * Checks of the Ball hits a goal or not.
         * If so, it increases the relevant score and resets the ball.
         * Otherwise it moves the ball on a step.
         */
        public void checkGoals() {
    	if(myBallX <= myBorderLeft) {
    	    myScores[0]++;
    	    reset();
    	}
    	if((myBallX + myBallDiameter) >= myBorderRight) {
    	    myScores[1]++;
    	    reset();
    	}
    	else {
    	    moveBall();
    	}
        }
    
        /**
         * Creates the illusion  of a moving ball.
         */
        public void moveBall() {
    	myBallX += myBallSpeedX;
    	myBallY += myBallSpeedY;
    
    	System.out.println("My Ball is going at slope: ("+myBallSpeedX+", "+myBallSpeedY+") !!");
        }
    
    

    Any advice would be greatly appreciated,

    Regards

    esmurph


Advertisement