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.

1 2 3

voidmoveLeft() { 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.

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.

Arthur: Are all men from the future loud-mouthed braggarts?Ash: Nope. Just me baby...Just me.

Slope is very bad to use, partly because the slope of a vertical line is infinite. Use vectors:vel_x = bullet_speed*cos(angle)vel_y = bullet_speed*sin(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_xpos_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.

Slope is very bad to use, partly because the slope of a vertical line is infinite. Use vectors:vel_x = bullet_speed*cos(angle)vel_y = bullet_speed*sin(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_xpos_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.

I created a Vector for this and got it all setup, however it's still inaccurate.

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.

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.

I'm honestly not sure what I'm doing, I'm just trying to figure everything out and learn. I'll switch it to radians and let you know the outcome.

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..

I think the real problem is that you're not keeping a consistent definition of an angle. The usual definition is that the +x axis is 0 degrees and the +y axis is 90 degrees. Math.atan2(y,x) will give you an angle according to this definition, so you need to make sure all of your code is consistent with this.

I'm guessing the player sprite image is orientated upwards and you're using the axes for 2D where the top left corner is (0,0)? in this case if the sprite is drawn without rotation it will face in the -90 direction so there needs to be an offset of +90 added to the rotation when it is actually drawn.

I'm not sure exactly what setRotation and playerSprite.setRotation do, or why you are calling one inside the other, but try (with the above modification as well):

1

playerSprite.setRotation(setRotation(degreesToMouse) + 90);

(as long as that setRotation only changes the actual sprite's rotation and not the rotation used for the bullet calculations, otherwise remove the +90 from here and move it to where the sprite is drawn)

I think the real problem is that you're not keeping a consistent definition of an angle. The usual definition is that the +x axis is 0 degrees and the +y axis is 90 degrees. Math.atan2(y,x) will give you an angle according to this definition, so you need to make sure all of your code is consistent with this.

I'm guessing the player sprite image is orientated upwards and you're using the axes for 2D where the top left corner is (0,0)? in this case if the sprite is drawn without rotation it will face in the -90 direction so there needs to be an offset of +90 added to the rotation when it is actually drawn.

I'm not sure exactly what setRotation and playerSprite.setRotation do, or why you are calling one inside the other, but try (with the above modification as well):

1

playerSprite.setRotation(setRotation(degreesToMouse) + 90);

(as long as that setRotation only changes the actual sprite's rotation and not the rotation used for the bullet calculations, otherwise remove the +90 from here and move it to where the sprite is drawn)

My Player Extends the Entity Class, setRotation is an float that sets the rotation value of the Entity, and returns the value set, allowing me to set both in a single line.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org