h3ckboy
|
 |
«
Posted
2009-02-27 18:22:04 » |
|
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.
|
|
|
|
pjt33
|
 |
«
Reply #1 - Posted
2009-02-27 18:59:12 » |
|
Do you mean actually simplifying fractions (divide numerator and denominator by their GCD) or rounding?
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Riven
|
 |
«
Reply #3 - Posted
2009-02-27 20:00:05 » |
|
I think he want's sourcecode  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| 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); } } |
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
h3ckboy
|
 |
«
Reply #4 - Posted
2009-02-27 21:29:03 » |
|
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.
|
|
|
|
pjt33
|
 |
«
Reply #5 - Posted
2009-02-28 01:04:30 » |
|
I jsut want to simplify it.
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?
|
|
|
|
Wildern
|
 |
«
Reply #6 - Posted
2009-02-28 01:23:28 » |
|
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.
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.
|
|
|
|
h3ckboy
|
 |
«
Reply #7 - Posted
2009-02-28 07:34:14 » |
|
pjt33 is right.
I mean reagular like 3rd grade math simplifying of fractions.
for example: 2/6 = 1/3.
|
|
|
|
h3ckboy
|
 |
«
Reply #8 - Posted
2009-02-28 08:20:25 » |
|
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.
|
|
|
|
Hansdampf
|
 |
«
Reply #9 - Posted
2009-02-28 08:37:09 » |
|
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.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
h3ckboy
|
 |
«
Reply #10 - Posted
2009-02-28 10:37:03 » |
|
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?
|
|
|
|
Hansdampf
|
 |
«
Reply #11 - Posted
2009-02-28 10:42:04 » |
|
is it ok that x and y for me are integers?
No. Cast them to double or float.
|
|
|
|
h3ckboy
|
 |
«
Reply #12 - Posted
2009-02-28 10:50:06 » |
|
I dunno what oyu mean by "cast" but I will changet ehm to floats
|
|
|
|
Hansdampf
|
 |
«
Reply #13 - Posted
2009-02-28 10:55:10 » |
|
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).
|
|
|
|
h3ckboy
|
 |
«
Reply #14 - Posted
2009-02-28 10:57:22 » |
|
yeah taht is wha tI jsut changed it to  . I still get possible loss of precission.
|
|
|
|
cylab
|
 |
«
Reply #15 - Posted
2009-02-28 12:35:27 » |
|
1
| 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)...
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
Riven
|
 |
«
Reply #16 - Posted
2009-02-28 12:36:31 » |
|
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.
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
h3ckboy
|
 |
«
Reply #17 - Posted
2009-02-28 13:08:32 » |
|
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  .
|
|
|
|
cylab
|
 |
«
Reply #18 - Posted
2009-02-28 13:12:55 » |
|
P.S: it is called the Newless Clubies for a reason  . 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 
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
h3ckboy
|
 |
«
Reply #19 - Posted
2009-02-28 13:16:51 » |
|
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.
|
|
|
|
cylab
|
 |
«
Reply #20 - Posted
2009-02-28 13:22:37 » |
|
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.
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.
It seems that you have a lot of energy, which is Good(tm), but you also seem to be sloppy, impatient and hurried - which is standing in your way. That might be the reason you don't get things from abstract explanations. Take a step back and relax. Try to analyse formulars/given code step by step and try to understand the single steps. Don't get impacient if you don't get it right away (nobody does). Just take your time (or get your hands on some Ritalin  )
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
h3ckboy
|
 |
«
Reply #21 - Posted
2009-02-28 14:12:00 » |
|
thx for the advice. I will take hte advice and try and figure out the source code from riven  . will post back wiht my analysis.
|
|
|
|
Riven
|
 |
«
Reply #22 - Posted
2009-02-28 14:19:41 » |
|
I'd actually advise against analyzing my sourcecode.
Read that darn book first! If something is not clear, do no skip the page, ever! Because the learning curve is only UP, and every following page assumes you grasped the previous.
It's all about discipline.
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
h3ckboy
|
 |
«
Reply #23 - Posted
2009-02-28 14:22:56 » |
|
I didnt say I skipped stuff. I said I took like 2 days on each little topic.
|
|
|
|
h3ckboy
|
 |
«
Reply #24 - Posted
2009-02-28 15:00:06 » |
|
Ok I skimmed through it to see wha tI had read before. I dont think that I had finished it.
I have covered all that the book has to offer though, through my scowering of th internet.
haha I just learned something. It will acutally fix a prolem Ihad in a game. I didnt konw how to make something gloabal to all instances. static variables. taht saved me tons of hassle.
thx for the badgering on my crappy skills, you saved me a lot of work.
|
|
|
|
Riven
|
 |
«
Reply #25 - Posted
2009-02-28 15:32:54 » |
|
Don't skim too. And if you think you absorbed everything in the book, you might want to reread it, or buy a better book.
Besides that, the static keyword is easily abused.
If you design things properly, you might only use static in the main method, and the slingleton pattern. Steer clear of static as much as possible, it'll bite you in the long run.
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
h3ckboy
|
 |
«
Reply #26 - Posted
2009-02-28 16:00:29 » |
|
is it ok if I make my score, level, and shield static?
|
|
|
|
Riven
|
 |
«
Reply #27 - Posted
2009-02-28 16:08:40 » |
|
What if you introduce more 'units' with shields...
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
h3ckboy
|
 |
«
Reply #28 - Posted
2009-02-28 16:45:09 » |
|
my game is at the end of its development. and If I add more units it will still work. cause the shield I am referring to is the player shield. check out ym game: http://www.java-gaming.org/topics/space-invaders-again-wait-it-is-different/19826/view.htmlThis may enlighten you on what I mean. Oh eyah taht version does not have the static variables. cause I am trying to make levels. An older version of the game iwch was made with java 2d jsut re-initiated the game. but it had the gameloop so I could control it better. so I am going ot init the state (or game I am not sure wich it is doing I know it gets to the right place). The init(container) works when I am going to the menu. but it doesnt work when I am going to the next level.
|
|
|
|
Eli Delventhal
|
 |
«
Reply #29 - Posted
2009-03-01 01:42:20 » |
|
This has already been explained to you, but typically when you want to handle movement you want to normalize your movement vector, which in the end produces a vector of length 1. That means, in the end, that it has direction only, but no magnitude.
You can think of it like a 1 dimensional vector. The only directions it can have are left or right, which would be represented by 1 and -1. Then no matter what you multiply by it, you end up getting the same direction, but it has the length of the multiple. Like multiplying it by 100 creates a vector going either left or right at that distance of 100. Dividing by 100 makes something going left or right that goes only 1/100 distance.
Typically you want whatever you're moving to have a speed which is a one dimensional decimal value. Then you get the direction it should move by getting a normalized vector, and you multiply that direction by the speed.
|
|
|
|
|