Vector math deals with slopes somewhat.If you are adding two vectors, you start at 0,0 and draw a line with the slope of the direction of the vector and a length of the magnitude of the vector (think direction and speed) That will end at a point x,y. Draw the second vector in the same manner with the point x,y as its starting point. This will end at a new point x',y'. Your new vector that is the sum of the other two is the vector from 0,0 to x',y'

h3ckboy, I don't know how old you are, but it would be a good idea to start looking into the basics of trigonometry and kinematics. Vectors and motion are vital to game development, so understanding these things will prove immensely useful; besides, it cant hurt to get ahead

A Vector is just a direction of a certain length (have I already explained this recently?). A unit vector is what you get when you normalize any vector - it has a total length of 1, and therefore can be considered of only having a direction. So just like if you're only going along 1 dimension, you could either being going right (1) or left (-1). Then if you multiply that my your speed, you'll get something moving right or left as far as you wanted it to go.

A slope is not even directional, it is angle-related. This is because a slope determines a line, and a vector is more like a ray or line segment except it can exist anywhere in space. A vector simply denotes a single movement, but it doesn't denote where. A vector with a value of (1,1) would specifically move towards the up-right. A vector with a value of (-1,-1) would specifically move towards the down-left. A slope of (1,1) and a slope of (-1,-1) are equivalent, however, because a slope is really a single number which is y/x, and 1/1 == -1/-1 == 1.

So, let's review.

Vectors- Denote a single movement of a given direction and a given distance.- Are completely position independent.- If they have a length of 1, they are a unit vector and therefore only denote direction.- Can be in any number of dimensions, from 1D to infinityD, although typically will be in 2D or 3D, or 4D when they're usually used as Quaternions.- Are necessary for anything movement related in a game.

Slopes- Denote an angle for a line within coordinate space.- Are equivalent to the tangent of the angle from the X axis.- Have no length or distance. The greater the value, the steeper, the lesser the value, the flatter.- Can only be in 2D (although I guess you could figure out some equivalent for 3D).- Are pretty much useless to game development.

My serious advice is that to get the best help, you should read all the replies in your threads regarding this problem, very carefully.

It might sound lame, but we've answered all your questions. You just haven't realized it. There is not much else to explain for us (unless anybody else has an idea).

You might want to read a few articles on what vectors are, and start from scratch with your code. Any code that handles your 'slopes' will only waste your time. There is nothing to improve there, just replace it with vector code - once you have a sound understanding of them - they are not that hard to grasp.

I hope you don't feel insulted (or otherwise misthreated) by this post - it is certainly not my intension.

Keep up the spirit, burn your ships (and read a few articles )

Hi, appreciate more people! Σ ♥ = ¾Learn how to award medals... and work your way up the social rankings!

I think Riven is right, but let me try to be more clear one last time.

A slope in the context of a game will essentially give you an angle. It does not have both X and Y, it is a single value. In order to handle movement on a 2D plane (which is what you have), you need something that has both an X and a Y. In other words, a vector.

Although we've mentioned all these things you can do with vectors, the actual vector itself is very very very simple.

1 2 3 4 5 6 7 8 9 10 11

publicclassVector2D{publicfloatx;publicfloaty;

publicVector2D(floatsetX, floatsetY) {x = setX;y = setY; }}

Seriously, that's it. Anything else in a vector class has nothing to do with the actual structure of a vector, instead it has to do with different things you can do with it. Just having these two floats, you can represent absolutely any movement in 2D space, be it left, right, up, down, or in between. And it can cross half a screen or 5000 screens. It's very simple but can do absolutely anything you want in terms of movement.

So. You create your vector initially with the positions of the mouse and the cannon.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

//This will give you a vector that points FROM the cannon TO the mouse. If you subtracted them in reverse order,//the vector would point in the opposite direction.Vector2Dvelocity = newVector2D(mouse.getX() - cannon.getX(), mouse.getY() - cannon.getY());

//Use the pythagorean theorem to get the length of the vector.floatlength = (float) Math.sqrt(velocity.x * velocity.x + velocity.y * velocity.y);

//Normalize the vector (making it have a total length of 1, turning it into a unit vector) using the length we found.velocity.x /= length;velocity.y /= length;

//Multiply the unit vector by the power of the cannon, so the more power there is, the further it flies.velocity.x *= cannonPower;velocity.y *= cannonPower;

//Finally, create a bullet with the initial velocity of what we just computed.world.add(newBullet(cannon.x, cannon.y, velocity));

That's as much as anyone can baby you. I'll answer any questions you specifically have about the code mentioned above, but I won't explain vectors any further (and I doubt anyone else will, either).

Actually, i just ran into this. Until now i've been keeping track of stuff via things like double x, double y, etc. Would i take a noticeable hit by using Point2d and Vector2ds instead? It seems like a much nicer way to keep track of things but there's going to be lots of projectiles on screen and i don't want to unnecessarily slow things down (this is to be a VERY VERY fast paced game). Also the solution to his question answered mine before i asked, but also if i wanted actual HOMING shots like a self guided missile ( as opposed to a automatically targeted turret shot like what the parent post seems to be talking about ) that would follow an evasive target what should i do? ( with limited turn angles that i can set [how would i do that?] )

"You could use Math.atan2(y, x) to get an angle, then rotate the speed around that angle." Is it related to this?

A homing missile is exactly the same as a ballistic missile except that it can constantly adjust its velocity. A ballistic missile only gets fired once using a given velocity, then stays on that course forever.

You could use Math.atan2(y, x) to get an angle, then rotate the speed around that angle.Or calculate the slope (y2-y1)/(x2-x1) and normalze that - beware of division by zero.

Also, for anyone else reading this thread, ignore this post (with all respect to Hansdampf, because I think he was just answering a non-pointed question). I don't think it's at all a good direction to be thinking. You don't want to use atan at all and you never want to use the slope at all. Use vectors.

ah right... that makes senses lol thanks a bunch ( for some reason i realized i could make a laser rigidly bend towards the enemy by redoing that tracking caluclation halfway through it's life but i didn't notice that you just repeatedly do that for full homing )

You don't want to use atan at all and you never want to use the slope at all. Use vectors.

Just curious why slopes are so bad? Is calling sin/cos/atan slow? Or is it just bad practice?

My current code is this:

1 2

x += Math.sin(direction) * speed;y -= Math.cos(direction) * speed;

The reason I did it this way was because it is really easy to modify the direction and speed based on the user keyboard input (just increase/decrease them). Would you still recommend using vectors in this situation?

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