Math/coding problem

Yep, in the Vector class. I'm using another library right now which has other vector operations, and I'm using a class called Vector2f which has these operations on a vector with floats as its subunits.




EDIT: Question, how do I find L and Lhat without knowing I? Should I derive Lhat by normalizing the current laser, before I make it shorter?

Trying out teta_bonita's solution with the assumption that L is actually the length of the laser before the collision, and that Lhat is this vector normalized, I got pretty much exactly the same results as I did with Dan's solution.

Depending on the angle I'm at, the laser will either fire to a point in space which is visible but nowhere near the edge of the circle, will fire to the opposite edge of the circle, or will fire at a random point in distant space.
 
Oh wait, can't you use the triangle rule of vector addition to find L when you know the radius of the asteroid and hte distance from the center of the asteroid to the ship?
 
Screw this, I'll work on it later. I'll start implementing missiles instead:
(A shot of a test while implementing the missile class and making the chain gun fire missiles at a ridiculous rate, killing my machine and crashing the game)

missileski9.jpg
 
r/sin (S) = L/sin (A) = D/sin (I)

Law of sines set up... Angles are named by the points.

r/sin (theta) = L/ sin (A) = D/sin (I)

We know theta, (angle the ship is firing minus the angle between ship and circle) we know D, we know R.

So we have one side of the equation.

Use that to find sin(I).

Since we have two angles we can figure out the third.

Substitute that back in to find L.

We now know the length of the laser... Is that enough?
 
how do I find L and Lhat without knowing I? Should I derive Lhat by normalizing the current laser, before I make it shorter?

Okay, you're trying to find I, and you do that by finding L (the vector displacement between I and S) and adding L to S. You can find L by multiplying L-hat by what the length of L should be.

L-hat is a normalized vector; you can get it from the angle your ship is facing. I don't know how you're storing it, but you need it to fire the laser. It's basically the direction of your laser (wich I'm guessing is the same direction your ship is facing). If you're storing the orientation of your ship as an angle (let's call it theta), then L-hat would be <cos(theta), sin(theta)>.
 
Teta, does my solution not work? All he needs is the Length of L to cut the laser off, right? Seems much simpler than yours.
 
Okay, you're trying to find I, and you do that by finding L (the vector displacement between I and S) and adding L to S. You can find L by multiplying L-hat by what the length of L should be.

L-hat is a normalized vector; you can get it from the angle your ship is facing. I don't know how you're storing it, but you need it to fire the laser. It's basically the direction of your laser (wich I'm guessing is the same direction your ship is facing). If you're storing the orientation of your ship as an angle (let's call it theta), then L-hat would be <cos(theta), sin(theta)>.

That's the problem though, I don't know the vector displacement between I and S, because I don't know I. I therefore do not know what the length L should be.
 
That's the problem though, I don't know the vector displacement between I and S, because I don't know I. I therefore do not know what the length L should be.

Sorry, I wrote the formula wrong the first time :bonce:. (one of the L-hats was an L instead).

Here's the fixed pic:

astroid_problem-1.png


The part in the formula that L-hat gets multiplied by is the length that L should be (the stuff in the parenthesis).

Sorry vegeta, I'll check out your solution in a sec...

Edit:

Yeah, I see what you're saying... that should work. It's a lot simpler to understand, but maybe not as easy to apply with programming because of the sines and arcsines and stuff.
 
Probably not, I'd rather have a nice single formula like yours :p
 
Yeah, I see what you're saying... that should work. It's a lot simpler to understand, but maybe not as easy to apply with programming because of the sines and arcsines and stuff.

Sines and arcsines are very easy to apply in programming languages. Unless I don't understand what you mean by not easy.
 
Sines and arcsines are very easy to apply in programming languages. Unless I don't understand what you mean by not easy.

Well for one thing the trigonometric functions take more processing power.... but that's not important unless you're doing thousands of them of course. I think the real trouble starts when you do arcsines and arccosines and add angles to the result. For example, the computer might define the arcsine's range between -180 and 180, but you want an angle between 0 and 360. Then you have to fiddle with it to get things to work. Or for a problem with two or more solutions, trig might make one of the solutions dissapear. And that could be the solution you want.

I don't know if it will would be a problem in this application, but trigenometric solutions have frequently caused me more headaches than they're worth. Maybe I just suck at trigenometry. :cheese:
 
