CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2010
    Posts
    73

    Unhappy Need help regarding game logic

    Hi to all,
    Hope you all will be fine. I am making a cricket game and now i am stuck at a point when i am trying to do a ball. Actually what happen , that when ball strikes with the ground(floor) it continues to bounce on the same point. I want that it creates a effect that after bounce the ball goes to the batsman. I define

    Code:
    getHeight - 130
    bouncing point. Here is my code to implement bounce. Please help.

    Code:
    public class DemoGameCanvas extends GameCanvas implements Runnable {
    
         //360 degrees = 2 * pi radians
        // 360 / 6.28 = 1 radian
        // 57. 3 degress = 1 radian
    
        private static final int FP_PI2 = MathFP.mul(MathFP.PI, MathFP.toFP(2));  // pi * 2 = 6.2832
        private static final int FP_DEGREES_PER_RADIAN = MathFP.div(MathFP.toFP(360), FP_PI2); // 360 / 2*pi = 1 radian
    
        private static int SPRITE_IMAGES_WIDTH = 67;
        private static int SPRITE_IMAGES_HEIGHT = 137;
    
        private static int BALL_SPRITE_IMAGE_WIDTH = 5;
        private static int BALL_SPRITE_IMAGE_HEIGHT = 5;
    
        private int ballXFP = MathFP.toFP(140);     //  x position (as a MathFP)
        private int ballYFP = MathFP.toFP(100);     //  y position (as a MathFP)
        private int direction = 270  ;              //  current direction in degrees 
        private int xVelFP;                         //  x velocity (as a MathFP)
        private int yVelFP;                         //  y velocity (as a MathFP)
        private int maxVelFP = MathFP.toFP("2");
        private int thrustFP = MathFP.toFP("0.1");    // decreasing this value will cause greater bounce
        
        private int ballOrigXFP;
        private int ballOrigYFP;
    
        public DemoGameCanvas() {
            super(true);
            try {
                //setFullScreenMode(true);
                init();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } 
        } //DemoGameCanvas()
    
        /**
         * Initialize the Game Design, then load all layers and start animation threads.
         */
        private void init() throws IOException {
    
        //Background image
            cricketGround = gameDesign.getCrik();
    
            //For all sprites positions
            lm = new LayerManager();
            gameDesign.updateLayerManagerForMatchGround(lm);
    
            ballImage = gameDesign.getBall5px();
            ballSprite = gameDesign.getWhiteBall();
            ballSprite.defineReferencePixel(3, 3);
            .......
        
        } //init()
    
        public void run() {
    
            Graphics g = getGraphics();
    
            while (!interrupted) {
    
                  updateScreen(g);
            }
    
            try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException ie) {
                    ie.printStackTrace();
                }
    
         
        } //run() 
    
        private void updateScreen(Graphics g) {
    
            createBackGround(g);
    
            moveBall();
    
            ballSprite.setRefPixelPosition(MathFP.toInt(ballXFP), MathFP.toInt(ballYFP));
          
            playShots();
        }
    
        private void createBackGround(Graphics g) {
    
            lm.paint(g, 0, 0);
            flushGraphics(0, 0, getWidth(), getHeight());
        }
    
        private void moveBall() {
    
              // Wrap the direction around if it's now invalid.
                if (direction < 0) {
                    direction = 359;
                }
    
                if (direction > 359) {
                    direction = 0;
                }
    
                //save the original position (so if it collided we can back out of this movement)
                 ballOrigXFP = ballXFP;
                 ballOrigYFP = ballYFP;
    
    
              /******
               * Converting degrees into radian
               * 360 degrees = 2*pi radians
               * 360 degrees = 6.28 radians
               * 360 / 6.28 degrees = 1 radian
               * 57.3 degrees = 1 radian
               * 1 degree = 1 / 57.3 radians
               * 45 degrees = 45 / 57.3 radians
               */
    
              //move the ball according to its current direction (radians)
              // if direction is 270 then dirRadian = 4.7124
              int dirRadians = MathFP.div(MathFP.toFP(direction), FP_DEGREES_PER_RADIAN); //direction / 57.3
            
              //Calculate the accelaration for this cycle by multiplying the direction
              //by the thrust on both x and y
              int xAccFP = MathFP.mul(thrustFP, MathFP.cos(dirRadians));    // Vox = Vo * cos(thete)
              int yAccFP = MathFP.mul(thrustFP, -MathFP.sin(dirRadians));   // Voy = Vo * sin(theta)
    
              //increase the velocity by the amount of accelaration in this cycle
              xVelFP = MathFP.add(xVelFP, xAccFP);
              yVelFP = MathFP.add(yVelFP, yAccFP);
    
              //cap our velocity to a controllable level. Capping both positive and negative velocity
              //Without this, the ball would soon be moving at breakneck speed
              if (xVelFP > maxVelFP) {           //+ve x velocity
                  xVelFP = maxVelFP;
              }else if (xVelFP < -maxVelFP) {    //-ve x velocity
                  xVelFP = -maxVelFP;
              }
    
              if (yVelFP > maxVelFP) {
                  yVelFP = maxVelFP;
              }else if (yVelFP < -maxVelFP) {
                  yVelFP = -maxVelFP;
              }
    
    
    //          //Move ths ship according to its current velocity
                ballXFP = MathFP.add(ballXFP, xVelFP);
               ballYFP = MathFP.add(ballYFP, yVelFP);
    
    
    //          //add the xcomponent of the movement (cos radian)
    //         ballXFP = MathFP.add(ballXFP, MathFP.cos(dirRadians));
    //
    //          //add the y component of the movement (negative sin radian). We use negative
    //          //sign to convert from cartesian to screen co-oedinate
    //          ballYFP = MathFP.add(ballYFP, -MathFP.sin(dirRadians));
    
              //check our position and if we're hitting the floor reverse a velocity component
              if (MathFP.toInt(ballXFP) + BALL_SPRITE_IMAGE_WIDTH >= getWidth()) {
    
                  //back out of the collision position
                  ballXFP = ballOrigYFP;
                  xVelFP = MathFP.mul(xVelFP, MathFP.toFP("-1.0"));
               
              }
    
              if (MathFP.toInt(ballYFP) + BALL_SPRITE_IMAGE_HEIGHT >= getHeight() - 130) {
    
                  //back out of the collision positin
                  ballYFP = ballOrigYFP;               
                  yVelFP = MathFP.mul(yVelFP, MathFP.toFP("-1.0"));
                 
              }
    
              // Check our position and wrap around to the other side of the canvas
              // if we have to.
              if (MathFP.toInt(ballXFP) < viewPortX) {
                  ballXFP = MathFP.toFP(getWidth()-1);
              }
    
              if (MathFP.toInt(ballXFP) > getWidth()) {
                  ballXFP = MathFP.toFP(viewPortX);
              }
    
              if (MathFP.toInt(ballYFP) < viewPortY) {
                  ballYFP = MathFP.toFP(getHeight() - 1);
              }
    
              if (MathFP.toInt(ballYFP) > getHeight()) {
                  ballYFP = MathFP.toFP(viewPortY);
              }
    
    
       }  moveBall()
    
    } // class DemoGameCanvas
    please help how can i implement the logic that after bounce the ball goes to batsman instead of bouncing at the same point.

    Thanks

  2. #2
    Join Date
    Jan 2011
    Location
    Tacoma, Washington
    Posts
    31

    Re: Need help regarding game logic

    Seems to me that you have the direction set to 270 degrees. When it hits you do some calculations with radians that points the ball straight down again.

    270 / FP_DEGREES_PER_RADIAN = 270 / 360 / 2*pi = 3/2pi = 270 degrees.

    therefore dirRadians = 270.

    Then you add this value to the x and y coordinate to ballXFP and ballYXP

    then ballXFP = cos(270) = 0 + ballXFP

    So this adds 0 to the current x coordinate.

    The direction remains 270 and a subsequent call to move ball will presumably do the same thing.

    Assuming that the x value is your horizontal axis then the ball does not move left or right for all future calls.

    That is my best guess. I hope my math and logic is correct. You've got a lot going on and I might have missed/over-simplified something. Plus I haven't done Trig in a while! =O

    Hope I helped. Good luck.

  3. #3
    Join Date
    Jul 2010
    Posts
    73

    Re: Need help regarding game logic

    Hi thanks,
    My friend after hitting the floor, i want ,the ball move in the same direction i.e. 270 but with a bouncing effect. Suppose i set my ball coordinates to (40, 100) and direction to 315.

    Code:
        private int ballXFP = MathFP.toFP(40);     //  x position (as a MathFP)
        private int ballYFP = MathFP.toFP(100);     //  y position (as a MathFP)
        private int direction = 315 ;              //  current direction in degrees
    Now when i run the code the ball goes in 315 direction and after hitting(collision) with floor it goes forward with a bouncing effect. OK here i am increasing both x and y. But in case of 270 direction(degree) . I am increasing only y right now. And i want that after hitting the floor the ball bounce and continue to move in 270 direction. Or you can say i want to achieve the far , near effect. The ball starts from 100(seems ball is coming from far distance ) coming, coming, coming and after after collision with the floor, the ball bounce and now ball seems near to you.
    In (40, 100) and 315 direction case bowler running horizontally, and when he balls , the ball collides with the floor and goes to the batsman.
    But in (140, 100) and 270 direction case, the bowler running vertically and now when the bowler balls the ball collides with the ground and continue to bounce there. It does not go to the batsman.
    I want to ask is this something related to z axis? (creating far and near effect) Is it possible to use z axis in 2D game ?

    go to
    and then play the ASHES Dominator or ASHES Fan Challenges. And then see how the bowler bowl in these games. I want to do the same thing in my game. The bowler bowl vertically and it comes directly or after bounce to batsman. Now i think it is more clearer to you that what i want to do.
    Thanks
    Last edited by Basit781; January 16th, 2011 at 03:45 AM.

  4. #4
    Join Date
    Jan 2011
    Location
    Tacoma, Washington
    Posts
    31

    Re: Need help regarding game logic

    I would think that if you wanted a z-axis in a 2D game you would need to change the size of the components to give the appearance of an item coming toward you or going away. I figure that would have to do with the size of the ball object as the panel is painting it.

  5. #5
    Join Date
    Jan 2011
    Location
    Tacoma, Washington
    Posts
    31

    Re: Need help regarding game logic

    So I played some cricket on the site that you provided. I know nothing about the game and it was funny to try to figure out what I was supposed to do. Hit the ball I guess. =D Anyway the game I played was 2D and therefore has no z-axis. The ball moves toward the batter (y-axis) and to the left and right (x-axis). So the only z-axis action in the game I played had to do with the ball changing size with the downward movement of the y-axis.

    I think I had an image of your game being with the "bowler" on one side and the "batman" on the other with a field in the middle. The bowler bowls toward the batter and the ball isn't changing size as it moves laterally. So maybe I was taking the wrong approach when analyzing it.

    Anyway, that is a long winded way to say that I don't think that any of your previous errors with the ball stalling out has anything to do with a "z-axis" motion.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured