Why doesn't this formula work

Using this rule in an actor on the left side of the screen moves the actor right towards the centre as intended. But if I shift the actor to the right side of the screen nothing happens when it should move it left towards the centre.

I've been over and over it with a calculator, I must be missing something. Can somebody explain to me why this doesn't work and offer a solution?

Comments

  • SocksSocks London, UK.Member Posts: 12,822
    Change '=1024' to '> 1024'
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598

    Hi @colander I've tested out your programming and providing that your actor is more than 512, it moves to the left as expected… so at a guess the problem might lie with your boolean condition… maybe try When game.Screenetc =>1024 perhaps…

    Edit: beaten by @Socks… :-)

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2013
    ...

    ?
  • colandercolander Member Posts: 1,610
    edited November 2013
    I tried that it made no difference, could it be a bug? I am using the Nightly Build 100 because it was more stable for my game than 145 for my game.
  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2013
    I tried that it made no difference, could it be a bug? I am using the Nightly Build 100 because it was more stable for my game than 145 for my game.
    With your rules

    Placing your actor at x=1000 will move it to X=979.66504, is this is what is happening ?
  • colandercolander Member Posts: 1,610
    edited November 2013
    @Socks no it moves it further right by a few pixels. I am trying to upload a Test project to show you but it keeps saying 'no file selected' what am I doing something wrong?
  • colandercolander Member Posts: 1,610
    I'll just describe what I did. I created a blank project in iPhone legacy landscape. Created an actor and put the above rule in it. put two instances on the scene at x 65 and 415. Created another actor and place an instance below each of the other two to see how much movement is occurring.

    Clicked preview there should be four standard size actors on the screen like initial scene 1. Go to the top left and select iPad Landscape from the drop down menu and you should get the result in initial scene 2.

    For some reason I can attach png files but not project files. aaaahhh! had to zip it it is attached now.
  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2013
    .
    With your rules

    Placing your actor at x=1000 will move it to X=979.66504, is this is what is happening ?
    @Socks no it moves it further right by a few pixels.
    From what I can see 1,000 should move stuff to the left ? (in fact anything above 512 will move stuff to the left).

    If x=1,000

    self.Position.X -(( self.Position.X -512)*0.04167)
    1,000 -(( self.Position.X -512)*0.04167)
    1,000 -(( 1,000 -512)*0.04167)
    1,000 -(( 488 )*0.04167)
    1,000 -(20.33496)
    979.66504 (20.33496 pixels to the left).

    . . . . . . . .


    Anyhow, your project is 480 pixels wide, so the condition 'if screen width is 1024' is never met . . . . ergo: nothing happens.

    . . . . . . . .

    Edit, just read your post above - about changing project size, yes when you change the project to iPad Landscape - the boxes move as expected - that is to say the formula seems to be working correctly.

    65 -(( 65 -512)*0.04167) = 83.62649
    415 -(( 415 -512)*0.04167) = 419.04199

    image


  • colandercolander Member Posts: 1,610
    edited November 2013
    @Socks The formula should move any actor on the left side of 512 to the right and anything on the right side of 512 to the left. So the right side of the screen should be a mirror image of the left.

    For example if the two actors end up being placed at x100 and x924 (100 px in from each side) after selecting iPad Landscape then it should calculate as follows (I think);

    self.Positon.X = 100-((100-512)*0.04167) = 117.16804 this works in the example
    self.Positon.X = 924-((924-512)*0.04167) = 906.83196 this doesn't work in the example

    I'm no math geniuses but it should work shouldn't it?
  • SocksSocks London, UK.Member Posts: 12,822
    @Socks The formula should move any actor on the left side of 512 to the right and anything on the right side of 512 to the left
    Yes, like I say something placed at X=1,000 will move to the left.
    So the right side of the screen should be a mirror image of the left.
    Depending on your starting state.
    For example if the two actors end up being placed at x100 and x924 (100 px in from each side) after selecting iPad Landscape then it should calculate as follows (I think);

    self.Positon.X = 100-((100-512)*0.04167) = 117.16804 this works in the example
    self.Positon.X = 924-((924-512)*0.04167) = 906.83196 this doesn't work in the example

    I'm no math geniuses but it should work shouldn't it?
    Works fine for me:


    image


  • colandercolander Member Posts: 1,610
    @Socks can you post your project? You are getting the result I want but not the scale.

    I am taking an iPhone 4 480x320 and using the viewers iPad Landscape in overscan magnifying it so it should look similar to the one I posted but with the mirror image result you have in your one above.

    The original screenshots were taken with the creator and are not to scale for some reason I only just realised this. So I used cmd+shift+4 to take the ones below and show them to scale when opened I hope. I'll check it after I post it. The starting state on the left is 480x320 and the one on the right is 1024x768.

    When you said "From what I can see 1,000 should move stuff to the left ? (in fact anything above 512 will move stuff to the left)" I forgot to say in my previous post yes that is correct. Sorry about that.

    I have repeated some things here I am just trying to make it all as clear as I can. I am definitely not the best at explaining stuff.
  • SocksSocks London, UK.Member Posts: 12,822
    edited November 2013
    @Socks can you post your project?
    I'd just be sending your own project back to you. All I've done is download the project you've uploaded (and stuck a Display Text / X position behaviour on the actors to see where they are).
    You are getting the result I want but not the scale.
    I don't know what that means ?
  • colandercolander Member Posts: 1,610
    I mean looking at your screenshot your actors on the right are a mirror of the actors on the left. But they are not to scale they are at the bottom of the screen and much smaller.

    Click on my second image above, it is to size and scale and compare it to your image. You must be doing something different than what I described which might help me figure out what I am doing wrong. What did you do to get that result?
  • SocksSocks London, UK.Member Posts: 12,822
    What did you do to get that result?
    Opened your project.
    Changed the project size to iPad Landscape.
    Placed a Display Text behaviour (showing X.position) on the actor.
    Changed the X positions of the actors to X=100 and X=924.
    Hit 'play'
    Took a screenshot.
  • colandercolander Member Posts: 1,610
    edited November 2013
    Ok I see what you have done I don't believe I can do it that way. I designed and made my game in iPhone 4 and I am trying to make a universal build so unless I completely redo it in iPad size I don't think I can do it that way.
  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    edited November 2013
    Change Attribute: self.Position.X
    To: game.Screen.Size.Width* (max(0,min(1,self.Position.X-game.Screen.Size.Width/2)) + max(-1,min(1,game.Screen.Size.Width/2-self.Position.X))*%from edge)

    (Note: game.Screen.Size.Width is multiplied by the rest of the expression)

    Explanation:
    max(0,min(1,self.Position.X-game.Screen.Size.Width/2)) will return 0 if the position of the actor is left of the center of the screen, and 1 if it is on the right of the center of the screen.
    Multiplying this value by the width of the screen will return either 0 or the screen width.

    (Note the switch between the width and position in this part of the expression)
    max(-1,min(1,game.Screen.Size.Width/2-self.Position.X)) will return 1 if it is on the left side of the screen or -1 if it is on the right side.
    Multiplying this by the screen width and the %from edge will give either a positive value if on the left side of the screen or negative value if on the right side of the screen.
    e.g. if %from edge was 0.04167 on a 1024 screen width it would return 42.67(left) or -42.67(right)

    Adding those 2 results together and you should get your positions.

    AND this doesn't care what screen size you have.
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    @CodeMonkey -- now we're getting somewhere! Thanks for posting this very useful key for making screen layouts more... umm..... universal.
  • colandercolander Member Posts: 1,610
    edited November 2013
    @CodeMonkey I couldn't get your code to work but I am not a math guy. I am still learning how GS works these various views in Preview. It probably works I just don't know how to get it working. The percentages have me baffled.

    I noticed you used game.Screen.Size.Width which returns 1024 in iPad size. I am using "OVERSCAN" (Sorry about the caps just making sure this doesn't get missed) which magnifies the image by 2.4 and makes the width appear 1152 wide with the sides cropped to the view size of 1024 but self.Position.X is still calculated as if it were on a 480 wide screen so I don't see how game.Screen.Size.Width can work.

    Now that I know Preview is working at 480 size I have adjusted my code. The project I have attached has my code in Actor 1 which now looks to be working but is really basic and I cheated to get the number 14.21052 because I couldn't work out the math. It's close but not right.

    Could you put your code in Actor 1 overwriting mine in my attached project so I can see it working and maybe begin to understand it. Actor 1 has my code, Actor 2 is just there to see what happens to them when the view is changed and actor 3 is approximately where I want them to end up.

    Thank you and everyone for your help with this and remember when you start Preview select iPad Landscape with Overscan.
  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    Okay with Overscan you can figure out how to place things based on screen ratio.

    Original Ratio is the ratio of the Screen.Width/Screen.Height of the project as you build it. In this case of iPhone Legacy, it is 480/320 = 1.5
    Using this, when you use Overscan, any ratio(of the current device screen.width/screen.height) greater or equal to this original ratio will mean the screen will stretch the width to fit the screen and cut off from the top or bottom as needed. Otherwise if the ratio is less than the original ratio, it will stretch the height to fit the screen and cut off from the left and right of the screen.

    Given attributes OriginalRatio of 1.5 and OriginalWidth of 480

    Rule: If OriginalRatio ≤ game.Screen.Size.Width/game.Screen.Size.Height ---Change Attribute: self.Position.X = self.OriginalWidth*max(0,min(1,self.Position.X-self.OriginalWidth/2))+max(-1,min(1,self.OriginalWidth/2-self.Position.X))*(self.Size.Width*.5) Otherwise: ---Change Attribute: self.Position.X = self.OriginalWidth*max(0,min(1,self.Position.X-self.OriginalWidth/2))+max(-1,min(1,self.OriginalWidth/2-self.Position.X))*(self.Size.Width/2+self.OriginalWidth*(1-game.Screen.Size.Width/(game.Screen.Size.Height*self.OriginalRatio))/2)

    You can copy and paste these and replace the attributes as needed. Make sure the extra spaces are removed and parentheses are all accounted for.
  • colandercolander Member Posts: 1,610
    Wow thanks CodeMonkey. it's a lot to take in for my level of math. I pulled your previous code apart and was starting to understand it so this will take awhile. I will get back here once I have it going or not :)
  • colandercolander Member Posts: 1,610
    edited November 2013
    @CodeMonkey I got it all working I used a game level attributes for OriginalWidth and OriginalRatio so it can be easily used in other actors. But if I move the actors position then run it it moves the actor back to the edge of the screen, a predetermined x position which means it won't work for the actors in my game.

    I don't need to change the size of any of my actors for iPhone 5 just the position of the HUD actors. I always new I would have to reduce the size of all my actors by the same factor for iPad and thought it would be the easy part "self.Size.Width/2.4*2". I need it to be to within 0.1 of a pixel for some of my actors so I don't think my simple equation will work.

    My game is a vertical scroller so I need to keep the iPad screen height but bring all my actors back on the screen an also in the correct size and positions so everything works. I have a ball rolling across uneven ground actors and all of them have to match up exactly for it to work.

    My brain is nearly fried from 3+ months of writing my game and learning GS in the process so I think I will leave it for now and come back to this another time. I have made two submissions iPhone and iPad for my game which is obviously two separate binaries, not ideal but will have to do for now.

    If someone else would like to continue this thread feel free to do so.
  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    You can replace that self.Size.Width*.5 with a hard value. I just used that to get the actors to be flush to sides.
  • colandercolander Member Posts: 1,610
    @CodeMonkey ok I can't help myself. I replaced self.Size.Width*.5 with min(self.Position.X, game.origWidth - max(game.origWidth/2, self.Position.X)). It works but is it the best way to do it?

    Where do I start with iPad position.X in Otherwise? I had a look but my brain started sizzling :)
  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    self.Size.Width/2 = self.Size.Width*.5 so wherever you see half-width replace that.

    Not sure what you want to accomplish with the min(self.Position.X, game.origWidth - max(game.origWidth/2, self.Position.X)) so I can't say either way.
  • colandercolander Member Posts: 1,610
    @CodeMonkey selfPosition.X*.5 always puts the actor against the edge of the screen. I mean if I shift the position,X of an actor on the left from 50 to 150 and then run it in Preview it will shift it back to 50. Changing selfPosition.X*.5 to min(self.Position.X, game.origWidth - max(game.origWidth/2, self.Position.X)) will keep the actor at 150.

    The same problem occurs with the formula for iPad in the Otherwise section of the rule, I will go and have a look at self.Size.Width/2 in it over the next few days. Thanks for your help.
  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    try replacing all the half width values with 150 if you want it always to be at 150.
  • colandercolander Member Posts: 1,610
    @CodeMonkey I have a lot of actors to move too many to do each individually and I might use it in my next project so I am trying to develop a one size formula to fit all scenarios for a universal build.
Sign In or Register to comment.