Hi all. I’ve done this a million times and I’ve always gotten it working, but whenever I try to replicate a technique I’ve used in a previous project to accomplish this it never works. I want to shoot a projectile in the direction of the player’s mouse cursor. I’m currently setting the projectile’s velocity by:
Vector2 vec = new Vector2(Gdx.input.getX() - getPos().x, Gdx.input.getX() - getPos().x);
float angle = (float) (Math.atan2(vec.y, vec.x));
getBody().setLinearVelocity(new Vector2(speed * MathUtils.cos(angle), speed * MathUtils.sin(angle)));
I’m not too good at vector math as I’m only 15 so I’m not sure if that’s even the correct way to do it, but it’s always worked for me.
The problem is that there are only two angles, 0.7853982 radians and -2.3561945 radians. So the projectile can only shoot in those two directions instead of all the possible directions.
I’m using LibGDX and Box2D, and I’m also in Ludum Dare 31 right now so I need an answer FAST.
So am I casting it weirdly, or do I need some Math.toDegrees’s?
Thanks everyone.