Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 A little Math  (Read 1452 times) 0 Members and 1 Guest are viewing this topic.
~Spaceaholic

Senior Devvie

Medals: 3
Projects: 1

 « Posted 2011-11-10 14: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

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..
~Spaceaholic

Senior Devvie

Medals: 3
Projects: 1

 « Reply #1 - Posted 2011-11-10 14: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);

I believe this solved my problem...
theagentd

« JGO Bitwise Duke »

Medals: 432
Projects: 2
Exp: 8 years

 « Reply #2 - Posted 2011-11-10 18:02:56 »

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

Myomyomyo.
~Spaceaholic

Senior Devvie

Medals: 3
Projects: 1

 « Reply #3 - Posted 2011-11-10 18: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...
theagentd

« JGO Bitwise Duke »

Medals: 432
Projects: 2
Exp: 8 years

 « Reply #4 - Posted 2011-11-11 04: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.

Myomyomyo.
Kurten

Junior Devvie

Medals: 3
Exp: 3 years

-w-

 « Reply #5 - Posted 2011-11-11 08: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
ReBirth
 « Reply #6 - Posted 2011-11-12 05:19:18 »

Mine more simple

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

btw, why use float instead double?

sproingie

JGO Kernel

Medals: 202

 « Reply #7 - Posted 2011-11-12 05: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.
theagentd

« JGO Bitwise Duke »

Medals: 432
Projects: 2
Exp: 8 years

 « Reply #8 - Posted 2011-11-12 08: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?

Myomyomyo.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 BurntPizza (30 views) 2015-04-23 03:42:11 theagentd (35 views) 2015-04-22 16:23:07 Riven (49 views) 2015-04-16 10:48:47 Duke0200 (59 views) 2015-04-16 01:59:01 Fairy Tailz (42 views) 2015-04-14 20:13:12 Riven (44 views) 2015-04-12 21:36:37 bus hotdog (60 views) 2015-04-10 02:39:32 CopyableCougar4 (66 views) 2015-04-10 00:51:04 BurntPizza (70 views) 2015-04-06 22:06:58 ags1 (69 views) 2015-04-02 10:58:48
 theagentd 23x BurntPizza 17x wessles 15x kingroka123 11x alwex 11x 65K 11x kevglass 8x Rayvolution 7x Roquen 7x Riven 7x chrislo27 7x Hanksha 7x Olo 7x Ecumene 7x ra4king 7x KevinWorkman 6x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org