Help. Moving object in 2D in straith path


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: Help. Moving object in 2D in straith path

  1. #1
    Join Date
    Sep 2004
    Posts
    5

    Help. Moving object in 2D in straith path

    I am making a simple video game with moving balls for mobile device (MIDlet)
    I have code sample with ball that moves in 45 degree. It is very simple to animate that. Only change position X and Y for +1 or -1 pixes and then repaint.

    I need something different. Let say that ball is the one position (X,Y)=(5,4) and I want to move it to the some desired position (X,Y)=(120,34) for example(in a straight path).

    There are basicaly two different problems here. FIrst is what to do when moving angle is not 0, 45 or 90. Then somethimes I should move the ball one pixel up, then up an left, then up...

    Second is the speed. It should go always in the same speed. I dont need gravitation effect or so, so I think it is very simple problem(maybe I will have to use sin and cos functions for calculating Thred delay).

    I know there are lot of examples and source codes for that, but I just couldn't find one.

    Can anyone help me, or give me some source code?
    My email is igormita@yahoo.com

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    The way I've always done this before is to update the x and y position of the ball by "change" variables - a certain amount of x, a certain amount of y.

    When the ball hits an edge you reverse the sign of the relevant "change" variable.

    Here's some rough code:
    Code:
    public void update() {
      ball.x += changeX;
      ball.y += changeY;
    
      if (ball is beyond the vertical screen borders) {
        changeX = -changeX;
      }
      if (ball is beyond the horizontal screen borders) {
        changeY = -changeY;
      }
    }
    Something like that...
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Sep 2004
    Posts
    5
    No, no, I know that. The problem is something else.
    For example you want to move ball 12px(movX) right and 3px down(movY).
    I think that I've found sollution.
    Here is the desired path(stars)
    ****--------------
    ------****-------
    ------------****
    I shold first check is it on right position. Then ball should stop.
    Then, if movX=movY then it should go diagonal, so it is easy adding 1 px right and down
    But if not (12<>3) then i should do it in 12 steps (bigger number) but on every 4th move to he rigth i should move one px down

    Code for that is
    if (movX mod movY=0) then go 1px right and 1px down
    else go 1px right
    movX=movX-1

    If movY is bigger then they change the place

    Here is with movX=3 and movY=4
    *--------
    *--------
    --*-----
    -----*---
    (4 mod 3 =1, go right)
    (3 mod 3 = 0, 3=3, go rest of the path diagonal)
    Hmm, hmm...
    It is not complete correct jet but that is an idea

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    I'm not quite sure what the problem is, but this seems to be a VERY complex way of coding a bouncing ball.
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Feb 2004
    Posts
    808

    Re: Help. Moving object in 2D in straith path

    Originally posted by igorBG
    [B]I am making a simple video game with moving balls for mobile device (MIDlet)
    I have code sample with ball that moves in 45 degree. It is very simple to animate that. Only change position X and Y for +1 or -1 pixes and then repaint.

    I need something different. Let say that ball is the one position (X,Y)=(5,4) and I want to move it to the some desired position (X,Y)=(120,34) for example(in a straight path).

    There are basicaly two different problems here. FIrst is what to do when moving angle is not 0, 45 or 90. Then somethimes I should move the ball one pixel up, then up an left, then up...

    a third factor steps in, that you have not considered; the number of steps you wish to go through to move the ball.. if a ball has to go from 10,10 to 20,20 in 10 steps, its 1 pixel per step. in 5 steps you must move 2 pixel per step and the ball moves twice as fast apparently..

    so decide the number of steps and then divide the path length by that
    in your example: from 5,4 to 120,34 requires:
    115 x
    30 y

    suppose this is over 5 iterations:
    115/5 = 23 x pixels each iteration
    30/5 = 6 ypixels per iteration

    many midlet devices dont do floats so you cant really have a x pixel of 0.1666 movement. instead you would need to multiply this by 6, to get 1 x pixel per every 6 iterations..

    so you divide the path length by the iterations.. but then you might have to multiply the number of iterations you make by some number to make any decimal points go away
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  6. #6
    Join Date
    Mar 2003
    Posts
    834
    I can read your original posting, there's no need to post it again. What I am saying is that I'm sure from your comments quite what the problem is with my solution.

    a third factor steps in, that you have not considered; the number of steps you wish to go through to move the ball.. if a ball has to go from 10,10 to 20,20 in 10 steps, its 1 pixel per step. in 5 steps you must move 2 pixel per step and the ball moves twice as fast apparently..
    I have considered it. If you want the ball to move from 10, 10 to 20, 20 in 10 steps, you simply set changeX and changeY to 1!
    ArchAngel.
    O:-)

  7. #7
    Join Date
    Mar 2004
    Posts
    635
    //number of steps to get from point A to point B
    steps = 5

    //distance from point A to point B
    distance = sqrt((ax-bx)^2 + (ay-by)^2)

    //speed at which ball will move to get from A to B
    speed = distance / steps

    //angle between A and B
    //i think atan2() is the right one
    angle = atan2(by-ay, bx-ax)

    //now calculate new position
    ax = ax + cos(angle) * speed
    ay = ay + sin(angle) * speed


    I might have switched cos and sin. So if it moves the wrong direction, just switch em.

  8. #8
    Join Date
    Sep 2004
    Posts
    5
    Thanks. You are helping me a lot.
    Now I got the new solution.
    Startiong point is 5,5 end point is 120,30. So deltaX,deltaY=115,15
    tan=115/5=23
    That is the tangens of angle in which ball should move i think, and I should always keep it, while ball is moving.

    Now I have to decide does the ball should go only 1px left or I should put it in the same time left and down in the next step.
    If it goes only left position is 6,5 deltaX,deltaY=114,5
    tan=114/5=22,8

    But if I change Y too then pos = 6,6
    deltaX,deltaY=114,4
    tan=114/4=28.5

    So the winner is first solution because tan is much closer to the desired 23.
    next iteration...

    It has to do something with proportional angles etc.
    I will try, I only have to think what to do when one delta comes to 0 (then change only other delta) which is important when ball moves nearly horizontal or vertical.

  9. #9
    Join Date
    Sep 2004
    Posts
    5
    Hey Phaelax I will try your solution.

  10. #10
    Join Date
    Mar 2003
    Posts
    834
    I still think you guys are making this much more complicated than is necessary. check out the following bouncing ball code, using the technique I described:

    http://javaboutique.internet.com/tut...ection6_9.html
    ArchAngel.
    O:-)

  11. #11
    Join Date
    Mar 2004
    Posts
    635
    I know what you're saying, but I dont think he just wants to bounce the ball around the screen. It's movement on X and Y are fixed. My solution allows him to move the ball to any point he wants. It does sort of the same thing. You're using two vectors that never change. I'm using a direction unit vector (cos(angle))and a magnitude(speed).

  12. #12
    Join Date
    Sep 2004
    Posts
    5
    I have example with bouncing ball, and that was my starting point.

    As I said I want to move ball to the desired coordinates.
    It is not important where the ball is, but where it should be and on what path it should move.

    To make things much more difficult I've found yesterday that MIDlets dont support floating point numbers!!!
    Only integers!!!! Something about that most mobile devices cant calculate with floating point.
    So 3/2=1 on mobile device.
    I've found sollution, I multiply everything with 1000 and in that way i've got enough precise calculations. 3*1000/2=1500
    It is very important to have precise tangent of angle in whick ball should move, and with this method I simulate 3 decimals. I dont think i need more than that.

    I wrote function yasterday, with algorhythm that you can find in my previous post and after several fixes it worked(well close enough). All balls go to the desired position(thay are randomly created), but some off them stop several pixels left or right. and the path is curve,not straight line(don't know jet why, propably one wrong + or - sign)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center