开发者

Face a Path object (arrow) towards another object (bitmap) on the screen

开发者 https://www.devze.com 2023-04-03 08:56 出处:网络
I\'ve almost got it, but there\'s a few problems. Here\'s my current code for drawing the arrows. The path creation included.

I've almost got it, but there's a few problems.

Here's my current code for drawing the arrows. The path creation included.

{//gets don开发者_如何学JAVAe just once to create the arrow
oBlipPath.moveTo(0, -5);
oBlipPath.lineTo(5, 0);
oBlipPath.lineTo(0, 5);
}

{//run in a for loop for every enemy (i)    
canvas.save();
canvas.translate(iWidth / 2, iHeight / 2);
canvas.rotate((float)Math.toDegrees(Math.atan2((oEnemies[i].getEnemyCenterY()-worldY)-user.getShipCenterY(), (oEnemies[i].getEnemyCenterX()-worldX)-user.getShipCenterX())));
canvas.drawPath(oBlipPath, oBlipPaint);
canvas.restore();
}

To give a little context, user is the player, who is on the center of the screen. oEnemies are objects which are for the player to shoot down, both onscreen and offscreen.

What I'm attempting to do is create a sort of radar around the player. Tiny little arrows to show the way to go to destroy all the objects and complete the mission.

As I've mentioned in my previous posts, I'm great with logic, but absolutely terrible at math. Thank god you folks are available here, because these roadblocks are a killer and I'd probably give up sooner than later. Thank you for the help so far!

Anyway, back to the issue. The translate 20,20 is there really for debugging. I was trying to figure out what it does. Right now, I see the arrows, but they look like they're rotating around an axis at the top left of the screen at a radius of 20 pixels. They are reacting to the object coordinates, but in the wrong way it seems.

I will keep fiddling, but my question is, how do I get the rotation pivot be at the center of the screen, and get the arrows to point towards the objects flying around? To be precise, the arrows need to not only point towards the objects, but also move around a radius around the player's avatar in the center of the screen.

Thank you!


(my math isn't great but i know the issue).

You need to translate your player and everything else with respects to 0,0 as part of the angle calculation then rotate and translate back.

Think of it like this, you always rotate around 0,0 otherwise the rotation isn't accurate (because you always rotate with respect to 0,0, if you aren't at 0,0 you will just rotate around it), so if you need to rotate an object at 10,20 by 20 degrees you first subtract the differences in both axis (in this case 10 and 20) then you rotate the object by 20 degress (so you are effectively rotating this object with respect to 0,0) then you add back on the 10 and 20 you subtracted. This causes the rotation to be correct.

In your example you need to offset the entire world (this is actually a world translation i think it's called) so you want to work out your players difference from 0,0 and subtract that from all objects effectively making 0,0 the centre of your player and keeping all other objects in relation to this. Then you make the rotation calculations to place the arrows around 0,0. Then translate everything (including the newly created arrows) back by adding the offset back on.

(The more effecient way to do this would be to calculate the matrix needed to move, rotate and move [see matrix multiplication] and apply this to all objects which would rotate everything at the same time.)

I've only done this in OpenGL and for that you just use a translation matrix to apply it to the entire world. I'm not sure that there is anything like that in andriod canvas but i assume this method is still the same.

This Link has a graphical illustration of this concept. I believe this will put you on the right track.

0

精彩评论

暂无评论...
验证码 换一张
取 消