Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Redundancy Elimination?

Reply
Thread Tools

Redundancy Elimination?

 
 
Luc The Perverse
Guest
Posts: n/a
 
      11-14-2005
I just wrote this piece of code for moving an object given an intial
position, velocity and acceleration.

protected float X=0.0f;
protected float Y=0.0f;
protected float velX=0.0f;
protected float velY=0.0f;
protected float accX=0.0f;
protected float accY=100.0f; //gravity
protected float DesiredVelocityX=0.0f;

protected float ApplyDiscreteMovement(float Pos, float Vel, float Acc,
float Time){
return Pos + Time * Vel + Acc * Time * Time /2.0f;
}
protected float AdjustVelocity(float Vel, float Acc, float Time){
return Vel + Time * Acc;
}

public void MoveMe(float ElapsedTime){
if(accX>0){
velX = AdjustVelocity(velX, accX, ElapsedTime);
if(velX>=DesiredVelocityX){ //reached desired velocity
accX = 0.0f;
velX=DesiredVelocityX;
}
} else if(accX<0){
velX = AdjustVelocity(velX, accX, ElapsedTime);
if(velX<=DesiredVelocityX){ //reached desired velocity
accX = 0.0f;
velX=DesiredVelocityX;
}
}
velY = AdjustVelocity(velY, accY, ElapsedTime);
X = ApplyDiscreteMovement(X, velX, accX, ElapsedTime);
Y = ApplyDiscreteMovement(Y, velY, accY, ElapsedTime);
}

The redundancy that I am concerned about is in reaching the desired
velocity. I am using a nested if statement. First I check which way they
are accelerating to know whether to use a greater or lesser than in the
comparison to see if it has been reached or passed.

I was wondering if there were a better way to do this?

I don't want to multiple both velocities by the acceleration, this seems
like an unneccesary amount of overheard.

I did come up with this, but I think it further obfuscates the intent.

if(accX!=0.0f){
velX = AdjustVelocity(velX, accX, ElapsedTime);
if((accX<0) == (velX<DesiredVelocityX)){ //passed desired velocity
accX = 0.0f;
velX=DesiredVelocityX;
}
}

I realize that this is not identical since it isn't testing for velX ==
DesiredVelocityX, but as this is the only places where accX is even looked
at, it has the same effect.

Is there a better way to do this?

--
LTP




 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      11-14-2005
Luc The Perverse <> wrote:
> I just wrote this piece of code for moving an object given an intial
> position, velocity and acceleration.


Okay.

> I did come up with this, but I think it further obfuscates the intent.
>
> if(accX!=0.0f){
> velX = AdjustVelocity(velX, accX, ElapsedTime);
> if((accX<0) == (velX<DesiredVelocityX)){ //passed desired velocity
> accX = 0.0f;
> velX=DesiredVelocityX;
> }
> }
>


The only thing unclear in that code is the if statement. If you like,
you can expand it:

if ( ((accX > 0) && (velX >= DesiredVelocityX))
|| ((accX < 0) && (velX <= DesiredVelocityX)))

or even:

boolean doneAccel = false;

if ((accX > 0) && (velX >= DesiredVelocityX)) doneAccel = true;
if ((accX < 0) && (velX <= DesiredVelocityX)) doneAccel = true;

if (doneAccel)
{
...
}

By the way, I can barely read your code. There are naming conventions
for a good reason... and, single-space indents?!? Do you even want
anyone to help you?

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      11-14-2005
On Sun, 13 Nov 2005 21:03:18 -0700, "Luc The Perverse"
<> wrote, quoted or indirectly
quoted someone who said :

