# Moving an object based on it's angle

This forum is currently in read-only mode.
0 Favourites
• 9 posts
• Let's say I have an object facing right, and I want it to move 2px left. Pretty easy, huh?

But what if I have an object facing 37� and I want it to move 2 pixels backward? I know there should be some math thing, but I'm not an expert with maths...

Also, here's a picture to explain what I want.

<img src="http://img03.picoodle.com/img/img03/4/4/20/f_Untitledm_05f7ffa.png">

• + Always

Set X to .X + cos(.Angle) * 100 * TimeDelta

Set Y to .Y + sin(.Angle) * 100 * TimeDelta

'100' means it moves at 100 pixels per second. Replace it with a private variable and you can control the speed.

• You could do it like this:

``````+ On "Beat Horse" pressed
+ horse.Value('life') Equal to 0
[ul]
[li]SuperV: Set position to 4chan[/li]
[/ul][/code:1359bne6]``````
• You could do it like this:

``````> + On "Beat Horse" pressed
+ horse.Value('life') Equal to 0
- SuperV: Set position to 4chan
[/code:3oikgn2x]
``````

Glad you liked the bonus ^^

---

Ashley, that worked fine, but what if I need to re-position gradually the object to it's original position? What's the best solution?

• What do you mean? What is the original position and how do you want it to move there?

• What do you mean? What is the original position and how do you want it to move there?

I'll explain to you what I want to achieve.

I have a Cannon turret, for my upcoming game, that is supposed to shoot big, big rockets. I want to make a realistic recoil where the turrets moves backward a few pixels then returns in the original position.

The turret's original position is the Base.X and Base.Y

What's the easiest/best solution to evitate bugs and to move the turret back to the original position?

Custom movement?

Bullet behavior?

• If you're just moving it a few pixels then why move it at all? Do it with sprite animation frames. Just make a "recoil" animation.

• A short animation would do it...

• ## Try Construct 3

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

Construct 3 users don't see these ads
• If you want to do it by adjusting the position, you can use:

+ Always

Set X to original x + cos(.Angle) * 'Recoil'

Set Y to original y + sin(.Angle) * 'Recoil'

Original x/y is the starting position of the object, eg. 320,240. 'Recoil' is a private variable which when 0 means it's at the original position, otherwise its the distance of the recoil. So if you change Recoil to 20 and gradually decrease it back to 0, it will jump 20 pixels and gradually move back to the original position.

If it goes the wrong way, use .Angle + 180.