Velocity Reviews > Java > Redundancy Elimination?

# 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){
if(velX>=DesiredVelocityX){ //reached desired velocity
accX = 0.0f;
velX=DesiredVelocityX;
}
} else if(accX<0){
if(velX<=DesiredVelocityX){ //reached desired velocity
accX = 0.0f;
velX=DesiredVelocityX;
}
}
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){
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

Chris Smith
Guest
Posts: n/a

 11-14-2005
Luc The Perverse <(E-Mail Removed)> 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

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green
Guest
Posts: n/a

 11-14-2005
On Sun, 13 Nov 2005 21:03:18 -0700, "Luc The Perverse"
<(E-Mail Removed)> 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
--
http://mindprod.com Java custom programming, consulting and coaching.

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

Luc The Perverse
Guest
Posts: n/a

 11-14-2005
"Chris Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed).. .
> Luc The Perverse <(E-Mail Removed)> 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

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

Luc The Perverse
Guest
Posts: n/a

 11-14-2005
"Chris Uppal" <(E-Mail Removed)-THIS.org> wrote in message
news:437886d1\$1\$38045\$(E-Mail Removed).. .
> 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

Scott Ellsworth
Guest
Posts: n/a

 11-14-2005
In article <43780c9f\$0\$8304\$(E-Mail Removed)>,
"Luc The Perverse" <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
Java and database consulting for the life sciences

Luc The Perverse
Guest
Posts: n/a

 11-15-2005
"Scott Ellsworth" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> In article <43780c9f\$0\$8304\$(E-Mail Removed)>,
> "Luc The Perverse" <(E-Mail Removed)> 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

Roedy Green
Guest
Posts: n/a

 11-15-2005
On Mon, 14 Nov 2005 13:38:42 -0700, "Luc The Perverse"
<(E-Mail Removed)> 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
--
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green
Guest
Posts: n/a

 11-15-2005
On Mon, 14 Nov 2005 19:45:55 -0700, "Luc The Perverse"
<(E-Mail Removed)> 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.
--
http://mindprod.com Java custom programming, consulting and coaching.