Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  A little Math  (Read 750 times)
0 Members and 2 Guests are viewing this topic.
Offline ~Spaceaholic

JGO n00b
*

Posts: 35
Medals: 1



« on: 2011-11-10 09:37:59 »

I am trying to get a bunch of objects to interact with eachother in such a way that a collection of objects will be repelled from a user controlled object. each individual object should be repelled along the slope between itself and the user object, using the objects centers to determine the slope. The speed at which the object is repelled should be a constant, regardless of the slope.

each object has
float xspeed
float yspeed
float addedxspeed
float addedyspeed

xspeed and yspeed are constant for the lifetime of an object but the added speeds can be changed and are acted upon by a friction constant.
the coordinate plane that I am working with starts with x=0 growing left to right and y=0 growing top to bottom.
I would like to know how to calculate what my addedspeeds should be in order to make the object travel away from the user at a set speed, lets say 3.

if it helps at all to know my objects represent fish and the user is trying to eat them, the fish get scared, and swim away from the user..
Thanks for your help, if any more information is needed I will gladly repost....
Offline ~Spaceaholic

JGO n00b
*

Posts: 35
Medals: 1



« Reply #1 on: 2011-11-10 09:59:01 »

The process of trying to explain the problem I was having seems to have helped me work through it...

float distance = (float)Math.sqrt(Math.pow(userFish.x - fish2.x, 2) + Math.pow(userFish.y - fish2.y, 2));

float diffX = userFish.x - fish2.x;
diffX = diffX * (3/distance);
                           
float diffY = userFish.y - fish2.y;
diffY = diffY * (3/distance);
                           
fish2.addedVelocityX = diffX;
fish2.addedVelocityY = diffY;

I believe this solved my problem...
Offline theagentd

JGO Wizard
****

Posts: 1386
Medals: 88



« Reply #2 on: 2011-11-10 13:02:56 »

You really need to sort out your variable naming. Change in velocity is called acceleration, you know...

There is no god.
Games published by our own members! Go get 'em!
Offline ~Spaceaholic

JGO n00b
*

Posts: 35
Medals: 1



« Reply #3 on: 2011-11-10 13:56:14 »

If you think this is bad you should see the code from when I started, for some odd reason when I am writing code variable names just don't come to me. but it is all in the works, getting better every day...
Offline theagentd

JGO Wizard
****

Posts: 1386
Medals: 88



« Reply #4 on: 2011-11-10 23:19:14 »

Ah, I see what you're doing now. Don't use pow(x, 2), it is dead slow. Instead calculate diffX = (x1 - x2) and diffY = (y1 - y2) before the distance calculation, and manually square them by writing diffX*diffX +  diffY*diffY. You can also then reuse this value later.
1  
2  
3  
4  
5  
float dx = userFish.x - fish2.x;
float dy = userFish.y - fish2.y;
float distance = (float)Math.sqrt(dx*dx + dy*dy);
fish2.addedVelocityX = dx * 3 / distance;
fish2.addedVelocityY = dy * 3 / distance;

This code does the exact same thing and is a looooot faster.

There is no god.
Offline Kurten

Jr. Member
**

Posts: 75
Medals: 2


-w-


« Reply #5 on: 2011-11-11 03:28:32 »

If you think this is bad you should see the code from when I started, for some odd reason when I am writing code variable names just don't come to me. but it is all in the works, getting better every day...

This is non-relevant to the original question, but one good way to write good variable names is to use mathematical names for them.

pointX / posX //Point is a point in the coordinate grid, therefore a coordinate.
pointY / posY

velX //velocity is from what i have understood the same thing as speed
velY

accX //acceleration is change in velocity if i remember correctly
accY

That's my way of naming my variables Smiley
Offline ReBirth

JGO Wizard
****

Posts: 1266
Medals: 19



« Reply #6 on: 2011-11-12 00:19:18 »

Mine more simple

position: X, Y
Velocity: dX, dY
Acceleration: dDY, dDX

Grin

btw, why use float instead double?

Offline sproingie

JGO Strike Force
***

Posts: 892
Medals: 55



« Reply #7 on: 2011-11-12 00:53:56 »

Floats are a force of habit for OpenGL programmers, because that's all most any GPU handles.  Modern cards do doubles but often much slower.
Offline theagentd

JGO Wizard
****

Posts: 1386
Medals: 88



« Reply #8 on: 2011-11-12 03:18:07 »

And do you really need that good precision? I mean, for a physics simulation that will run for a long time or that just needs to be precise, sure, but for graphics?

There is no god.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.1 seconds with 19 queries.