This almost works, but when I click on a point that has a smaller X value than it's current position it reverses the Y Direction.
On Aug 27, 1:01 pm, Robert Green <rbgrn....@gmail.com> wrote: > Direction will be in the range -180 to 180 so don't use abs! Always > use sin/cos for y/x respectively when in y+ down 2D coordinate > systems, otherwise x/y respectively. > > Here is kind of what you want > > // tickDelta should be something like .016f to .033f - definitely > should total 1.0f per second :) > float tickDelta = (currentMs - lastMs) / 1000f; > // now mMoveSpeed can be expressed in units-per-second and it'll work > at any framerate if you multiply by tickdelta > > float touchDelta = (float)(TouchY - V.CurY) / (TouchX - V.CurX); > float dirRads = (float)Math.atan(touchDelta); > float amountX = (float)Math.cos(dirRads) * mMoveSpeed * tickDelta; > float amountY = (float)Math.sin(dirRads) * mMoveSpeed * tickDelta; > x += amountX; > y += amountY; > > // no additional checks needed. This is how you do 2D directional > movement. > // this assumes that either TouchX/TouchY or V is set only when the > touch is touched down initially so that a vector is made when the > touch moves and that value is not updated. > > On Aug 26, 8:40 pm, Jeffrey <jeffisagen...@gmail.com> wrote: > > > > > I know this is more of a general programming question, but my Android > > friends here have always been helpful. I am trying to make an image > > move from one part of the screen to a position the user touches, as a > > constant speed. I don't know the best way to go about doing this and > > the way I have been trying to get to work is taxing my brain too much. > > > I have been using geometry to calculate slope, then using Sin * > > movement speed to calculate the difference in X,Y coordinates. > > > This is the code I'm looking at: > > > TouchX = V.TX; (This is the touch event coordinates > > TouchY = > > V.TY; ) > > > Slope = (TouchY - V.CurY) / (TouchX - V.CurX); > > (this figures out > > the slope of the line to the touch point) > > DegreeSlopeY = Math.atan(Slope); > > DegreeSlopeY = Math.abs(DegreeSlopeY); > > (this is so > > that I can calculate the direction the image should travel along the > > slope) > > DegreeSlopeX = (90.0000000000000 - DegreeSlopeY); > > NewY = (mMoveSpeed * Math.sin(DegreeSlopeY)); (This > > calculates the > > shift in Y axis for the image) > > NewX = (mMoveSpeed * Math.sin(DegreeSlopeX)); > > (This calculates the > > shift in X axis for the image) > > if(TouchX < V.CenterX) NewX = -NewX; (This is to > > finish > > calculating the correct X axis direction to travel) > > if(TouchY < V.CenterY) NewY = -NewY; (This is to > > finish > > calculating the correct X axis direction to travel) > > > The problem that I'm having is the image is veering way of course when > > traveling in Y heavy paths. When traveling horizontally it works > > almost perfect. > > > I know there is probably an easier way though I don't know how to use > > a lot of things (like OpenGLES) so if your advice is to use a > > different method please link a tutorial for that method if you know of > > one. > > > Thanks in advance. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en