Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  aiming? could call it homing missiled I guess  (Read 5053 times)
0 Members and 1 Guest are viewing this topic.
Offline 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.

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.
Offline 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
Offline 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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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
Offline 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?
Offline cylab

JGO Ninja


Medals: 55



« 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!
Offline 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.
Offline Wildern

Junior Devvie





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

Here is a good interactive on 2D vector math.
http://demonstrations.wolfram.com/2DVectorAddition/
Offline 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....
Offline 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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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.
Offline 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 enemy
int diffY; //the distance in Y-direction
double 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 fram
double goY = diffY*distance/maxDistance; //the y-way

this.x += goX;
this.y += goY;

Offline cylab

JGO Ninja


Medals: 55



« 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 fram
double 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 frame
double goY = diffY/distance*maxDistance; //the y-way


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

Mathias - I Know What [you] Did Last Summer!
Offline 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  Grin
Offline 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++;



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

thx for you help Smiley
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


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

...

thx for you help Smiley

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 Smiley)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline 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
Offline 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! Smiley.

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.
Offline 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.
Offline 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
Offline 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?
Offline 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
Offline 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
Offline 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 )
Offline 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.
Offline 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.  Tongue

See my work:
OTC Software
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!