Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Getting a bit of a spin out of Java

  • 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