# Math help - Is this possible?

Member Posts: 1,159

Hey guys,

I've ran into a problem with displaying how many percent of a level my hero has cleared.

Im using this equation:

roundTo(( self.Position.X / scene.Size.Width )*100,0).."%"

As described by @RabidParrot here:

The problem is that Im shifting motion of my hero from positive to negative a couple of times.

Example:

Heros motion X is set to 100.
When he hits a certain position, I change my heros X position (forward) and then his X motion to -100.
After hitting the next position I change his X position (forward) and his X motion to 100.
This happens two times during a level.

When my hero dies, I save the position and display the percentage completed.

Is there a way to calculate and display percentage as in the example above even when I go "back"?
Since the percentage is calculated from left to right..if I cleared lets say 50% on first position change, and then change heros motion to -100 that would mean that if I clear 25% more of the level the displayed percentage would not be calculated right.

Did that make any sense?

I know @Socks and @RThurman are math wizards

• Member, Sous Chef, PRO Posts: 2,865

@Dues -- Perhaps you can treat the percent as a high score.

That is, whenever the current 'score' is greater than the recorded 'score', then save the current 'score' as the recorded 'score.'

(You can replace the word 'score' with 'percent' in the above sentence.)

• Member Posts: 1,159
edited May 2016

@RThurman said:
@Dues -- Perhaps you can treat the percent as a high score.

That is, whenever the current 'score' is greater than the recorded 'score', then save the current 'score' as the recorded 'score.'

(You can replace the word 'score' with 'percent' in the above sentence.)

But that won't give me an accurate percentage done. If lets say 20% of the level is played backwards, and my hero dies somewhere in that 20% the percent wont update properly since I go from lets say x position 3000 to x position 2500. That means I can clear say 50% and then 55% but the percent woukd show the opposite.

• Member, Sous Chef, PRO Posts: 2,865

Hmmm... Either I don't understand what you mean -- or you you don't get what I meant.

Perhaps if you give a few more examples, it might help.

• Member, PRO Posts: 1,067

I think I understand, took me a few minutes. So you want not only the forward completion but backward completion too? So like 80% of level is running all the way forward and then 20% is playing the level backward for a bit?

• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,944

It sounds like you're wanting to keep track of the entire distance traveled even though the player can move backwards. Think of a car trip where you go from San Francisco to Los Angeles (400 miles) but you leave your jacket in Santa Barbara and have to drive back (100 miles) to get it. You then complete the trip by returning to Los Angeles (100 miles).

So in all, you've driven 600 miles but you're only 400 miles from your initial starting point.

Is that a correct analogy?

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• Member Posts: 1,159

@tatiang said:
It sounds like you're wanting to keep track of the entire distance traveled even though the player can move backwards. Think of a car trip where you go from San Francisco to Los Angeles (400 miles) but you leave your jacket in Santa Barbara and have to drive back (100 miles) to get it. You then complete the trip by returning to Los Angeles (100 miles).

So in all, you've driven 600 miles but you're only 400 miles from your initial starting point.

Is that a correct analogy?

Exactly!

• London, UK.Member Posts: 12,822

@tatiang said:
It sounds like you're wanting to keep track of the entire distance traveled even though the player can move backwards. Think of a car trip where you go from San Francisco to Los Angeles (400 miles) but you leave your jacket in Santa Barbara and have to drive back (100 miles) to get it. You then complete the trip by returning to Los Angeles (100 miles).

So in all, you've driven 600 miles but you're only 400 miles from your initial starting point.

Is that a correct analogy?

What kind of jacket was it ? I'm thinking perhaps something classy ?

• Member Posts: 1,159

@Socks said:

@tatiang said:
It sounds like you're wanting to keep track of the entire distance traveled even though the player can move backwards. Think of a car trip where you go from San Francisco to Los Angeles (400 miles) but you leave your jacket in Santa Barbara and have to drive back (100 miles) to get it. You then complete the trip by returning to Los Angeles (100 miles).

