Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: logic quition on 2d game direction(airhockey)

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default logic quition on 2d game direction(airhockey)

    i am trying to create 2d game. i dont know what is the name of this game but i think its airhockey(paddle are vertical rect). there are two paddles and one ball. and you have to hit the ball. if u dont hit the ball and it goes behind your padder than other person get a score. you can move your paddle only up or down.

    // ex of a left paddle, right paddle and b = ball.
    ____________________________
    |   _                   _            |
    |  | |                 | |            |        
    |  | |         b       | |           |
    |  | |                 | |            |
    |  | |                 | |            |
    |  | |                 | |            |
    |   -                  _              |
    -------------------------------------



    dx = 5;  //ball x postion speed
    dy = 5;  //ball y postion speed
     
    //collision detection
    //if ball hit the paddler
    if(...)
    {
      if(..)
       {
          //change direction of ball
          dx = -dx;
          dy = -dy;
       }
    }


    code above just change ball direction if it hit the paddle.
    so if ball hit paddle on right than ball will go left.

    now the problem with this code is that ball will goes the same place. so if you do not move the both paddles than ball will keep gooing left and right in same patterns. i am not sure how games online like this make the pattern different.

    i was thinking i can check where did the ball hit first than change dy or dx. so..


    //if ball hit top of paddle
    if(...)
    {
       dx = -dx;
       dy = -dy+1;  //so ball direction will go lil up
    }
     
    //if ball hit middle of paddle
    if(...)
    {
       dx = -dx;  //just change direction
       dy = -dy;
    }
     
     
    //if ball hit bottom of paddle
    if(...)
    {
       dx = -dx;
       dy = -dy-1;   //ball direction will go lil down
    }

    i am not sure if this is the best way or if this is how people create this kind of game.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: logic quition on 2d game direction(airhockey)

    This sounds a lot like pong to me.

    You could start your dx and dy off as random numbers, or you could speed them up over time. Or you could base them off of where they hit the paddle (hitting the top of the paddle bounces the ball up, hitting the bottom bounces the ball down, in the middle bounces is straight, something like that). It's really up to you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    hwoarang69 (March 19th, 2013)

  4. #3
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: logic quition on 2d game direction(airhockey)

    thanks

Similar Threads

  1. Logic Error in noughts and crosses game
    By eyesackery in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 15th, 2012, 07:40 AM
  2. [SOLVED] Need help with motion direction
    By Sayco in forum Java Theory & Questions
    Replies: 9
    Last Post: March 6th, 2012, 10:15 AM
  3. need a bump in the right direction
    By mszyndlar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 15th, 2011, 01:38 AM
  4. [SOLVED] Someone point me to the right direction..
    By ineedhelp in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: June 30th, 2011, 10:03 PM
  5. Direction,
    By Time in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 21st, 2010, 05:21 PM