My previous question asked was about setting the rotation of an sprite to face the Mouse at all times, with help of a member here I managed to get that workng, now that I have done that, I have created a Bullet class, and great knews, I’ve got everything set up just the way I want it, minus one thing. The movement. I have no idea how to go about moving this properly, I’ve tried a few things, but I can’t get it the move in a straight line.
What I’m Trying to do: Instantiate a sprite to move in a certain direction.
I’m just not sure on what needs to be done for the logic calculations, once again. I’ve done some research and I was brought to a topic on GameDev forums that provided a solution, however it was giving me the same issue that I had to begin with. There is not a “target point” for this object, it’s just supposed to move along an angle.
This angle is the same angle that the sprite it rotated by, fortunately, so getting the angle wasn’t a problem.
But how would I move it?
Currently I’m using a Vector2 for my positioning and just using a standard system for movement.
void moveLeft() {
setX(getX() - playerSpeed);
}
^This is to give you an example.
While I was developing in Unity a few years ago, this was called “adding force” to an object, but I’m not exactly sure how it all worked, but it caused it object to move forward constantly.
Use rise over run. Slope is OP.
((mouse y - player y) / (mouse x - player x))
Bam, you have the slope the bullet needs to move at, now for every 1 x you move, you move slope * 1 y.
Couldn’t you solve this problem using the slope formula. You can pick an arbitrary target point along your projected line. In 3D applications this is done by assuming a vector length of one. It seems to me that if you can determine the rise/slope over one pixel then calculating your vertical position at any point along a horizontal plane/run should be trivial. I’m assuming this is 2D we’re talking about, so feel free to disregard this if that’s not the case. You’d have to account for the edge cases of 0, 45, 90, and 180 degrees.
As for moving forward, determine the direction you’re moving in (-1 left or +1 right), and multiply that value times your speed and add it to your current X position, then determine the Y position based off of the previous formula.
Edit: Doh! Looks like Biznatch beat me to it. :point:
^This one causes motion, however it’s innacurate (Probably because I’m just making it add to the Y Axis and multiplying the speed by the slope(Is what I assume is going on here)
Ideally you should have some sort of time delta passed to your update method that would be used in the calculation along with bullet speed to achieve FPS independent movement.
Slope is very bad to use, partly because the slope of a vertical line is infinite. Use vectors:
vel_x = bullet_speedcos(angle)
vel_y = bullet_speedsin(angle)
(vel means velocity)
Then every frame just update position by the velocity (or if you’re using delta times update position by velocity*delta):
pos_x += vel_x
pos_y += vel_y
This way the bullet travels at the same speed in any direction.
btw the vector [cos(angle),sin(angle)] is a unit vector (length of 1) in the direction of the angle, so it moves the bullet by 1 unit in that direction. Multiplying by bullet_speed makes the bullet move bullet_speed units in that direction, which is what you want. It’s probably worth learning a bit of linear algebra for this stuff.
Learn something new everyday. Totally forgot about slope being bad for cases where you need to cover all the possibilities, because ya know, slope can be undefined after all. Did not know that cosine and sine were so useful for that either.
It’s really inaccurate, to give you an example of what I mean, here are a few images, with the mouse in the position i clicked, and the direction the bullet is travelling.
For Math.sin and Math.cos I’m fairly sure the angle needs to be in radians, not degrees. Using radians everywhere would make the code simpler, but if you really don’t like radians I guess you can use degrees, just be careful.
the difference of the startPoint and the endPoint?
EDIT: My problem isn’t the speed of movement, it’s getting it to move in the correct direction.
The way you guys are explaining things is helping a bit, however I’m the kind of person that learns from code that I see.
I don’t mind trying to figure it out, but it’s difficult for me when I don’t exactly understand what you guys are telling me I need to do…
((( Basic N-E-S-W movement was soooo much easier, lol. )))