[Solved] Help with a very simple math expression, from official GS Basic Table Tennis template.

osakamitsuosakamitsu Member Posts: 70
edited February 2015 in Working with GS (Mac)

Hello, Slowly I am going through the templates and trying to learn how they tick. I am stuck on this math expression and what it is doing exactly.

http://i.imgur.com/nWaACbf.png

I understand that this expression adds velocity in the Y direction if the ball gets stuck in an X loop. But what is this expression doing?

When all condition are valid:
self.Motion.Linear Velocity.Y is equal to 0

Then:
When ball actor collides with the wall/paddles

Change attribute: self.Motion.Linear Velocity.Y To:random(70,120)*(-1)^(random(1,2))

I would assume we are still talking degrees with the 70 and 120 since they are at the top of the circle of degrees, but why use 70 and not 60 to even out the range of degrees?

But I am really stumped on this part:

*(-1)^(random(1,2))

My best guess thinking back to algebra, the (-1) is reflecting/reversing the 70 and 120 degrees? But I have no clue if I am on the right track. And why the exponent (^) with the random(1,2)? What does the 1 and 2 represent?

can someone break that part down for me?

Many thanks in advance! :D

Comments

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

    I can't say I'm very familiar with that template but it would appear that

    Change attribute: self.Motion.Linear Velocity.Y To:random(70,120)*(-1)^(random(1,2))

    changes self.Motion.Linear Velocity.Y to a random number between 70 and 120 but then either makes it positive or negative since (-1)^1=-1 and (-1)^2=1. Does that help at all?

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

  • -Timo--Timo- Member Posts: 2,313
    edited February 2015

    Y motion isnt a degree. The Y motion is a movement on the y axis (up and down) and the value is the speed. Just play with this in a blank project to see what it does.
    Now the rule, if motionY=0 (so when its only moving left and right and not up or down) then it changes the Y motion to random(70,120)x(-1)^random(1,2). This is just to keep the game like it should. You dont want the ball to only keep moving left and right, you also want it to move it up and down.
    I assume you know what random does? Between those numbers it picks a random one. So random(70,120) can be 71. 85, 110 or whatever between these numbers. Just like the random(1,2). This can be 1 or 2.

    -1^1 = -1.

    -1^2 = -1x-1 = 1
    Negative Y motion moves down and positive Y motion moves up.
    Random(70,120) only gets positive values. But the part after this can make it negative
    Lets take 100 as example. 100x1=100 and ball gets positive Y motion and moves up. 100x-1=-100 and ball gets negative y motion and moves down.
    If the ball has a Y motion of 0 it randomly picks a number between -120 and -70 or 70 and 120.

    Why not 60?
    You can make it 60, it will then pick a random value between -120 and 60 or 60 and 120. The Y motion just can become lower now.

    I hope this makes sense and you understand it.

  • osakamitsuosakamitsu Member Posts: 70
    edited February 2015

    Thank you so much guys! Yeah I wasn't on the right track lol. I understand that it is the up and down direction but I was thinking it was an angle in the upward or downward direction. Part of me didn't feel like it was talking about an angle but I couldn't figure out why.

    I was wondering how it distinguishes between non angle and angle, but I guess if the Y attribute is not involved it resolves to an angle number...

    So really it looks like this:
    random(Y value between 70 to 120)x(-1)^random(1,2)

    Ok, so we have (-1)^1 or 2. The 1 or 2 or is chosen randomly.

    So the 1 or 2 that is randomly chosen can be either:

    -1^1 = -1 * 1 = -1
    or
    -1^2 = -1 * -1 = 1

    So we only have two numbers, -1 or 1 that it could possibly be from the exponent.

    The final value of the -1 or 1 gets multiplied to the random Y value.

    So, it doesn't give random angle but it ends up being an angle in the end because it's moving in the X (y=0) direction already, and then gets bumped to the random +/- Y value while it is in motion creating the new angle?

    I think I am on the right track now... correct? :P

    Been too long since I have had math...

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

    I was wondering how it distinguish between non angle and angle, but I guess if the Y attribute is not involved it resolves to an angle number...

    Not exactly. self.Motion.Linear Velocity.Y is a measure of the upward (or downward) speed of an actor. If the X velocity is 0, then the actor will either move straight up (y>0) which you could also call 90° or straight down (y<0) which you could call 270°, or not at all (y=0).

    If the X velocity is negative or positive then the Y velocity will indeed create an angle of direction.

    So, it doesn't give random angle but it ends up being an angle in the end because it's moving in the X (y=0) direction already, and then gets bumped to the random +/- Y value while it is in motion creating the new angle?

    I would say that's accurate. :)

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

  • osakamitsuosakamitsu Member Posts: 70
    edited February 2015

    Seems I forgot that the attributes were named "motion.linear velocity" when I was sorting all of that out! :P

    Thanks for clearing all that up for me! I feel much better about it.

    Seriously, thank you guys so much for your time!

  • SocksSocks London, UK.Member Posts: 12,822
    edited February 2015

    @tatiang said:
    I can't say I'm very familiar with that template but it would appear that

    changes self.Motion.Linear Velocity.Y to a random number between 70 and 120 but then either makes it positive or negative since (-1)^1=-1 and (-1)^2=1. Does that help at all?

    On a side note . . . I like to use ((random(0,1)*2)-1) . . . for random positive/negative, it's probably easier for people to work out what it does as it doesn't use any of them there triangle things.

  • SocksSocks London, UK.Member Posts: 12,822
    edited February 2015

    @osakamitsu said:
    I was wondering how it distinguishes between non angle and angle, but I guess if the Y attribute is not involved it resolves to an angle number...

    Linear velocity is a speed value (pixels per second), angles are not involved.

    [Edit - just spotted @tatiang has already pointed this out]

  • osakamitsuosakamitsu Member Posts: 70
    edited February 2015

    @Socks

    Actually your reply was greatly appreciated! I do like your version of the equation and I wondered how the speed was calculated. I figured it was calculated by being compared to the in game engine clock but the pixels per second was the missing piece of the puzzle. :P I guess that should have been common sense but having little experience, sometimes it helps to have the obvious pointed out. :D

    Actually quick question...

    In your version if I am wanting a random number between (0,1) but then it's multiplied by 2... then -1... won't I just be left with 0 for one of the numbers? Im probably looking at it wrong.

    ((random(0,1)*2)-1)

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

    random(0,1) returns 0 or 1. Multiplying by 2 returns 0 or 2. Subtracting 1 returns -1 or 1.

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

  • osakamitsuosakamitsu Member Posts: 70
    edited February 2015

    What would I do without you guys? So simple. I just keep over complicating it.

    You guys are incredible. Thank you so much! I mean it.

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

    @osakamitsu said:
    Socks

    Actually your reply was greatly appreciated! I do like your version of the equation and I wondered how the speed was calculated. I figured it was calculated by being compared to the in game engine clock but the pixels per second was the missing piece of the puzzle. :P I guess that should have been common sense but having little experience, sometimes it helps to have the obvious pointed out. :D

    Yes, agreed, even the obvious stuff needs pointing out from time to time, but I'd say the one mistake people tend to make when starting with GameSalad is to overcomplicate simple concepts.

    A: "Hey, I want to rotate my actor's self.rotation to 90° when you click on it, so I've set up a game level attribute (angle attribute) - I've called this Original Angle, then I've got a second attribute called New Angle (also an angle attribute) - and in the actor I have a rule that says when touch is pressed and Original Angle is not equal to 90° then change New Angle to 90°, and then another rule that says when Original Angle is not equal to New Angle then constrain self.rotation to New Angle"

    B: "Why not simply have: when touch is pressed change rotation to 90° ?"

    A: "What kind of table should I make for that, should the table be constrained to the value of the attribute that is tracking the rotation or should it have its own attribute that checks the rotation when the actor is touched but before the constrain passed the rotational information to the New Angle attribute that controls the rotation of the actor ?"

    B: "Kill me . . . "

    :tongue:

    @osakamitsu said:
    Actually quick question...

    In your version if I am wanting a random number between (0,1) but then it's multiplied by 2... then -1... won't I just be left with 0 for one of the numbers? Im probably looking at it wrong.

    ((random(0,1)*2)-1)

    Random (0,1) will give you a number between 0 and 1, this will always be an integer (random doesn't do decimal points), so we will get either 0 or 1.

    Now take the result from above and multiply it by 2 . . . which give us 0 or 2.

    Now subtract 1 . . . which gives us -1 or 1.

    So ((random(0,1)*2)-1) gives us either -1 or 1.

    So random(100,200) * ((random(0,1) *2)-1) . . .will give you a value between 100 and 200 that is either negative or positvie.

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

    @tatiang said:
    random(0,1) returns 0 or 1. Multiplying by 2 returns 0 or 2. Subtracting 1 returns -1 or 1.

    This.

  • osakamitsuosakamitsu Member Posts: 70

    @Socks @tatiang

    You guys are seriously great! I totally understand now. :D

  • osakamitsuosakamitsu Member Posts: 70
    edited February 2015

    @Socks or @tatiang

    Ok now I have myself wondering about something.

    The ball is stuck in the loop. It registers a value of 0 in the Y value of it's linear velocity. The expression kicks in and finds a random value of 100. What is this Y value relative to? The actor right? Because it's his self attribute, right? So the ball would be jerk in a direction 100 pixels relative to the middle of the actor? So, 100 pixels above the center of the actor? Then a number of -117 would be 117 pixels below the center of the actor?

  • SocksSocks London, UK.Member Posts: 12,822
    edited February 2015

    @osakamitsu said:
    The ball is stuck in the loop.

    'Loop' could mean anything in this context, it helps make the question clearer if you can be specific.

    @osakamitsu said:
    It registers a value of 0 in the Y value of it's linear velocity.

    What does 'register' mean in the context of your project/GameSalad ?

    @osakamitsu said:
    The expression kicks in and finds a random value of 100.

    Not sure what that means either?

    @osakamitsu said:
    What is this Y value relative to? The actor right?

    If you are referring to the actor's Y.linear velocity, then the value will be the speed in the Y direction . . . it's not really relative to anything, I suppose you could say it was relative to 0 (no speed / stopped), but then again it is no more 'relative' to 0 than it is to 134 or 73.88.

    It's a bit like asking, when a car is driving at 100mph, what is this 100mph relative to ? Maybe the road, which is 0mph, so by that extension an actor's Y.linear velocity is 'relative' to 0, like every other number.

    @osakamitsu said:
    So the ball would be jerk in a direction 100 pixels relative to the middle of the actor?

    It would move at a speed of 100 pixels per second in the Y+ direction (up), an object's movement (assuming the object is not an amorphous blob with a changing shape) is not relative to any one part of the object (like the middle of the actor).

    With our hypothetical car, what is the 100mph relative to, the windscreen, the rear numberplate, the driver ?

    @osakamitsu said:
    So, 100 pixels above the center of the actor? Then a number of -117 would be 117 pixels below the center of the actor?

    If we are still taking about the Y linear velocity of an actor . . .

    100 is a velocity of 100 pixels per second moving upwards, -100 is a velocity of 100 pixels per second moving downwards.

  • osakamitsuosakamitsu Member Posts: 70

    @Socks

    Im not sure how to properly convey what I am thinking. I understand it is a value of movement. I guess that means whatever the current position of the actor is when it generates that number from the expression, then it will start to move up or down depending on that y value. I guess what I am wondering is, depending on how high or low the number is, it determines the strength of the movement? In just speed? or angle too? I was also wondering where it measures the y value from. The center point of the actor, or the game scene. It would have to be the actor though right?

  • SocksSocks London, UK.Member Posts: 12,822
    edited February 2015

    @osakamitsu said:
    Im not sure how to properly convey what I am thinking. I understand it is a value of movement.

    Even that sounds a little too complicated ! Lol :wink:

    Would you call the speed of a car its 'value of movement' or would you simply call it the 'speed' ?

    @osakamitsu said:
    I guess that means whatever the current position of the actor is when it generates that number from the expression, then it will start to move up or down depending on that y value.

    Yes, the Y value is the speed of the actor, so if you say move in the up direction at 50pps, then it will do just that, head off in the up direction at a speed of 50pps.

    @osakamitsu said:
    I guess what I am wondering is, depending on how high or low the number is, it determines the strength of the movement?

    'Strength of the movement' ? What is strength of the movement ?

    If a car is travelling at 50mph, what would you say its strength of movement was ?

    @osakamitsu said:
    In just speed? or angle too?

    The value is the speed (in pixels per second).

    If the actor is going up it is heading in a direction of 90° relative to the scene or 0° relative the actor, if it is going down it is heading in a direction of 270° relative to the scene or 0° relative the actor.

    @osakamitsu said:
    I was also wondering where it measures the y value from. The center point of the actor, or the game scene. It would have to be the actor though right?

    The Y value is the speed the actor is moving, it doesn't make sense to ask where 'it' (the actor?) is measuring the speed from, as I say above the only thing vaguely sensical is to say a speed of 100 is 100 more than 0, so I suppose you could say the speed is relative to 0 (stopped), but no more than saying it is relative to 200 or 69.33 or any other number.

    The centre point of an actor does not travel faster or slower than the corners or 9.72 pixels upwards from the centre of the actor or the lefthand edge (all other things being equal), so the idea that the speed is measured from the centre point of an actor also doesn't make much sense.

    Where on a car is its speed measured from, the roof, the headlights, the rear bumper ?

  • osakamitsuosakamitsu Member Posts: 70
    edited February 2015

    @Socks

    haha alright I got it now. :P

    You rule. Thank you so much. :)

  • osakamitsuosakamitsu Member Posts: 70

    @Socks

    lol Okay I think I may have figured out how to ask what I am slightly confused about.

    I understand that it just results in a motion of up or down. But let's say that there are two numbers that are randomly drawn. 70 and 120. What would the difference be between those numbers? Meaning what would be affected in game?

    Since this is linear velocity would it be the speed? I was thinking everything in this game moved at the same speed but maybe I am wrong...

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

    But let's say that there are two numbers that are randomly drawn. 70 and 120. What would the difference be between those numbers? Meaning what would be affected in game?

    It sounds like you are asking what the difference between self.Motion.Linear Velocity.Y=70 and self.Motion.Linear Velocity.Y=120 is. The number represents the number of pixels per second that an actor moves across the screen. So the difference would be 50 pixels per second.

    Since this is linear velocity would it be the speed?

    Yes.

    I was thinking everything in this game moved at the same speed but maybe I am wrong...

    Why would things move at the same speed? Actors move at whatever speed you set. Without your intervention, they don't move at all (self.Motion.Linear Velocity.X and Y equal to zero).

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

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

    Here's a demo that shows three ways of moving an actor at the same speed (100 pixels per second for 3 seconds with a distance of 300 pixels).

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

  • osakamitsuosakamitsu Member Posts: 70
    edited February 2015

    @tatiang said:
    Why would things move at the same speed? Actors move at whatever speed you set. Without your intervention, they don't move at all (self.Motion.Linear Velocity.X and Y equal to zero).

    Ah ok thanks! Well I guess to say everything at the same speed would be wrong. I meant that I thought everything was a constant speed of what ever you set. I was using constant as "same" lol. I thought the ball had a constant speed and not vary, aside from when it switches from direction. Which is one reason was confused. But I get it now.

    :P

    Thanks for the demo! Very cool and helpful!

  • SocksSocks London, UK.Member Posts: 12,822
    edited February 2015

    @osakamitsu said:
    I understand that it just results in a motion of up or down. But let's say that there are two numbers that are randomly drawn. 70 and 120. What would the difference be between those numbers? Meaning what would be affected in game?

    I get the distinct feeling this question has already been answered !!!! :smiley: :wink:

    @timolapre1998 said:
    Y motion isnt a degree. The Y motion is a movement on the y axis (up and down) and the value is the speed.

    >

    @tatiang said:
    self.Motion.Linear Velocity.Y is a measure of the upward (or downward) speed of an actor

    >

    @Socks said:
    Linear velocity is a speed value (pixels per second)

    >

    @Socks said:
    the value will be the speed in the Y direction

    >

    So what would be effected in the game, it would be the speed of the actor, a value of 70 will move the actor at 70 pixels per second, a value of 120 will move it at 120 pixels per second.

    . . . . . . . .

    @osakamitsu said:
    Since this is linear velocity would it be the speed?

    Yes, the value is the speed in pixels per second.

    @osakamitsu said:
    I was thinking everything in this game moved at the same speed but maybe I am wrong...

    Actors don't move at all unless you tell them to, and when you tell them to move you tell them at what speed to move, so the only way everything in a game would move at the same speed would be if you told them all to move at the same speed.

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

    @tatiang said:
    Here's a demo that shows three ways of moving an actor at the same speed (100 pixels per second for 3 seconds with a distance of 300 pixels).

    Put all my money on yellow :neutral:

    Now I have to live in a box.

  • osakamitsuosakamitsu Member Posts: 70

    See, there I go complicating things again.

    Thanks guys. :D

Sign In or Register to comment.