I am trying to create a “piston” object for my game, where I change the width of the object from 0 to a certain value, but when I have the object rotated at an angle, like 90 degrees, it gets moved and scaled?
Is this a tiled sprite, and could you share your events? Hard to know where the issue is otherwise.
This is ultra stupid
BUT in case of vertical you need to change height not width
On top of that if you want it to grow from bottom to top you need to also change its Y pos to bottom of piston
Where for horizontal if you want it to grow from right to left you need to change x pos of it to right side of piston
That’s what I thought, but I was also wondering if there was an easy way to do this at other angles, like 45 degrees?
Width and height are applied before rotation, regardless of the order of your events. Or in other words, if you change the width or height of a rotated object, it changes the original dimensions of the unrotated version of that object.
In your GIF, the vertical object doesn’t look like it is being scaled. The thickness of the line stays constant (the height). Are you sure there isn’t some other event or behavior that is moving the object? And did you modify the origin or center points of these objects?
To stretch and position an object at any angle between two points I do this:
- Change the width of the object using DistanceBetweenPositions()
- Change the angle of the object using AngleBetweenPositions()
- Position object by its center, using the midpoint formula ((X1 + X2) / 2, and (Y1 + Y2) / 2)
Never did try it
So i have no idea
I bet there is
But most likely not pleasant one
BTW cant you just do it via animation?
These are all of my events btw.
I see… what about the object points, did you change the Origin or Center points? And do you have any other behaviors like Sticker, or Physics joints?
I don’t have any behaviors and haven’t changed any object points
I found a solution, at least one that will work for now.
I saved the center position in two variables in the object, and constantly change the center of my object to the center position.
Then I changed the sprite of my piston so that the “center” of the object is really the edge of the sprite.
If anyone can find a better way to do this it would be welcome, but that is what I will use for now.
(Sorry magicsofa if this is what you were trying to tell me, I may not have understood what you said)
Ok so I just tested this out, and the behavior you are seeing is a result of what I mentioned before: when you change the width and height, you are changing the original dimensions of the object. So if an object is rotated 90 degrees, changing the width can be thought of this way:
- Rotate the object back to 0 degrees
- Change the width
- Rotate to 90 degrees again
So, when you change the width, the center position changes too, which is where the object rotates around. Since rotation is done AFTER scaling, the final result looks like it is sliding around. But in reality, it is just rotating around where the center would have been if you had scaled the object with 0 rotation.
It is a bit confusing but it makes sense in a way because it preserves the X and Y coordinates of the object.
BUT WAIT… there is a way to circumvent this. Can you guess what it is? That’s right, if you change the origin point to the center of the object then this side effect goes away completely! The reason is quite simple… now the object will scale “from center” instead of from the origin (usually top left corner). So, scaling and rotating are done from the same point, resulting in (I think) the behavior you want.
The drawback of this is that you will have to position the object based on its center rather than the top left corner. Although, you could probably use a custom point for the top left if you wanted to.
Here’s a gif showing how it works:
All I did was move the origin point to the same position as the center point.
I didn’t know you could change the origin point to the center point
When you’re looking at the animations for the object, look at the bottom of the window for “Edit Points” button. This is where you can change the center and origin, as well as add your own custom points