Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (622) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1] 2
 ignore  |  Print
 Need help moving projectiles with math...  (Read 6223 times) 0 Members and 1 Guest are viewing this topic.
wreed12345

JGO Knight

Medals: 25
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Posted 2013-01-01 02:55:22 »

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
 1  2  3 `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

LunaticEdit

Senior Devvie

Medals: 8
Projects: 1

 « Reply #1 - Posted 2013-01-01 03:28:21 »

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

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

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

 1  2  3  4 `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:

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

now

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

Hopefully that's close to what you're looking for...

wreed12345

JGO Knight

Medals: 25
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Reply #2 - Posted 2013-01-01 03:31:27 »

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

LunaticEdit

Senior Devvie

Medals: 8
Projects: 1

 « Reply #3 - Posted 2013-01-01 03:32:20 »

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

wreed12345

JGO Knight

Medals: 25
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Reply #4 - Posted 2013-01-01 03:32:42 »

im only 14 and taking geometry now

Jimmt
« League of Dukes »

JGO Kernel

Medals: 159
Projects: 4
Exp: 3 years

 « Reply #5 - Posted 2013-01-01 03:50:28 »

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.

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.

wreed12345

JGO Knight

Medals: 25
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Reply #6 - Posted 2013-01-01 03:57:05 »

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?

LunaticEdit

Senior Devvie

Medals: 8
Projects: 1

 « Reply #7 - Posted 2013-01-01 04:05:38 »

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.

wreed12345

JGO Knight

Medals: 25
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Reply #8 - Posted 2013-01-01 04:08:32 »

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

Roquen
 « Reply #9 - Posted 2013-01-01 05:33:36 »

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

JGO Kernel

Medals: 202

 « Reply #10 - Posted 2013-01-01 05:37:59 »

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)
Roquen
 « Reply #11 - Posted 2013-01-01 09:13:24 »

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.
Jimmt
« League of Dukes »

JGO Kernel

Medals: 159
Projects: 4
Exp: 3 years

 « Reply #12 - Posted 2013-01-01 16:49:05 »

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

« JGO Bitwise Duke »

Medals: 432
Projects: 2
Exp: 8 years

 « Reply #13 - Posted 2013-01-01 16:53:16 »

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

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()?

 1  2  3  4  5 `float dx = targetX - originX, dy = targetY - originY;float distance = (float)Math.sqrt(dx*dx + dy*dy);vx = dx * speed / distance;vy = dy * speed / distance;`

Myomyomyo.
LunaticEdit

Senior Devvie

Medals: 8
Projects: 1

 « Reply #14 - Posted 2013-01-01 17:26:40 »

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

matheus23

JGO Kernel

Medals: 121
Projects: 3

You think about my Avatar right now!

 « Reply #15 - Posted 2013-01-01 17:40:55 »

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

 1  2  3  4  5  6 `// 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
 `/ distance`
division and the
 `scale`
is the replacement for the
 `* speed`
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)".

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Roquen
 « Reply #16 - Posted 2013-01-01 19:38:55 »

 1  2  3 `s  = speed / distance;vx = dx * s;vy = dy * s;`

And if anyone thinks "this is less accurate" then you're doing it wrong anyway.
wreed12345

JGO Knight

Medals: 25
Projects: 2
Exp: 2 years

http://linebylinecoding.blogspot.com/

 « Reply #17 - Posted 2013-01-02 00:40:24 »

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

alesky

Junior Devvie

Medals: 3
Exp: 15 years

mmm....

 « Reply #18 - Posted 2013-01-02 01:17:19 »

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

 1  2  3 `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

calucalte the slope the first time
 1  2 `float bulletX, bulletY;float slope = (mY - sY) / (mX - sX);`

and after, in any cicle do this
 1  2 `bulletX = bulletX + vX*TbulletY = slope * (bulletX - sX) + sY;`

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

