Simple Physics Simulation in java not working?

At the moment I cannot run java, so I cannot test your code, but I notice a couple of things.

At the moment I cannot run java, so I cannot test your code, but I notice a couple of things: You have two independent sets of variables for (almost) the same thing: {dx, dy} and {m_velocityX, m_velocityY}. As a result, your getSpeed and setSpeed are independent, and so are getDirection and Setdirection. I cannot believe that this will work the way you expect.

Your coordinates seem a little confused (which is probably why the ball "falls" to the left). You seem to be measuring angle clockwise from the X direction, which is not standard. EDIT: Plínio Pantaleão has identified the reason for falling to the left.

Your code seems to involve simple linear motion, bouncing, gravity and friction. You should implement and test these one at a time. I suggest the motion first, then add bouncing, then friction and gravity separately (in either order) and finally combine them.

EDIT: This code for gravity is wrong: if (m_y + m_bottomBound > 400) { m_velocityY *= -0.981; // setY(400 - m_bottomBound); m_y = 400 - m_bottomBound; } First you are using 400 as a "magic number"; I can't tell what it represents. Then you are restricting the effect to a region near the top of the screen, for reasons that are not clear. At the end of the function you change m_y in a way that makes no sense.

And (maybe) worst of all, you have gravity reversing and multiplying vertical velocity, which is not how gravity works. Try this instead: // You will have to adjust this. Start with a small value and increase it until // you see an effect.

Final static double GRAVITY = 0.1; // Note that +y is down, so accelerating downward means increasing vY m_velocityY += GRAVITY.

Thanks a lot for your feedback but non of the tries giving the desired effects, every try has negative consequences, I was trying to do whatever people advised and discussed on forums. – static void main Oct 5 '10 at 22:25 @jibbylala: Sei nicht niedergeschlagen! These things take time.

What works now? Motion? Bouncing?

Let us get motion working, then bouncing, then friction or gravity. And when a step goes wrong, tell us what you see. – Beta Oct 6 '10 at 2:47 @Beta:)vielen danke, I checked the gravity code in some other example its working, not the above one but the one given in tutorial,the main concern is ball to ball intersection/collision now.

– static void main Oct 6 '10 at 4:01 @jibbylala: Good, show us your code for ball-ball collision. The physics is a little trickier than ball-wall collision. – Beta Oct 7 '10 at 13:38 ball-ball collision/intersection is left,it has to be done.

– static void main Oct 7 '10 at 21:41.

Ok, not solving the problem yet. But a few things that may help: 1 - Math. Sin/cos take angle in radians.(that may help a litle).

- AdobeBall 2 - You have to calculate gravDir for each invoke of move() since m_velocityX/Y may have change direction- AdobeBall 3 - Your main loop in runball() are taking the same ball two times and comparing to itself... Use j=i+1 in second loop 4 - You call runball() in your thread loop. So I would expect the balls to move anyway.... Not just if you call bounce(). I'm correct?

You may correct your code and try again. And post the news! :).

Yes, you are correct about point 4 :-) – static void main Oct 4 '10 at 14:00 Ah! Point 1 explains why things fall to the left! +1 – Beta Oct 4 '10 at 14:03 as far as my understanding for point 1: double gravDir = 90/57.2960285258; and for point 3 the changes not let the ball moves: for (int j=i+1; j PI/2.

Its more readable. And about point 3 - I think this code is supposed to test one ball with other (what is the point in find the distance between one ball and itself? ).

If you let the code this way, in some cases i==j and you will run this test. But I don't get the ideia of your test, so if it make sense to you, keep that way :) – Plínio Pantaleão Oct 4 '10 at 16:43 thanks, I actually don't care about the code I mean I just want two ball moving and demonstrate the physical phenomenon involved (motion, bouncing, gravity and friction,collision), that's why I adapt that code because it supposed to do that but if I could have someother code doing that I have no problem. – static void main Oct 4 '10 at 17:52.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions