Moving an object based on it's angle

For questions about using Classic.

Post » Sun Apr 20, 2008 12:33 pm

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...

Deadeye, math expert, and the whole community, help please :D

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





and a bonus : http://img28.picoodle.com/img/img28/4/4 ... bf4692.gif
B
2
S
2
G
5
Posts: 512
Reputation: 2,674

Post » Sun Apr 20, 2008 4:45 pm

+ 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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Sun Apr 20, 2008 8:58 pm

You could do it like this:

[code:1359bne6]
+ On "Beat Horse" pressed
+ horse.Value('life') Equal to 0
- SuperV: Set position to 4chan
[/code:1359bne6]
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Sun Apr 20, 2008 9:13 pm

[quote="deadeye":3oikgn2x]You could do it like this:

[code:3oikgn2x]
+ On "Beat Horse" pressed
+ horse.Value('life') Equal to 0
- SuperV: Set position to 4chan
[/code:3oikgn2x][/quote: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?
B
2
S
2
G
5
Posts: 512
Reputation: 2,674

Post » Sun Apr 20, 2008 9:15 pm

What do you mean? What is the original position and how do you want it to move there?
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Sun Apr 20, 2008 9:22 pm

[quote="Ashley":9kkpk5fu]What do you mean? What is the original position and how do you want it to move there?[/quote:9kkpk5fu]

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?
B
2
S
2
G
5
Posts: 512
Reputation: 2,674

Post » Sun Apr 20, 2008 9:23 pm

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.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Sun Apr 20, 2008 9:24 pm

A short animation would do it...
B
8
S
2
G
5
Posts: 744
Reputation: 3,288

Post » Sun Apr 20, 2008 9:29 pm

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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 3 guests