Sorry, I wrote the formula wrong the first time :bonce:. (one of the L-hats was an L instead).

Okay so where you have Lhat( , are you implying a scalar multiply or a dot product?


Blah, still doesn't work.

Here is my code implementation:

Vector2f S = line.getStart(); //S equals coordinates of player

Vector2f A = new Vector2f(circle.getX(),circle.getY()); // A equals coordinates of asteroid

Vector2f I; //The intersect point

Vector2f L=line.getEnd(); //L=coordinates of the laser's current endpoint

Vector2f D = A;
D.sub(S); //D=A-S

float r = circle.getRadius(); //Radius of asteroid

Vector2f Lhat = line.getEnd();
Lhat.normalise(); //Lhat is the normal of the laser

I=S;

Lhat.scale((float)((D.dot(Lhat) - Math.sqrt(r*r+(D.dot(Lhat)*D.dot(Lhat))-D.dot(D))))); //General equation

I.add(Lhat); //I=S+equation

return new vector(I.getX(),I.getY()); //Return coordinates
 
So line.getStart() is the position of your ship, and I'm guessing line.getEnd() is the position of the player's cursor? (where you're aiming). If I guessed correctly, then you should get Lhat like this:

Vector2f Lhat = line.getEnd();
Lhat.sub( S );
Lhat.normalise();

Lhat is the direction vector that points from S towards where the laser is firing, so if line.GetEnd() is where the laser is firing, subtracting S from that and normalizing it should give you the vector you need.

Besides that, your code looks good. You probably want to check the value r*r+(D.dot(Lhat)*D.dot(Lhat))-D.dot(D)))) though to make sure it's greater than 0 first, otherwise there is not hit position and all solutions are imaginary.
 
I'm feeling kind of butthurt here. What exactly was wrong with my solution? Like Vegeta said, all you need to find out is the length of the laser because you already know the angle of it, then you can work out the intersect point too. Would it be too hard to implement because of those problems Teta mentioned?
 
I FIGURED IT OUT!!!

Well, first off, I fixed Lhat using Teta_bonita's data, and it still didn't work.

I spent time debugging every step of the statement, and I was most concerned with what was under the square root sign.

Apparently, almost everything was negative, unless I fired the laser at a specific spot on the top left hand corner of the asteroid.

I put an absolute value sign, just to be safe. This caused my lasers to intersect in a perfect circle around the spot just to the top left of the asteroid.

Then, it hit me. In Java, circle's X and Y coordinates represent the top left corner of the circle.

So I offset the circle's coordinates by the radius of the circle, and it works perfectly! Dan's solution, when this is added, also works perfectly.

Thanks guys!!
 
I FIGURED IT OUT!!!

Well, first off, I fixed Lhat using Teta_bonita's data, and it still didn't work.

I spent time debugging every step of the statement, and I was most concerned with what was under the square root sign.

Apparently, almost everything was negative, unless I fired the laser at a specific spot on the top left hand corner of the asteroid.

I put an absolute value sign, just to be safe. This caused my lasers to intersect in a perfect circle around the spot just to the top left of the asteroid.

Then, it hit me. In Java, circle's X and Y coordinates represent the top left corner of the circle.

So I offset the circle's coordinates by the radius of the circle, and it works perfectly! Dan's solution, when this is added, also works perfectly.

Thanks guys!!

Oh nice. Yeah in many languages it seems that the default origin is the top left of the image at least when it comes to sprites and stuff, unless specified differently otherwise. That's why when I want to set the origin as the center of a sprite that is almost the same all around, I just use the sprite width divided by 2, and the sprite height divided by 2, since often, at least in C# the width represents the far right of the object, and the height represents the bottom.
 
its looking pretty spiffy , i love the particle system you've implemented. btw have you looked into the seperation axis theorem ? it would give you more accurate collisions and it doesn't use expensive processes like square roots and trig functions.
 
Very cool! :)

What's the cause of the low FPS though?
 
Very cool! :)

What's the cause of the low FPS though?

1. Frame capture software can only capture at 30 FPS
2. Frame capture software eats up a lot of memory and CPU time
3. My code is poorly optimized. :(
 
What OpenGL API do you use for Java?

Not that I can help you with optimizing your code, just curious really :)
 
great work , i love the graphics.
it looks like you've sunk quite a few hours into this . when is it being released ? .
 
That is looking exellent, very impressive stuff.
 
Back
Top