So in all, you've driven 600 miles but you're only 400 miles from your initial starting point.

Is that a correct analogy?

What kind of jacket was it ? I'm thinking perhaps something classy ?

Oh yes! That one you wouldn't want to leave behind!

• Member Posts: 795

Why does the hero need to go backwards? Is it after collecting something?

You probably need to keep track of the "level state" somehow and based on that calculate the progress percentage.

• London, UK.Member Posts: 12,822

@Manto said:
Why does the hero need to go backwards? Is it after collecting something?

His jacket.

• Member Posts: 1,159

@Manto I was thinking "reverse level" a good example would be how Geometry Dash does it.
You go fro left to right, hit a portal and the you come out from right and go left

Maybe Im complicating things here.

• Member Posts: 795
edited May 2016

@Dues said:
@Manto I was thinking "reverse level" a good example would be how Geometry Dash does it.
You go fro left to right, hit a portal and the you come out from right and go left

Maybe Im complicating things here.

In that case I would make a boolean game attribute let's call it "going_left" to indicate whether the player is going right or left. And when they hit the portal (or the jacket) the attribute would change.

Then to calculate the progress percentage use:

Condition:
If going_left = true
roundTo(( self.Position.X / scene.Size.Width) * 50,0).."%"
Otherwise
roundTo(50 + ( self.Position.X / scene.Size.Width) * 50,0).."%"

• London, UK.Member Posts: 12,822

@tatiang said:
It sounds like you're wanting to keep track of the entire distance traveled even though the player can move backwards. Think of a car trip where you go from San Francisco to Los Angeles (400 miles) but you leave your jacket in Santa Barbara and have to drive back (100 miles) to get it. You then complete the trip by returning to Los Angeles (100 miles).

So in all, you've driven 600 miles but you're only 400 miles from your initial starting point.

Is that a correct analogy?

@Dues

If the distance travelled can be more than the distance between point A and point B (nominally scene.Size.Width) then can the final percentage of distance travelled be more than 100% ?

• Member Posts: 1,159

@Manto you might be on to something there!

@Socks

I'll try to explain in a better way (i think)

If the scene is let's say 100 pixels to make it easy, if we go 50 pixels we cleared 50%
If we then move out hero to 70 pixels and let him travel backwards to 50 pixels we would then have cleared 70% of the level.

Man, I might just have made it even more complicated

• London, UK.Member Posts: 12,822

@Dues said:
@Manto you might be on to something there!

@Socks

I'll try to explain in a better way (i think)

If the scene is let's say 100 pixels to make it easy, if we go 50 pixels we cleared 50%
If we then move out hero to 70 pixels and let him travel backwards to 50 pixels we would then have cleared 70% of the level.

Man, I might just have made it even more complicated

Constrain D to max(self.x,D)

• Member, Sous Chef, PRO Posts: 2,865

Maybe something like this?

• Member Posts: 1,159

@RThurman That is a brilliant solution! Thank you very much!

• Member, Sous Chef, PRO Posts: 2,865

@tatiang said:
It sounds like you're wanting to keep track of the entire distance traveled even though the player can move backwards. Think of a car trip where you go from San Francisco to Los Angeles (400 miles) but you leave your jacket in Santa Barbara and have to drive back (100 miles) to get it. You then complete the trip by returning to Los Angeles (100 miles).

Oh! Its "one of those" kinds of math questions:
Train A, traveling 70 miles per hour (mph), leaves San Francisco heading toward LA, 400 miles away. At the same time Train B, traveling 60 mph, leaves LA heading toward San Francisco. How far away from Santa Barbara do the two trains meet?

• Member, Sous Chef, PRO Posts: 2,865

@Dues said:
@RThurman That is a brilliant solution! Thank you very much!

You are welcome!

• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,944

You had me at "Train B". That's when I got up and walked away from the computer.

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• Member Posts: 1,159

@tatiang said:
You had me at "Train B". That's when I got up and walked away from the computer.

Imagine how I feel