It is not a traditional fraction. It is two seperate numbers.
I use a formula to get the slope. I want to simplify the slope, so that it does not jump by like 100 pixels.
yeah I have googled it.
thx in advance.
It is not a traditional fraction. It is two seperate numbers.
I use a formula to get the slope. I want to simplify the slope, so that it does not jump by like 100 pixels.
yeah I have googled it.
thx in advance.
Do you mean actually simplifying fractions (divide numerator and denominator by their GCD) or rounding?
I believe what you want to do is normalize to the unit vector which is a bit different than simplifying a fraction.
I think he want’s sourcecode :persecutioncomplex:
float speed = veryFast * (float)Math.PI;
Vec2 source = new Vec2(13, 14);
Vec2 target = new Vec2(15, 15);
Vec2 direction = target.sub(source);
Vec2 normal = direction.normalize();
Vec2 velocity = normal.mul(speed);
source = source.add(velocity);
class Vec2
{
public float x, y;
public Vec2(float x, float y)
{
this.x = x;
this.y = y;
}
public Vec2 mul(float v)
{
return new Vec2(x*v, y*v);
}
public Vec2 add(Vec2 v)
{
return new Vec2(x+v.x, y+v.y);
}
public Vec2 sub(Vec2 v)
{
return new Vec2(x-v.x, y-v.y);
}
public Vec2 normalize()
{
float len2 = x*x + y*y;
float inv = 1.0f / Math.sqrt(len2);
return new Vec2(x * inv, y * inv);
}
}
what all does that source code do? cause it has vecotrs n it. and I think that it does more than jsut simplifying.
I dont need ot know how to get a vector for simplyfying. I am pretty sure of that. I already hvae the slope. that is done. I jsut want to simplify it.
[quote=“h3ckboy,post:5,topic:33067”]
I’m still waiting for you to define “simplify”. So far there have been three guesses as to what you want: are any of them correct?
That source code provides a class for handling your vector operations… it supports adding, subtracting, multiplying and normalizing.
A useful class when working with vectors.
pjt33 is right.
I mean reagular like 3rd grade math simplifying of fractions.
for example: 2/6 = 1/3.
I think I have found a way to do it. but hte numbers sometimes still come out big. like sometimes it tis 5, another 80.
You don’t want to simplify it. What you want is to NORMALIZE it. You are wasting a lot of time. Riven posted all code you need.
edit: btw, you have your slope. Put it as ‘y’ in the code, ‘x’ is 1.
I get “possible loss of procission”
is it ok that x and y for me are integers?
I turned normalize into a void and [laced it inside my enemy class is that ok?
No. Cast them to double or float.
I dunno what oyu mean by “cast” but I will changet ehm to floats
ok, that was unclear.
do all your calculations with floats or doubles, only when you draw them to the screen you might need to cast them (temporarily) to integer if your display is discrete (pixels).
yeah taht is wha tI jsut changed it to :).
I still get possible loss of precission.
int myIntX = (int)myFloatX;
No offence, but you still need to learn a lot of the basics. Please try to find some decent basic java primer or book in your library and understand it’s contents. It would be easier for you and us.
And you should really learn and understand (2D) vector math - so many things get so much easier with vector math instead of dealing with slope functions etc.
Rivens code does (despite an irritating PI value ;)) all you are trying to do and you didn’t even noticed that, because you didn’t try to understand the code (or weren’t capable because of missing basics)…
Seriously, you need to spend some time with a good Java book. Not a tutorial, a book.
You might think you know what you’re doing in Java, as you have programmed a few tiny projects, but well, the more questions you post here, the more it becomes clear that you lack the basic knowlegde of how to program. You can’t expect to get anywhere without.
Don’t get me wrong, I’m just saying that a little investment in a book, and spending a lot of time on it, will in the end make you productive and able to get things done without needing help all the time.
I guess I have a lot to learn, I was jsut trying to figure it out on my own. casue I dont get stuff when I learn it from some1 else. cause if someone shows me a formula Idont get it. If I play with it for a while I will get teh idea.
yes I have read a BOOK(wow) before. I was learning it rivens way.
P.S: it is called the Newlass Clubies for a reason ;D.
It’s a place where more experienced programmers give advice to the new ones. So we give you the advice to read a good java book. If you already read one, I suspect it was a bad one 
I skimmed :).
reading takes too long. my problem when I was reading it was, taht whenever I learned a new thing I thouhgt of a coo thing to do tiwh it. so ther went a day or two into that then I moved ot the nect thing. so it took a while. I believe my book was java for dummies. I apparently and stil a dummy.