Results 1 to 36 of 36

Thread: Pong game/Angles questions

  1. #1

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755

    Arrow Pong game/Angles questions

    hi!
    im making a pong game like dxball (http://www.longbowdigitalarts.com)...

    When i shoot the ball i want it to move in a specific angle...
    how do i do that?

    i've tried this code:
    VB Code:
    1. Ball(i).x = Ball(i).x + Sin(Ball(i).Angle) * 5
    2.         Ball(i).y = Ball(i).y + Cos(Ball(i).Angle) * 5

    but it doesnt move where it should all the time....
    whats wrong?
    Last edited by cyborg; Dec 11th, 2002 at 10:14 AM.
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  2. #2

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    If i get some more questions about this pong game, i will post them in this thread!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  3. #3
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    well on the first look the code seems right.
    but if you declared the x and the y variable as integers you will probably get a bad movement... try declaring them as singles or scale your numbersystem up (dunno some people like not to use float values )
    Sanity is a full time job

    Puh das war harter Stoff!

  4. #4

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    this solved the problem:

    VB Code:
    1. Ball(i).X = Ball(i).X + Sin(Ball(i).Angle * (Pi / 180)) * Ball(i).Speed
    2.     Ball(i).Y = Ball(i).Y + Cos(Ball(i).Angle * (Pi / 180)) * Ball(i).Speed
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  5. #5

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Here's a beta version of the game.....the gfx are stole from DXball, but im gonna make my own later...
    Attached Files Attached Files
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  6. #6

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Ok...I've got some errors that i hope anyone of you can find...

    I've added border loading, but the collition detection is a bit wrong...

    1. Sometimes the ball gets stuck and moves along one of the sides
    2. Sometimes (maybe all the time (not sure)) when the ball bounces on the roof, the new angle is wrong!
    3. When the ball moves through the bricks, it sometimes moves between two bricks...

    The zip contains the source and the compiled program with 2 test borders....
    Attached Files Attached Files
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  7. #7
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    1. Sometimes the ball gets stuck and moves along one of the sides
    I can't see what you are meaning by this...But it is wery easy to get the ball to move from left to right and back again without moving very much in Y direction. And this way it will hit all bricks...

    2. Sometimes (maybe all the time (not sure)) when the ball bounces on the roof, the new angle is wrong!
    I can't see anything wrong with the angle when it hits the roof...

    3. When the ball moves through the bricks, it sometimes moves between two bricks...
    Yes you need bether collision detection. I havn't looked at you code, but I'm guessing that you are only checking the left property of the ball. You would have to add the width to and check for the whole ball...

    I'm so tired no so I won't try to give you a solution now....but I will see what I can do tomorow...

    Good Night Cyborg...

  8. #8
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    BTW I forgot to tell you, it's a nice game, and the ball moves smooth...

  9. #9

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Thanks!

    Sometimes when the ball hits one of the sides, it doesnt bounce, it just follows the side in the same speed as before!

    The collition detection checks the whole ball!

    If i set the startangle on the ball to 180 it should move straight up, but it moves a bit to the right....

    If i get the ball to bounce straight up, it should move straight down after hitting the roof, but it moves a bit to the left...
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  10. #10

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Originally posted by NoteMe
    I can't see what you are meaning by this...But it is wery easy to get the ball to move from left to right and back again without moving very much in Y direction. And this way it will hit all bricks...
    Fixed...
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  11. #11

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    If you care....
    here's a new and improved version!
    Attached Files Attached Files
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  12. #12
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by cyborg
    If you care....
    here's a new and improved version!
    I never stop caring about your games . This game is turning out great. But I'm not sure if it is only my machine or if it is because I have a lot of apps running in the background, but I don't think the ball moves that smooth anymore, and the it seems like the speed is increasing after the ball has hit the "roof" and some bricks (or is this just something I imagine my self??)...

    Nice game..

  13. #13

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    the speed is allways incresing untill it reaches a certain value...
    the new angle gets wrong after hitting the roof and the bricks...

    maybe it doesnt move that smooth anymore, but that can be fixed...
    the colition detection that i've made it checking all pixels on the ball every 10th millisecond....
    i think that code can be improved...i'll look into that in a few days!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  14. #14
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by cyborg
    the speed is allways incresing untill it reaches a certain value...
    the new angle gets wrong after hitting the roof and the bricks...

    maybe it doesnt move that smooth anymore, but that can be fixed...
    the colition detection that i've made it checking all pixels on the ball every 10th millisecond....
    i think that code can be improved...i'll look into that in a few days!
    Looking forward to it...you are starting to be good at game programming...

  15. #15

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    thanks very much!
    i just love to make games
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  16. #16
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Hmm

    I haven't looked at your code, but when you said about your collision detection.. Ok.. so you have a ball and a brick... the collision detect should use angles and distance.. ..

    What you need is to get the distance between the ball and al bricks. Any bricks with a distance below the balls speed (add a bit of leway.. just in case) are checked for collision.

    To check for collision:
    Get the angle to the 4 corners of a brick
    If the ball is going to hit the brick, it's movement angle will be within those angles (Don't to compensate for ball size!!)..so..

    Let's assume that A(1) = 45 (top left), A(2) = 40 (bottom left), a(3) = 35 (top right) and A(4) = 30 (bottom right)
    *its up to you to get angles


    First, you find the lowest number...

    Low = 360
    for b = 1 to 4
    if a(b) < low then low = a(b)
    next b

    then the highest..

    High = 0
    for b = 1 to 4
    if a(b) > High then High = a(b)
    next b

    and near finally:
    If Low <= High *(this is explained below)* then
    if BallAngle > Low and BallAngle < High then msgbox " BOOM "

    ELSE

    *Because 360 = 0, 361 = 1 in angles.. you could potentially get a *5 and a 355.. where 355 would actually be the lower one.. So *just do:

    for b = 1 to 4
    a(b) = a(b)+180
    if a(b) > 360 then a(b) = a(b) - 360
    next b
    **redo low-high here
    **redo angle check, dont forget to change Ballangle (+180 *-360)
    for b = 1 to 4
    a(b) = a(b)-180
    if a(b) < 0 then a(b) = a(b) + 360
    next b
    End if


    THIS IS UNTESTED AND I MADE IT UP HERE, NO GUARANTEES ON BUG-FREE.

    Ok, now for colliding with sides:

    BallAngle = AngleofWhatYouHit-Ballangle

    So if your headed straight up (90) and hit the ceiling (0) you will get -90 (270), which is the way it should be.

    If you hit side (90) and are going at 45 (up+right) it will change to 135 (up+LEFT)
    Don't pay attention to this signature, it's contradictory.

  17. #17

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Thanks...

    I'll see if i can get something like that into the game later on...

    The biggest problem now is that something get wrong with one type after finishing a level...i cant redim it...

    And another big problem is when the ball hits something, its new angle is not correct...

    When the ball hits the top, i use this code:
    Ball(i).Angle = 180 - Ball(i).Angle

    When it hits one of the sides, i use this:
    Ball(i).Angle = -Ball(i).Angle
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  18. #18
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    alright since you have to calculate the x and the y component of your movement every time you should directly store those values instead of doing the same calculation over and over again. that way collision gets easier too. if it hits the side take the x-component *-1 if it hists the top take the y-component *-1. If you hit something that requires you to change the angle you can always get the angle back from the x and the y component using the tan but I would store the angle anyways (again to save calculations and avoide rounding mistakes and lines of code)
    Sanity is a full time job

    Puh das war harter Stoff!

  19. #19
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Are you doing Sin for X and Cos for y? or Cos for x and Sin for y?
    Last edited by alkatran; Dec 15th, 2002 at 09:29 AM.
    Don't pay attention to this signature, it's contradictory.

  20. #20
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Can you tell me what the angle is her (from what to what)...

    When the ball hits the top, i use this code:
    Ball(i).Angle = 180 - Ball(i).Angle
    Look at my drawing and tell me...because on my drawing I have marked angle A and B but I can't see your code calculation those...or am I not able to see it...

    Attached Images Attached Images  
    Last edited by NoteMe; Dec 15th, 2002 at 09:27 AM.

  21. #21
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Top is 180, Ball.angle on left of equation is B, Ball.angle on right of equation is A

    He should be using 0 and not 180 I think
    Don't pay attention to this signature, it's contradictory.

  22. #22
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by alkatran
    Top is 180, Ball.angle on left of equation is B, Ball.angle on right of equation is A

    He should be using 0 and not 180 I think

    I still can't see what he is thinking...because if the angle moves from A to B, and in A it has 60degrees, then he want an angle of -60degress or 300degress after the hit...

  23. #23
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    well all of that wouldn't be an issue using the x and the y component
    Sanity is a full time job

    Puh das war harter Stoff!

  24. #24
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by /\/\isanThr0p
    well all of that wouldn't be an issue using the x and the y component

    I know, thats how I make games

  25. #25

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    As you can see on the pic, 0 degrees is straight down.

    Then you can see that the angle A + B = 180, so B = 180 - A.

    Attached Images Attached Images  
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  26. #26

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    i didnt want to use radians in this game, because im always getting trouble when the ball is moving many Y values in one X value...then the ball moves too fast!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  27. #27
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by cyborg
    i didnt want to use radians in this game, because im always getting trouble when the ball is moving many Y values in one X value...then the ball moves too fast!
    This doesn’t make sense. You can have the exact same X and Y movements using Rad, but I have to admit that I like to use Degrees too...

    And the calculations of the angle looks fine with me, so I think that there has to be another place in the code there is a BUG...But I can't see the ball moving in the wrong direction on my PC

  28. #28

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Open up the code, search for this line:
    Ball(1).Angle = 180 - 10

    change it to: Ball(1).Angle = 180

    That should make the ball move straight up when shooting it the first time! but it doesnt!

    and if you bounce the ball to move straight up, it should move straight down after hitting the top, but it moves a bit to the left!

    I have no idea where the error can be!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  29. #29

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    lol! Just 1 sec after posting the last post i found the error!
    Pi was defined in a long, making it to only 3, not 3.14........
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  30. #30

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Can anyone help me with this code?

    This is used in the collition detection to check the new direction of the ball (which side of the brick it hits)



    TempCol holds the X value of the brick that the ball hits
    TempRow holds the Y value of the brick that the ball hits
    These are not in pixels, but in count of bricks...
    One brick are 30*15 pixels

    X holds the X value of the ball when hitting a brick
    Y holds the Y value of the ball when hitting a brick

    VB Code:
    1. If X < TempCol * 30 - 29 Or X > TempCol * 30 - 2 Then
    2.             If X < TempCol * 30 - 28 Then
    3.                 'hit left side
    4.                 Form1.ChangeDirection BallNr, "L"
    5.             ElseIf X > TempCol * 30 - 2 Then
    6.                 'hit right side
    7.                 Form1.ChangeDirection BallNr, "R"
    8.             End If
    9.         Else
    10.             If Y < TempRow * 15 + 29 Then
    11.                 Form1.ChangeDirection BallNr, "U"
    12.             Else
    13.                 Form1.ChangeDirection BallNr, "D"
    14.             End If
    15.         End If


    This code is not correct, and there must be a better way of doing this...
    Last edited by cyborg; Dec 15th, 2002 at 04:09 PM.
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  31. #31

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Here's a zip with the program and source...fully commented..
    Did some fixes to it...
    Attached Files Attached Files
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  32. #32
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    umm

    When I went from level 1 to 2.. there was a block that wasn't ever detected as hit.. it didn't fit pattern either, here's a picture:
    Attached Images Attached Images  
    Don't pay attention to this signature, it's contradictory.

  33. #33
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: umm

    Originally posted by alkatran
    When I went from level 1 to 2.. there was a block that wasn't ever detected as hit.. it didn't fit pattern either, here's a picture:


    Nice shooting...

  34. #34

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Yes i know...
    just found out what was wrong!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  35. #35

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Does anyone have an idea on how to fix that code (some posts up)?
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  36. #36
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    umm

    I'm not really sure, I like my system , but don't forget to increase the 'range' of detection as ball speeds up (and check angle, so it doesn't catch a right hit on left side)
    Don't pay attention to this signature, it's contradictory.

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