> if(accX>0){
> velX = AdjustVelocity(velX, accX, ElapsedTime);
> if(velX>=DesiredVelocityX){ //reached desired velocity
> accX = 0.0f;
> velX=DesiredVelocityX;
> }
> } else if(accX<0){
> velX = AdjustVelocity(velX, accX, ElapsedTime);
> if(velX<=DesiredVelocityX){ //reached desired velocity
> accX = 0.0f;
> velX=DesiredVelocityX;
> }

You gave three possibilities < 0, > 0, ==0

you currently do nothing on ==0;

You might factor like this if the 0 case comes out in the wash:

velx = adjustVelocity( velX, accX, elapseTime);
if (accX>0 ? velX >= desiredVelocityX : velX <= desiredVelocityX )
{
accX = 0.0f;
velX = desiredVelocityX;
}

note the corrections to the naming caps convention.
See http://mindprod.com/jgloss/codingconventions.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      11-14-2005
Chris Smith wrote:

> [ ...] and, single-space indents?!?


Probably the result of pasting tabbed code into Outlook Express. My code comes
out the same way. Sometimes I clean it up (usually, if it's a short explanatory
snippet), sometimes I leave it as is (typcially if it's intended to be complete
code).

-- chris


 
Reply With Quote
 
Luc The Perverse
Guest
Posts: n/a
 
      11-14-2005
"Chris Smith" <> wrote in message
news:.. .
> Luc The Perverse <> wrote:
>> I just wrote this piece of code for moving an object given an intial
>> position, velocity and acceleration.

>
> Okay.
>
>> I did come up with this, but I think it further obfuscates the intent.
>>
>> if(accX!=0.0f){
>> velX = AdjustVelocity(velX, accX, ElapsedTime);
>> if((accX<0) == (velX<DesiredVelocityX)){ //passed desired velocity
>> accX = 0.0f;
>> velX=DesiredVelocityX;
>> }
>> }
>>

>
> The only thing unclear in that code is the if statement. If you like,
> you can expand it:
>
> if ( ((accX > 0) && (velX >= DesiredVelocityX))
> || ((accX < 0) && (velX <= DesiredVelocityX)))
>
> or even:
>
> boolean doneAccel = false;
>
> if ((accX > 0) && (velX >= DesiredVelocityX)) doneAccel = true;
> if ((accX < 0) && (velX <= DesiredVelocityX)) doneAccel = true;
>
> if (doneAccel)
> {
> ...
> }
>
> By the way, I can barely read your code. There are naming conventions
> for a good reason... and, single-space indents?!? Do you even want
> anyone to help you?


Thanks.

What you are seeing is an improvement in naming convetions. I used to use
single letters or letter number combos for names.

What capitalizing the first letter the only thing I did wrong?

--
LTP




 
Reply With Quote
 
Luc The Perverse
Guest
Posts: n/a
 
      11-14-2005
"Chris Uppal" <> wrote in message
news:437886d1$1$38045$.. .
> Chris Smith wrote:
>
>> [ ...] and, single-space indents?!?

>
> Probably the result of pasting tabbed code into Outlook Express. My code
> comes
> out the same way. Sometimes I clean it up (usually, if it's a short
> explanatory
> snippet), sometimes I leave it as is (typcially if it's intended to be
> complete
> code).


Yes that is what happened.

It was well formatted in notepad.

--
LTP




 
Reply With Quote
 
Scott Ellsworth
Guest
Posts: n/a
 
      11-14-2005
In article <43780c9f$0$8304$>,
"Luc The Perverse" <> wrote:

> First I check which way they
> are accelerating to know whether to use a greater or lesser than in the
> comparison to see if it has been reached or passed.
>
> I was wondering if there were a better way to do this?


You could also use an abs.
if (Math.abs(velX)<desiredVelocityX){
do some stuff
}

This is not identical to what you have, but when I wrote n-body
simulators, I found it best to first determine the velocity,
acceleration, and position for the next time step, then the code that
checked the state for completion. I would also add the verification
code that the next time step had the same energy as the step I had just
done, which kept me from using an inaccurate method. (Note: if you
compute potential and kinetic, but allow inelastic collisions, you can
often take the difference in energy, and track it as the thermal energy
of the colliding objects.)

Scott

--
Scott Ellsworth

Java and database consulting for the life sciences
 
Reply With Quote
 
Luc The Perverse
Guest
Posts: n/a
 
      11-15-2005
"Scott Ellsworth" <> wrote in message
news:scott-...
> In article <43780c9f$0$8304$>,
> "Luc The Perverse" <> wrote:
>
>> First I check which way they
>> are accelerating to know whether to use a greater or lesser than in the
>> comparison to see if it has been reached or passed.
>>
>> I was wondering if there were a better way to do this?

>
> You could also use an abs.
> if (Math.abs(velX)<desiredVelocityX){
> do some stuff
> }


OMG duh!

I think this is the answer I was looking for.

> This is not identical to what you have, but when I wrote n-body
> simulators, I found it best to first determine the velocity,
> acceleration, and position for the next time step, then the code that
> checked the state for completion. I would also add the verification
> code that the next time step had the same energy as the step I had just
> done, which kept me from using an inaccurate method. (Note: if you
> compute potential and kinetic, but allow inelastic collisions, you can
> often take the difference in energy, and track it as the thermal energy
> of the colliding objects.)


I think you are overestimating the physics of my sprite engine

I am still trying to find an efficent method for detecting collisions at
all. I'm trying to avoid using anything with an N^2 efficiency. I know if
I defined areas as rectangles, I could so some funky stuff like a binary
search tree with a quick sorting algorithm, and then compare min and maxes
to find collisions, but only if the discrete unit of measure is adequately
small that an object cannot jump over another object.

I'm thinking what I really need to do is have collision areas made with true
polygons, focusing on triangles and rectangles, and then stretch them
through 3 space (time) for each discrete movement, and look for the lowest Z
(time) value intersections in the planes and then recalculate with the
smaller time interval to calculate the position to enact an inelastic
collision.

My dilemma is, I don't know if all of that is necessary for a game. I can
reasonably assume that collision capible sprites, NPCs, PCs and Projectiles
will all be updated on a regular enough interval that there will be no
missed bullets. Forgive the pun, but I like my algorithms to be bullet
proof

But as far as storing thermal energy, I don't know about that.
Conservation of energy in a simulated environment is not exactly a priority
AFAIC. Anyways, by definition, isn't all energy conserved kinetically in an
inelastic collision? (Or do I have it backwards?)

--
LTP




 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-15-2005
On Mon, 14 Nov 2005 13:38:42 -0700, "Luc The Perverse"
<> wrote, quoted or indirectly
quoted someone who said :

>What capitalizing the first letter the only thing I did wrong?


see http://mindprod.com/jgloss/codingconventions.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-15-2005
On Mon, 14 Nov 2005 19:45:55 -0700, "Luc The Perverse"
<> wrote, quoted or indirectly
quoted someone who said :

>> You could also use an abs.
>> if (Math.abs(velX)<desiredVelocityX){
>> do some stuff
>> }

>
>OMG duh!
>
>I think this is the answer I was looking for.


definitely better than my ternary operator solution. It reduces even
more redundancy.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with achieving link redundancy to a Cisco 3550 switch Garrick Ransome Cisco 2 11-03-2006 02:20 PM
redundancy/failover/load balancing with eigrp examples???? David Cisco 0 11-12-2003 04:23 PM
redundancy with 2 links Bengt Ohlen Cisco 7 11-06-2003 02:31 PM
Redundancy across FE ports 7204vxr Todd Carew Cisco 4 11-05-2003 10:01 PM
Cat5K Supervisor III Redundancy Problem Hamid Cisco 0 10-19-2003 03:06 PM



Advertisments
 



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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57