h3ckboy


«
Posted
20090227 15:38:54 » 

how do I make the bullets go where I click. but at an angle. not jsut going along the x axis then the y.
thx in advance.
EDIT: this is not the only thing I would use it for, but I think that the same thing would work for all of my ideas.




Hansdampf


«
Reply #1  Posted
20090227 15:42:51 » 

1) calculate the direction 2) normalize it 3) multiply it by the speed 4) add it to to the position




h3ckboy


«
Reply #2  Posted
20090227 16:03:24 » 

how do I calculate the direction is what I am asking(sry if not direct enough).
thx for fast response.




Games published by our own members! Check 'em out!


Hansdampf


«
Reply #3  Posted
20090227 16:13:31 » 

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




h3ckboy


«
Reply #4  Posted
20090227 16:15:13 » 

is the x and y int he first formula the x distance and y distance?




cylab


«
Reply #5  Posted
20090227 16:17:15 » 

this is 2d vector math. look it up. 1) calculate the direction: substract the origin from the destination point 2) normalize it divide the result vectors components by the vectors length 3) multiply it by the speed multiply the components by the desired amounts of pixels to move per frame 4) add it to to the position add the result vector to the origin point

Mathias  I Know What [you] Did Last Summer!



h3ckboy


«
Reply #6  Posted
20090227 16:26:14 » 

It explains what a vector is thi sis the most explanation on moving down the line I get though Note that we've simply moved the point at <5,5> in the direction <4,4>. It never says how to do it though.




Wildern


«
Reply #7  Posted
20090227 16:34:06 » 





h3ckboy


«
Reply #8  Posted
20090227 16:36:53 » 

I feel. dumb. This is slopes right? cause that is waht it apears like to me.
I jsut learned this in math like 2 weeks ago. I am dumb....




h3ckboy


«
Reply #9  Posted
20090227 16:52:14 » 

how can I simplify the fraction I get.
like how doI simplify vy/vx?
cause the ships jsut fly through the screen so i cannot see what theya re doing.




Games published by our own members! Check 'em out!


Wildern


«
Reply #10  Posted
20090227 17:27:53 » 

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'
The link I posted allows you see that in real time.




damaxxed
Senior Newbie
I ♥ Prototyping


«
Reply #11  Posted
20090305 23:27:17 » 

A solution WITHOUT Vectors, I used in a 2D game of mine: 1 2 3 4 5 6 7 8 9 10
 int diffX; int diffY; double maxDistance; double distance = Math.sqrt(distX*distX + distY*distY); double goX = diffX*distance/maxDistance; double goY = diffY*distance/maxDistance; this.x += goX; this.y += goY; 




cylab


«
Reply #12  Posted
20090305 23:44:18 » 

Good work! But 1 2
 double goX = diffX*distance/maxDistance; double goY = diffY*distance/maxDistance; 
has to be 1 2
 double goX = diffX/distance*maxDistance; double goY = diffY/distance*maxDistance; 
And btw. this is what vector math would do under the hood

Mathias  I Know What [you] Did Last Summer!



DelONE
Senior Newbie


«
Reply #13  Posted
20090305 23:53:08 » 

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




DzzD


«
Reply #14  Posted
20090306 00:02:39 » 

A solution WITHOUT Vectors, I used in a 2D game of mine: 1 2 3 4
 ...
this.x += goX; this.y += goY; 
just a notice, not sure but you may have some trouble this way, double/float dont like to be integrated you should better use something like : 1 2 3 4 5 6 7 8
 ... int step ... ... ... this.x = this.x0+ goX*this.step; this.y = this.y0+ goY*this.step; this.step++; 




Eli Delventhal


«
Reply #15  Posted
20090306 01:16:51 » 

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 anglerelated. 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 upright. A vector with a value of (1,1) would specifically move towards the downleft. 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.




h3ckboy


«
Reply #16  Posted
20090306 07:38:28 » 

