GameSalad

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Math help - Is this possible?

DuesDues Posts: 1,159Member

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:

http://forums.gamesalad.com/discussion/83173/percent-of-level-ran

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 ;)

Comments

  • RThurmanRThurman Posts: 2,842Member, Sous Chef, PRO

    @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.)

  • DuesDues Posts: 1,159Member
    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.

  • RThurmanRThurman Posts: 2,842Member, Sous Chef, PRO

    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.

  • AlchimiaStudiosAlchimiaStudios Posts: 1,040Member, PRO

    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?

    Follow us: Twitter - Website

  • tatiangtatiang Posts: 11,907Member, Sous Chef, PRO, Senior Sous-Chef

    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

  • DuesDues Posts: 1,159Member

    @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! :)

  • SocksSocks London, UK.Posts: 12,822Member

    @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 ?

  • DuesDues Posts: 1,159Member

    @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!

  • MantoManto Posts: 793Member

    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.

  • SocksSocks London, UK.Posts: 12,822Member

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

    His jacket.

  • DuesDues Posts: 1,159Member

    @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. :)

  • MantoManto Posts: 793Member
    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).."%"

  • SocksSocks London, UK.Posts: 12,822Member

    @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% ?

  • DuesDues Posts: 1,159Member

    @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 :yum:

  • SocksSocks London, UK.Posts: 12,822Member

    @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 :yum:

    Constrain D to max(self.x,D)

  • RThurmanRThurman Posts: 2,842Member, Sous Chef, PRO

    Maybe something like this?

  • DuesDues Posts: 1,159Member

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

  • RThurmanRThurman Posts: 2,842Member, Sous Chef, PRO

    @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?

    Answer:
    I am sure glad I am not in the 5th grade!

  • RThurmanRThurman Posts: 2,842Member, Sous Chef, PRO

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

    You are welcome!

  • tatiangtatiang Posts: 11,907Member, Sous Chef, PRO, Senior Sous-Chef

    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

  • DuesDues Posts: 1,159Member

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

    Imagine how I feel :joy:

Sign In or Register to comment.