Results 1 to 15 of 15
  1. #1
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default calculating the speed of moving point

    hey,

    i receive one set of x and y coordinates. the coordinates keeps on changing randomly. the x could increase and y could decrease or vise versa.
    the x and y values could stop changing suddenly.
    the x and y values could start increasing/decreasing very fast or very slowly.
    the x value could increase/decrease but y value could stay the same or vise versa.

    the input of x and y is received when the program is executed and the new values of x and y are fed to the program while the program is executing.


    what i want is to calculate the speed of that moving point(x,y) , in pixels per second.

    so if the point is not moving( x and y values do not change ) the speed should be 0

    if the x and y values increase/decrees very fast the speed should change accordingly e.g 1000 pixels/sec

    it should work in the similar concept as the car, once the driver sits in the car and starts the engine and if he or she does not know where and how far they going the speedometer would still be showing the speed at which the car is going


    i do not know what distance the x and y will cover and i do not know the time for how long the program will be executed and how long the x and y values will be changing


    i hope you understand what i need and will be able to help me.

    thank you very much

    any help will be greatly appreciated

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    Do you know the distance formula? Just use it to determine distance each step, then take the average.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default

    thanks for your reply
    well i do know distance formula it would be speed = distance/time;
    but as i said i do not know the distance and i do not know the time.
    and i do not get any different stages in the program my x y moves all around the screen or just stops.

    thanks

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Speed is distance/time. To calculate the distance between two points,

    Java Code:
    double delX = x2 - x1;
    double delY = y2 - y1;
    double d = Math.sqrt(delX*delX+delY*delY);
    Beyond that, you have to decide if you want to report instantaneous speed or some form of predictive or averaged speed. Those are algorithm choices rather than java specific issues.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    The distance formula is not distance/time. That would not make sense. I meant the point distance formula.

    I'm not really sure what you're saying about not getting different stages. If you want help with code, you'll have to post an SSCCE that demonstrates what you're talking about.

    PS- You really shouldn't post duplicate threads. It's really confusing to follow.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default

    thanks for this formula, but what would be the x2 and y2 i understand x1 and y1 is the current position of my point.

    if x2 and y2 is the previous position, but which position would it be and when.

    the coordinates change all the time think of it as a mouse cursor if you move it the speed of it would change according how fast you moving it and if you stop the speed should be zero.

    mouse cursor does not have starting or ending position it just moves when you tell it to move and moves as long as you keep on instructing it to move. so how would the speed of it would be calculated

    thank you

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gedas View Post
    thanks for this formula, but what would be the x2 and y2 i understand x1 and y1 is the current position of my point.

    if x2 and y2 is the previous position, but which position would it be and when.

    the coordinates change all the time think of it as a mouse cursor if you move it the speed of it would change according how fast you moving it and if you stop the speed should be zero.

    mouse cursor does not have starting or ending position it just moves when you tell it to move and moves as long as you keep on instructing it to move. so how would the speed of it would be calculated

    thank you
    Sample your (x, y) points at regular time interval lengths t. Make t is small as possible.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default

    sorry for double posting , new to this forum...

    so if i store the coordinates lets say every 100 milliseconds. then i find the distance between the coordinates that i stored 100 milliseconds ago and now. so i have my distance which i would divide by 100 milliseconds this would have to show me the speed right?
    am i on the right track?

    thanks again

  9. #9
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Yes, that's correct. The disadvantage of sampling fast is that you will end up with limited resolution. For example, if you are sampling so fast that there is never more than 1 pixel of motion, then your speed is going to alternate between 0 and 1/time_delta...which is probably not what you want.

    What you're trying to do can be algorithmically challenging, because what you probably really want is a Kalman filter that is tolerant of noise and predicts what the speed is *now* instead of reporting what it *was*.

    Best of luck!

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Two similar threads merged together. Original poster, please don't double post the same question in multiple threads.

  11. #11
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default

    yeah thats a problem it gives me the speed that *was* instead of what it is now. also the problem with the above solution is that if small range of coordinates has been covered very fast it would still be divided just after 100 miliseconds and it still would give me the avrage speed instead of the current speed.

    Kalman filter that is tolerant of noise and predicts what the speed is *now* instead of reporting what it *was*.
    could you tell me more about this kalman filter ? and how would it be implemented?


    thank you very much

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,301
    Rep Power
    20

    Default

    could you tell me more about this kalman filter ?
    Kalman Filter

    db

  13. #13
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default

    well i read about it but i am still very puzzled how could this be implemented in the code. and the theory of it is too hard to grasp for me cos my math knowledge is limited to my high school education.

    could you please explain to me how Kalman filter could be used in my situation.

    thank you very much

  14. #14
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    A Kalman filter is a statistical predictor. It's implementation is a signal processing question rather than a Java question.

  15. #15
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default

    well it is a question of how to implement signal processing in to java code. if you know what forum could answer me this question please let me know.
    p.s my orginal question was not related with Kalman filter.

    thank you

Similar Threads

  1. Java Topology suit (JTS) - calculating point
    By ArticRoot in forum New To Java
    Replies: 4
    Last Post: 02-22-2011, 02:54 PM
  2. Moving objects - setting different speed
    By Antigol in forum Java 2D
    Replies: 2
    Last Post: 08-14-2009, 01:14 AM
  3. How to speed sql Statements?
    By bezudar in forum Advanced Java
    Replies: 3
    Last Post: 11-20-2008, 09:53 AM
  4. compare speed
    By bbq in forum JDBC
    Replies: 1
    Last Post: 06-28-2007, 05:34 PM

Posting Permissions

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