Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (592) Games in Android Showcase (168) games submitted by our members Games in WIP (645) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1] 2
 ignore  |  Print
 aiming? could call it homing missiled I guess  (Read 5774 times) 0 Members and 1 Guest are viewing this topic.
h3ckboy

JGO Coder

Medals: 5

 « Posted 2009-02-27 14: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.

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

Senior Devvie

Projects: 3

too offending?

 « Reply #1 - Posted 2009-02-27 14:42:51 »

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

lots of sillystupid games: http://www.emaggame.com
h3ckboy

JGO Coder

Medals: 5

 « Reply #2 - Posted 2009-02-27 15:03:24 »

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

thx for fast response.
Hansdampf

Senior Devvie

Projects: 3

too offending?

 « Reply #3 - Posted 2009-02-27 15:13:31 »

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.

lots of sillystupid games: http://www.emaggame.com
h3ckboy

JGO Coder

Medals: 5

 « Reply #4 - Posted 2009-02-27 15:15:13 »

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

JGO Wizard

Medals: 85

 « Reply #5 - Posted 2009-02-27 15: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

JGO Coder

Medals: 5

 « Reply #6 - Posted 2009-02-27 15:26:14 »

It explains what a vector is

thi sis the most explanation on moving down the line I get though

Quote
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

Junior Devvie

 « Reply #7 - Posted 2009-02-27 15:34:06 »

Here is a good interactive on 2D vector math.
h3ckboy

JGO Coder

Medals: 5

 « Reply #8 - Posted 2009-02-27 15: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

JGO Coder

Medals: 5

 « Reply #9 - Posted 2009-02-27 15: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.
Wildern

Junior Devvie

 « Reply #10 - Posted 2009-02-27 16: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 2009-03-05 22: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; //the distance in X-direction from the bullet to the enemyint diffY; //the distance in Y-directiondouble maxDistance; //the maximum air-line way the bullet is allowed to travel in this frame (e.g. timedelta * 100 pixel/millisec)double distance = Math.sqrt(distX*distX + distY*distY); //the exact, air-line distance (calculated with Pythagoras' theorem)double goX = diffX*distance/maxDistance; //the x-way which the bullet travels in this framdouble goY = diffY*distance/maxDistance; //the y-waythis.x += goX;this.y += goY;`

cylab

JGO Wizard

Medals: 85

 « Reply #12 - Posted 2009-03-05 22:44:18 »

Good work!

But
 1  2 `double goX = diffX*distance/maxDistance; //the x-way which the bullet travels in this framdouble goY = diffY*distance/maxDistance; //the y-way`

has to be
 1  2 `double goX = diffX/distance*maxDistance; //the x-way which the bullet travels in this framedouble goY = diffY/distance*maxDistance; //the y-way`

And btw. this is what vector math would do under the hood

Mathias - I Know What [you] Did Last Summer!
Del-ONE

Senior Newbie

 « Reply #13 - Posted 2009-03-05 22: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 2009-03-05 23: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

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #15 - Posted 2009-03-06 00: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 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.

See my work:
OTC Software
h3ckboy

JGO Coder

Medals: 5

 « Reply #16 - Posted 2009-03-06 06: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 pre-al lol
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #17 - Posted 2009-03-06 08: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.

See my work:
OTC Software
h3ckboy

JGO Coder

Medals: 5

 « Reply #18 - Posted 2009-03-06 12: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
« League of Dukes »

« JGO Overlord »

Medals: 1000
Projects: 4
Exp: 16 years

 « Reply #19 - Posted 2009-03-06 18:31:24 »

...

thx for you help

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

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #20 - Posted 2009-03-06 19: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 `//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.Vector2D velocity = new Vector2D(mouse.getX() - cannon.getX(), mouse.getY() - cannon.getY());//Use the pythagorean theorem to get the length of the vector.float length = (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(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).

See my work:
OTC Software
h3ckboy

JGO Coder

Medals: 5

 « Reply #21 - Posted 2009-03-06 19: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

JGO Coder

Medals: 5

 « Reply #22 - Posted 2009-03-06 20: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

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #23 - Posted 2009-03-06 22:55:29 »

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

See my work:
OTC Software
Sanzeinga

Senior Newbie

 « Reply #24 - Posted 2009-03-19 21: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

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #25 - Posted 2009-03-19 22: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 pseudo-code.
 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.

See my work:
OTC Software
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #26 - Posted 2009-03-19 22:43:14 »

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.

See my work:
OTC Software
Sanzeinga

Senior Newbie

 « Reply #27 - Posted 2009-03-19 23: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 2009-03-19 23: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

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #29 - Posted 2009-03-20 03: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.

See my work:
OTC Software
Pages: [1] 2
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Za\'Anzabar (15 views) 2015-06-29 05:44:54 TritonDreyja (31 views) 2015-06-24 17:10:40 CopyableCougar4 (27 views) 2015-06-23 00:34:45 BurntPizza (32 views) 2015-06-21 20:36:46 cookiecompiler (74 views) 2015-06-11 15:42:53 cookiecompiler (38 views) 2015-06-11 15:41:14 NegativeZero (63 views) 2015-06-11 09:49:18 Fairy Tailz (88 views) 2015-06-11 01:59:47 chrislo27 (60 views) 2015-06-06 18:12:44 Burnsalan20 (72 views) 2015-06-05 03:00:51
 princec 35x Riven 19x BurntPizza 19x theagentd 19x nsigma 18x opiop65 18x CopyableCougar4 17x wessles 16x EgonOlsen 15x KaiHH 13x SauronWatchesYou 12x sunburn 11x NegativeZero 10x orangepascal 10x DavidBVal 10x KevinWorkman 10x
 How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14
 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