Agro
 « Reply #19 - Posted 2013-01-02 01:40:31 »

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:

 1  2  3  4  5  6  7 `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

Jimmt
« League of Dukes »

JGO Kernel

Medals: 159
Projects: 4
Exp: 3 years

 « Reply #20 - Posted 2013-01-02 01:46:16 »

Took 8th grade geo last year. Hated it, algebra was easier. You'll learn basic trig at the end (probably, just sin/cos/tan=opp/hyp, adj/hyp, opp/adj, etc)
Roquen
 « Reply #21 - Posted 2013-01-02 07:12:37 »

slopes are near useless (except in specialized cases) and are only an educational step.  Consider as the delta's approach zero...BOOM.
Axeman

Senior Devvie

Medals: 7

 « Reply #22 - Posted 2013-01-02 15:02:44 »

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)

Just had to thank you for a great link! I´ll put it to good use now...
Roquen
 « Reply #23 - Posted 2013-01-02 15:37:20 »

Complex numbers can be really helpful in understand 2D transformations...the sad problem is that there are very few good explanations laying around.  Complex numbers are pretty much only talked about (for geometry) in dealing with classic (aka nasty/hairy) geometry problems and not basic common stuff...and there's a silly insistence on sticking to the absurd notion of square roots of negative numbers (which is misleading).
Axeman

Senior Devvie

Medals: 7

 « Reply #24 - Posted 2013-01-02 16:06:48 »

Roquen: I´m gonna try to read up a bit more on this and then you probably can expect a question or two in your tutorial section...
DrZoidberg

Senior Devvie

Medals: 17

 « Reply #25 - Posted 2013-01-03 01:40:58 »

 1  2  3  4  5  6  7 `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

Just thought I'd mention that from a computer science point of view, mathematical entities (e.g. Vectors, Matrices, etc) should always be represented by immutable objects except if it would hurt performance. You may notice that classes like Integer, Long, BigInteger, BigDecimal, etc. are all immutable. For simple code it doesn't make much of a difference but if you do complex vector math, mutable vectors can make the code really messy.
Riven
« League of Dukes »

« JGO Overlord »

Medals: 954
Projects: 4
Exp: 16 years

 « Reply #26 - Posted 2013-01-03 06:47:23 »

Are you suggesting to make an immutable Matrix class...? Has anyone ever done that?

And yes, it always hurts performance, which is why it would be madness to make vector classes immutable, let alone matrices.

Sun's vecmath classes are mutable, but create many objects under the hood. It may look like a great compromise but in the end it's the reason nobody uses it. It's slow and puts incredible stress on the garbage collector.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Roquen
 « Reply #27 - Posted 2013-01-03 07:00:14 »

Just thought I'd mention that from a computer science point of view, mathematical entities (e.g. Vectors, Matrices, etc) should always be represented by immutable objects ...
That's utter nonsense.  Stop listening to whomever told you that.  Beside java doesn't even have immutable object (no...it really doesn't).  The widespread usefulness of immutables took a huge blow in the 80s with the introduction of SSA form, but even before that this statement would still be nonsense.
Varkas
 « Reply #28 - Posted 2013-01-03 13:43:46 »

That's utter nonsense.  Stop listening to whomever told you that.  Beside java doesn't even have immutable object (no...it really doesn't).

Are you sure about that? I too, was told that immutable objects come with benefits and there are tutorials how to make objects immutable, e.g. this:

http://www.javapractices.com/topic/TopicAction.do?Id=29

Quote
In Effective Java, Joshua Bloch makes this recommendation :

"Classes should be immutable unless there's a very good reason to make them mutable....If a class cannot be made immutable, limit its mutability as much as possible."

But I agree, that a math matrix or vector class is better represented by a mutable entity.

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Riven
« League of Dukes »

« JGO Overlord »

Medals: 954
Projects: 4
Exp: 16 years

 « Reply #29 - Posted 2013-01-03 13:56:13 »

There's the ideal situation, in which object creation and collection is free and immutability solves many problems (mainly in multithreading), and there is this annoying requirement of games to be running at a rock solid 60Hz.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1] 2
 ignore  |  Print

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

 BurntPizza (27 views) 2015-04-23 03:42:11 theagentd (29 views) 2015-04-22 16:23:07 Riven (44 views) 2015-04-16 10:48:47 Duke0200 (52 views) 2015-04-16 01:59:01 Fairy Tailz (38 views) 2015-04-14 20:13:12 Riven (41 views) 2015-04-12 21:36:37 bus hotdog (58 views) 2015-04-10 02:39:32 CopyableCougar4 (61 views) 2015-04-10 00:51:04 BurntPizza (60 views) 2015-04-06 22:06:58 ags1 (62 views) 2015-04-02 10:58:48
 theagentd 30x BurntPizza 16x wessles 15x 65K 11x Rayvolution 11x alwex 11x kingroka123 11x KevinWorkman 9x kevglass 8x phu004 8x Roquen 7x chrislo27 7x Hanksha 7x Riven 7x Olo 7x ra4king 7x
 How 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:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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