If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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.

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

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

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!

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.

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).

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)