Move X relative to angle of object.

0 favourites
  • 5 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • I had previously made a post outlining the problem and the steps I had taken in order to resolve it, For some reason it has vanished.

    I'm attempting to model a steam engine, for a game I am working on.

    I have the relevant underlying mechanics sorted out, and I have a flywheel rotating at speeds depending on these underlying mechanics.

    I'm currently unable to figure out how to implement the valve rod and piston for the engine. It does not have to do anything other than be animated in relation to the flywheel.

    I currently having it travelling to the right, which is governed by the angle of the flywheel.

    But I'm not sure how to get it to travel back to the left.

    imgur.com/a/mlSDD

    In addition, here is a link to the .capx

    drive(dot)google(dot)com/file/d/0B_8zveWSf50XVEdOR1NXamN1WHc/view?usp=sharing

  • Is this what you're after?:

    https://www.dropbox.com/s/hfifnkqcvj0yd ... .capx?dl=0

    You can stop the piston tracking the angle of the flywheel by disabling the "set angle" action in both events.

    In your original code the problem was that as soon as the second event moved the piston back under 600px the first event would trigger shifting it back to 250px

  • Is this what you're after?:

    https://www.dropbox.com/s/hfifnkqcvj0yd ... .capx?dl=0

    You can stop the piston tracking the angle of the flywheel by disabling the "set angle" action in both events.

    In your original code the problem was that as soon as the second event moved the piston back under 600px the first event would trigger shifting it back to 250px

    Fantastic, thanks for the excellent example.

    The code I originally had was indeed wrong, I couldn't figure out the best way to have it behave correctly.

    What does the commas in the angle expression signify?

    Looking at the code, without understanding the purpose of the commas, here is what I have gathered:

    Every tick (and on init), the angle of the piston references the coordinates of itself and then it appears to reference an imagepoint on the flywheel.

    I have also noted that the imagepoint is defined on the sprite and is easily changed, this is great although the piston seems to fall out of 'sync' at certain points of rotation.

    The X coordinate also references the flywheel, by subtracting it's width from the flywheel.

    So in other words, the code is dynamic and should be easily adjustable.

    Thanks again, this will prove to be extremely useful for future purposes.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • wad67, You're welcome, although I also had the code wrong, hence the offset. What you really need is this:

    https://www.dropbox.com/s/2nujpt8zcmc02 ... .capx?dl=0

    I worked out the maths from this handy youtube vid:

    Subscribe to Construct videos now

    I've added the instance variables "radius" to the flywheel, and "pivotDistance" to the piston, then I calculate those lengths on start - the main reason for doing this is that the exact image-points distances are slightly less than the width ratios for those sprites. It also means we don't have to recalculate the lengths every time the positioning code block runs.

    You'll notice that I've added a sprite9 in - this makes it easier for calculating the correct piston angle; you can set it invisible if you like, or you may want to change the graphics to fit with the rest of the engine.

  • wad67

    btw, the angle expression works as follows:

    angle(x1,y,1,x2,y2)

    ...returns the angle between point (x1,y1) and (x2,y2)

    Very handy and saves you a bunch of trig.

    distance(x1,y1,x2,y2) works in a similar way

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)