I've just implemented my first throwing weapon- a snowball. Now I want the player to be able to throw in the direction of the mouse curser, instead of just throwing to either sides. The problem is I can't seem to grasp how to do this the right way.
When I create a snowball I give it a starting position and a velocity (a Vector2). I add the velocity to the position, and update the velocity with gravity and such accordingly.
My current solutions, that I haven't had good results with are the following:
- Convert the mouse position into tile coordinates, and use the distance in tiles on the y axis to determine the up- or downwards speed in worldspace. No changes to the X velocity,
- Do some math with radians or degrees, to get the angle between the player and the cursor. This solution is one I haven't tested, since I have a hard time figuring out the math
My last suggestion is just trial and error and simply hardcode some values that I find works out, based on the mouse position, but this is really not the way to do it (unless there really aren't any other way, which I doubt).
I am going to sneak in another quick question about weather.
I've recently added rain and snow to my game. Snow will slowly gather on the ground and be "harvestable". Now I want the snow to smelt again, when it gets warmer, but I'm looking for a smart way of doing this. Currently I just randomly pick a spot on the x-axis in tile terms within a set distance of the player, and then I iterate through the Y axis to check for snow, and remove it. I do this to add a sense of smelting over time, instead of the snow just dissapering in a predictable manner. Is my approach good enough, or are there better ideas?
If you have a hard time picturing what I mean, I have a video you can look at to see the snow in effect. If you don't want to see the whole video go to 1.28 to see where the snow starts, or go to 2:05 for the harvest part.
[media]
[/media]
So to sum it up:
- How do I shoot/throw an object in the direction of the mouse in a 2D tile based platformer?
- (Not so important) Are there a smart way to make the snow smelt over time, or is my method good enough?
Any help is greatly appreciated. - Thanks in advance :-)