wreed12345


«
Posted
20130101 02:55:22 » 

Ok so i felt like i should know how to do this using the formula yy1 = m(xx1) 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


«
Reply #1  Posted
20130101 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


«
Reply #2  Posted
20130101 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?




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


LunaticEdit


«
Reply #3  Posted
20130101 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


«
Reply #4  Posted
20130101 03:32:42 » 

im only 14 and taking geometry now




Jimmt


«
Reply #5  Posted
20130101 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. Try using Google for once.




wreed12345


«
Reply #6  Posted
20130101 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


«
Reply #7  Posted
20130101 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: http://www.khanacademy.orgSign 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


«
Reply #8  Posted
20130101 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
20130101 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.




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


sproingie


«
Reply #10  Posted
20130101 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/avisualintuitiveguidetoimaginarynumbers/(that site has lots of great explanatory metaphors for other things too)




Roquen


«
Reply #11  Posted
20130101 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 builtin to the algebra itself, not that you don't need a basic understand of these topics.




Jimmt


«
Reply #12  Posted
20130101 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


«
Reply #13  Posted
20130101 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


«
Reply #14  Posted
20130101 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


«
Reply #15  Posted
20130101 17:40:55 » 

I do it exactly the same as theagentd, but I use Vectors for readability 1 2 3 4 5 6
 Vec2 delta = new Vec2(targetX  originX, targetY  originY); Vec2 velocity = delta.normalize().scale(speed);
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 division and the is the replacement for the 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)".




Roquen


«
Reply #16  Posted
20130101 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


«
Reply #17  Posted
20130102 00:40:24 » 

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




alesky


«
Reply #18  Posted
20130102 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 so your code become 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*T bulletY = 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
20130102 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


«
Reply #20  Posted
20130102 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
20130102 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


«
Reply #22  Posted
20130102 15:02:44 » 

Just had to thank you for a great link! I´ll put it to good use now...




Roquen


«
Reply #23  Posted
20130102 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


«
Reply #24  Posted
20130102 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


«
Reply #25  Posted
20130103 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


«
Reply #26  Posted
20130103 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



Roquen


«
Reply #27  Posted
20130103 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
20130103 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=29In 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.




Riven


«
Reply #29  Posted
20130103 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



