How do I set angle toward mouse but stop within certain #°

Get help using Construct 2

» Mon Feb 27, 2017 9:27 pm

I have the angle working, but I have no idea how I can stop the object "rotating" towards the mouse. My character has a gun and it rotates towards the mouse and the gun flips and the character gets mirrored when the mouse is either side of the character.

The thing is, I do not want the gun to keep rotating past a certain degree, as at the moment it will clip inside the player. I want to try and have it so it only rotates along an arc of about 80° .

Does anyone know how to do this?
B
15
S
8
G
2
Posts: 68
Reputation: 2,465

» Mon Feb 27, 2017 11:09 pm

rotate towards min(the_angle, max_angle)

min() = take the lowest of both values
the_angle = angle towards mouse
max_angle = the maximum that you allow it to rotate
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

» Mon Feb 27, 2017 11:26 pm

I can't seem to find the right event to use within the angle section. I've tried different things but I'm not 100% and nothing has worked. Could you put a quick example together please?
B
15
S
8
G
2
Posts: 68
Reputation: 2,465

» Tue Feb 28, 2017 12:32 pm

I always forget that angles increase/decrease different around zero point.
I also wanted it to work flawless when the dude jumps.

So i used a little trick.

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

Hope it brings you somewhere.
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

» Tue Feb 28, 2017 5:46 pm

SpyDaniel wrote:I can't seem to find the right event to use within the angle section. I've tried different things but I'm not 100% and nothing has worked. Could you put a quick example together please?

I looked at your CAPX but because you are using Pin To and set angle and position that interferes with trying to read the angle and keep it from going past 270 and below 40 degrees.

I tried several things but that Pin keeps over riding it and locking the head in weird positions.

I think you need a rotation joint and physics to accomplish what you are trying to do or use a blank sprite as a block to keep head from over rotating.
Banned User
B
29
S
7
G
58
Posts: 1,229
Reputation: 34,910

» Tue Feb 28, 2017 8:17 pm

Thanks @99Instances2Go, I don't actually understand what all the maths mean that gets the gun to rotate, such as "clamp" and "sin". I guess I could copy it to my game, but I would not really ever understand how it works.
B
15
S
8
G
2
Posts: 68
Reputation: 2,465

» Tue Feb 28, 2017 9:18 pm

clamp(x, lower, upper)
Return lower if x is less than lower, upper if x is greater than upper, else return x.

As long as X is between 'lower' and 'upper', the result = x
If x is lower then 'lower', the result is 'lower'
If X is higher then 'upper', the result is 'upper'.

Say you want to constrain the Y position of a sprite between 100 pixels and 500 pixels.
Set y of the sprite to clamp(sprite.y , 100 , 500)

sin(angle).
Say you have a bullet flying at an angle. If the bullet travels 1 unit (in the direction of the angle) then it travelled a distance of sin(angle) on the Y and cos(angle) on the X.
Easier said: To move a bullet at a angle of 60 degrees, you move it cos(60) in the X and sin(60) on the Y.
And that is all there is to say about cos(angle) and sin(angle), except maybe that is scalable.
If you move a bullet at x= cos(60) * 50 & y=sin(60) * 50, it still moves at an angle of 60 degrees, just 50 times faster.
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

» Tue Feb 28, 2017 10:09 pm

SpyDaniel wrote:I have the angle working, but I have no idea how I can stop the object "rotating" towards the mouse. My character has a gun and it rotates towards the mouse and the gun flips and the character gets mirrored when the mouse is either side of the character.

The thing is, I do not want the gun to keep rotating past a certain degree, as at the moment it will clip inside the player. I want to try and have it so it only rotates along an arc of about 80° .

Does anyone know how to do this?

That prevents the head from rotating beyond 270 and 90.
Banned User
B
29
S
7
G
58
Posts: 1,229
Reputation: 34,910

» Fri Mar 03, 2017 12:47 am

@99Instances2Go Thanks for explaining!

@lamar I did not include any example of my game, so I'm not sure if you have mixed up 99Instances2Go example to be my work?
B
15
S
8
G
2
Posts: 68
Reputation: 2,465

» Fri Mar 03, 2017 1:35 am

SpyDaniel wrote:@99Instances2Go Thanks for explaining!

@lamar I did not include any example of my game, so I'm not sure if you have mixed up 99Instances2Go example to be my work?

Are you saying this robot with the head that kept rotating was not yours because I am sure you posted that link?

Maybe you just like to play games in the forum.
Banned User
B
29
S
7
G
58
Posts: 1,229
Reputation: 34,910

Next