Need help moving projectiles with math...

Ok so i felt like i should know how to do this using the formula y-y1 = m(x-x1)
So I have a stationary object (refered to as sX and sY) and the moving object (mX, and mY) and two cordinates for the projectile (bulletX and bulletY) i know these are bad variable names but just using for the sake of this post
I am shooting the projectile from the stationary object to the moving object


float bulletX, bulletY;
float slope = (mY - sY) / (mX - sX);
bulletY = slope * (bulletX - sX) + sY;

The part I dont understand is what to assign to the value of bulletX since I cant use this code as the value of bulletX has not yet been assigned. Keep in mind im only 14 lol
Just to refrase I am trying to move an image across a line designated in the code above which moves the image from a stationary object to a moving object the target

Problem numero uno is you are talking about trigonometry, not linear algebra :slight_smile:

I’d like to point you to my favorite trig cheat sheet care of wikipedia:

http://upload.wikimedia.org/wikipedia/commons/9/9d/Circle-trig6.svg

O is your stationary object, A is your moving object, and C is [Movable.X, Stationary.Y].

public float getAngle(final Point source, final Point target) {
    final float angle = (float) Math.toDegrees(Math.atan2(target.x - source.x, target.y - source.y));
    return (angle < 0) ? angle + 360 : angle;
}

Will get you the angle. I’m pretty sure your bullet is moving in a straight line so you want to do this as soon as it is ‘fired’. Then you need to convert the angle into a vector and store it like so:

float vectorX = Math.cos(angle);
float vectorY = Math.sin(angle);

now

bulletX = startX + (distance * vectorX)
bulletY = startY + (distance * vectorY)

Hopefully that’s close to what you’re looking for…

I have never learned trig so some of this stuff is confusing… is there any simpler way to do this in linear algebra?

im only 14 and taking geometry now

Any way you look at it, trig != algebra
He’s already given you the code, what’s to complain about? You don’t even need to write anything yourself. :point:

Just use vector2 and the rotate() method for libGDX. And…you really should start giving out some appreciation/medals, considering how many questions you ask and how many answers you get.

Try using Google for once.

I give out medals and use google, I ask questions for clarity. I know this might be hard for you to understand but as a kid I like understanding things, i am naturally curious and want to know everything. this is tough for me but I am attempting it. whats the fun of doing something easy?

I’m 27 myself, I began writing program back when Windows 3.11 was new - back then everything was hard, AND I didn’t have the internet, much less google, to help me out; just dusty books. I already explained how you need to come up with your answer, and what branch of math it is; but do not expect me to teach the entire mathematical subject to you in a forum. Also, do not get mad at me because you are trying to make waffles with a screwdriver and I’m telling you to use a spatula. I HIGHLY recommend you visit this site:

Sign up, start with basic addition/subtraction, and work your way up the tree (when you register it will give you a skill tree from basic to stupid complex types of math) until your brain starts hurting. They have amazingly well done videos for just about every subject. Since you need to get trigonometry under your belt, simply follow the skill tree and work your way up to it. It’s not going to happen in a day, but you already stated you wanted to learn… this is how.

I’m not getting mad at you I know this is something you can not teach me I was just curious if you could do this in linear algebra as that was something I had learned and I thought it could be done in that. Anyway thanks for the help i appreciate it

LA is a meta algebra. For 2D learn vectors (or better yet complex numbers) first. A little basic geometry and trig is required.

Expanding on that a bit: Linear Algebra is any and all operations you do on a vector space, but still you need some kind of basic operations on the components of the vector space for it to be meaningful, and of course you need to learn those operations. In the case of 3d graphics, the operations we’re concerned with are trigonometry.

As for complex numbers, I wish I was taught this in grade school:
http://betterexplained.com/articles/a-visual-intuitive-guide-to-imaginary-numbers/

(that site has lots of great explanatory metaphors for other things too)

To build on that…the original usefulness of complex numbers (the second algebra to be known) was the ability to avoid classic geometry and trig…in the sense the geometric and trig meaning is built-in to the algebra itself, not that you don’t need a basic understand of these topics.

You’ll understand things a lot better if you figure them out yourself, and I have no idea why this is even remotely tough for you. Like I said, you’ve been given the source, it doesn’t get much easier than that.

Right back at you. There’s no need for any trigonometry here! Just create a direction vector and normalize it to get a velocity directly. Oh, did I mention that it’s a lot faster too than using atan2() + sin() + cos()?



float dx = targetX - originX, dy = targetY - originY;
float distance = (float)Math.sqrt(dx*dx + dy*dy);

vx = dx * speed / distance;
vy = dy * speed / distance;


Now I feel stupid for just now seeing that… I use the distance formula myself a lot but never though about creating a directional vector and using it that way. Seems obvious now… There’s always room for improvement I guess 8)

I do it exactly the same as theagentd, but I use Vectors for readability :stuck_out_tongue:


// initialize:
Vec2 delta = new Vec2(targetX - originX, targetY - originY);
Vec2 velocity = delta.normalize().scale(speed);

// update:
position.add(velocity);

In the end, it’s the same:
the dx and dy from theagent’s line 1 in my line 1,
the normalize automatically does the [icode]/ distance[/icode] division and the [icode]scale[/icode] is the replacement for the [icode]* speed[/icode] in theagent’s code.

See my implementation of the Vec class.

You can also use libgdx’s Vector2’s, if you use libgdx. The method “normalize()” would then be called “nor()”, and “scale(float)” would be “mul(float)”.


s  = speed / distance;
vx = dx * s;
vy = dy * s;

And if anyone thinks “this is less accurate” then you’re doing it wrong anyway.

hey guys just being curious why cant i do this using the method i first attempted?

I think that is clear for you that we use trigonometry to do this,
(and trust me, try to learn at list this 2 function sin and cos they will change your point of view)

if u want to do with your code


float bulletX, bulletY;
float slope = (mY - sY) / (mX - sX);
bulletY = slope * (bulletX - sX) + sY;

you can do if u have the speed separate in the x component and y component:
at this point conside that your speed is expressed in pixel for seconds

if your speed at x components is vX the value of next x of the bullet is bulletX = bulletX + vX*T
where T is the time (in second) pass since last loop
so your code become

calucalte the slope the first time


float bulletX, bulletY;
float slope = (mY - sY) / (mX - sX);

and after, in any cicle do this


bulletX = bulletX + vX*T
bulletY = slope * (bulletX - sX) + sY;

remember this is valid only if sX and sY don’t change
otherways save it when u calculate the slope

You don’t even need sin or cos to know future positions of projectiles. You just need a position, and a direction in which the projectile will move.

Its easier to just use vector maths for 2D projectiles. A vector is represented exactly as a coordinate, but it represents length and direction. So, if you have a bullet at position (4, 5) and its direction is (1, 0), the bullet will move to the right by 1 coordinate every frame.

If you have a direction of (1, 1), it won’t be moving exactly 1 coordinate spot per “frame”. You need to normalize the vector, which means get the length of the vector, and divide all the components (x and y) by the length. Then you can multiply both of these values to achieve speed in the bullet.

Use something like this:


Vector2D pos = new Vector2D(4, 5);
Vector2D dir = new Vector2D(1, 1);

dir.normalize();
dir.multiply(2);

pos.add(dir);

Using slope to do stuff is kind of complicated, and I feel that vectors can be achieved to do much more. =D

Also, i’m also in 8th grade geometry 8D