This question has been stuck on my mind for a while, and im wondering if im missing a method or something.Im trying to figure out how i can get an imageview in Android Studio(IDE) to move in the direction of its rotation, though this image probably best explains my question

- 552
- 6
- 27
-
Can you please show what have you tried? – Ekin Jul 11 '15 at 16:41
-
i honestly dont know where to start, ive googled and stackoverflowed "how to get an imageview to move in the direction of rotation" and variations of that but all that comes up is stuff on rotation or does not anwer my question – Mazino Jul 11 '15 at 16:44
-
if it helps im thinking of using a while loop – Mazino Jul 11 '15 at 16:48
-
http://stackoverflow.com/questions/29343400/android-java-move-control-in-direction-in-degrees This might help, I suggest checking the links provided there too. – Ekin Jul 11 '15 at 16:54
-
the question u linked me to was the almost the same question, but if u look closely they dont reach a finite answer/conclusion :/ – Mazino Jul 11 '15 at 16:58
1 Answers
So I found out how to do it with the help of some links and i formulated my own solution.
First, I calculate how much I am adding/subtracting from the image's x and y using its current rotation with some Trig. The ratio between the two is what gives the effect of moving in the direction.
final double rise = Math.toDegrees( Math.sin(toRadians(YOU.getRotation())) )*SPEED;
final double run = Math.toDegrees( Math.cos(toRadians(YOU.getRotation())) )*SPEED;
Then, I use a timed loop and add the set x and y
(new Thread(new Runnable()
{
@Override
public void run()
{
for (int x=0; x<600; x++) {
try {
Thread.sleep(6);
runOnUiThread(new Runnable() thread
{
@Override
public void run() {
YOU.setX((float) (YOU.getX() + run)); //pay attention here
YOU.setY((float) (YOU.getY() + rise));//pay attention here
}
});
} catch (InterruptedException e) {
// ooops
}
}
}
})).start();
}
Notice how I use a for loop with a bunch other code, It should be noted that this isnt neccesary, but I use it for my specific needs. You can put a while loop instead of a for loop if u want but this part
YOU.setX((float) (YOU.getX() + run));
YOU.setY((float) (YOU.getY() + rise));
is the code that adds/subtracts your x and y.
Also, the variable SPEED
in the declaration of rise
and run
is set to 0.02 (not shown here) because I add x and y through a loop that makes it look much smoother. The greater the number here the farther itl go (per time u add) but it doesnt affect the accuracy of angle forward. Though I'd reccomend putting at least a filler number there.
Here is my original code all in one place in case you need it.
public void moveloop(){
final double rise = Math.toDegrees( Math.sin(toRadians(YOU.getRotation())) )*SPEED;
final double run = Math.toDegrees( Math.cos(toRadians(YOU.getRotation())) )*SPEED;
(new Thread(new Runnable()
{
@Override
public void run()
{
for (int x=0; x<600; x++) {
try {
Thread.sleep(6);
runOnUiThread(new Runnable() thread
{
@Override
public void run() {
YOU.setX((float) (YOU.getX() + run));
YOU.setY((float) (YOU.getY() + rise));
}
});
} catch (InterruptedException e) {
// ooops
}
}
}
})).start();
}
Helpful links:
https://math.stackexchange.com/questions/142073/calculate-new-graph-point-with-coordinate-and-angle