Wait cant you use a slope to figure out the y/x of the slope, and then go y down and x left(in am including negativews here)
and 1/1 is different than 1/1.
y+=1
is dif than
y+=1
P.S: I am in preal lol




Eli Delventhal


«
Reply #17  Posted
20090306 09:24:52 » 

You're not actually using the slope.
Slope = "rise over run" = y / x. The value of y / x. It does not have both an X and a Y component. 1 / 1 = 1. 1 / 1 = 1. Therefore the two are the same if you're talking about slope.
A vector has direction as mentioned above. For a vector, 1,1 is different from 1,1.
Just never refer to the slope again, because you're never going to use it. You're going to use vectors.




h3ckboy


«
Reply #18  Posted
20090306 13:51:22 » 

wait so is what I am doing a vector than? cause wha tI did is just have a theoretical "fraction" and then jsut have it as x and y. I have gone farther with this, and I do this: vx = vx/vy; vy = vy/vy; I am having a problem wiht this. What happens is taht if they are both negative they become positive :/. I ahve fixed that problem wiht an if, but when one if positive and one is negative it makes them both positive . thx for you help




Riven


«
Reply #19  Posted
20090306 19:31:24 » 

... thx for you help 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



Eli Delventhal


«
Reply #20  Posted
20090306 20:25:53 » 

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
 public class Vector2D { public float x; public float y;
public Vector2D(float setX, float setY) { 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
 Vector2D velocity = new Vector2D(mouse.getX()  cannon.getX(), mouse.getY()  cannon.getY());
float length = (float) Math.sqrt(velocity.x * velocity.x + velocity.y * velocity.y);
velocity.x /= length; velocity.y /= length;
velocity.x *= cannonPower; velocity.y *= cannonPower;
world.add(new Bullet(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).




h3ckboy


«
Reply #21  Posted
20090306 20:37:58 » 

Thank You! that has to be the most helpful thing any1 has ever told me! I finally get it! . I didnt realize it was so simple. I was almost doing that already. I just didnt have a seperate class for it. I am gunna try tweaking my game with the info you jsut gave me. will post back with any questions.




h3ckboy


«
Reply #22  Posted
20090306 21:02:21 » 

THANK YOU!
youa re a genious!
I feel dumb now :/.
hehe I will try to figure stuff out more, so that you guys dont ahve to help me as much.
thx.




Eli Delventhal


«
Reply #23  Posted
20090306 23:55:29 » 

Glad that helped, no problem. I figured maybe you just needed to look at some simple source code.




Sanzeinga
Senior Newbie


«
Reply #24  Posted
20090319 22:46:23 » 

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?




Eli Delventhal


«
Reply #25  Posted
20090319 23:41:20 » 

Java's very very good at cheap object management, so using Vector2 should not slow things down at all, especially in any game that one human being can possibly make on modern hardware. As for your question, go back and read more closely. You never want to use the slope. Never, ever, never. Just use a Vector2. Here's what you do for a homing missile, in pseudocode. 1 2 3 4
 Vector2 velAdjustment = missilePos  missileTargetPos; velAdjustment.normalize; velAdjustment *= missileSpeed; missileVelocity += velAdjustment; 
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.




Eli Delventhal


«
Reply #26  Posted
20090319 23:43:14 » 

You could use Math.atan2(y, x) to get an angle, then rotate the speed around that angle. Or calculate the slope (y2y1)/(x2x1) 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 nonpointed 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.




Sanzeinga
Senior Newbie


«
Reply #27  Posted
20090320 00:29:34 » 

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 )




Ranger


«
Reply #28  Posted
20090320 00:33:10 » 

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




Eli Delventhal


«
Reply #29  Posted
20090320 04:42:10 » 

It is slightly slower to no real detriment, but I was just telling the noobs to stay away from it because it was giving them the wrong idea of how they should be approaching this.




