How do I Limit rotation of a sprite

Get help using Construct 2

Post » Mon Feb 29, 2016 10:01 pm

Hi there i been searching for weeks on this forum for this specific thing but i didnt have much luck i have tried everything that i could find on here and nothing worked,
-I have an Arch like object with "set angle toward (mouse.x,mouse.y) on every tick.
-and the arch set position to player image point 2 on every tick

I need this this arch to stop rotating and remain fixed if the mouse causes the angle to be above say 260 or below 45.

Also it will need need to work when the arch faces the other direction
I just dont want it to shoot more or less strait up and down so it look more natural with the player when playing shoot animation

thanks in advance
B
38
S
22
G
52
Posts: 213
Reputation: 30,009

Post » Mon Feb 29, 2016 10:07 pm

Maybe the clamp expression is what you are looking for?

https://www.scirra.com/manual/126/system-expressions

clamp(x, lower, upper)
Return lower if x is less than lower, upper if x is greater than upper, else return x.
Mistakes were made.
B
49
S
24
G
107
Posts: 1,581
Reputation: 60,303

Post » Mon Feb 29, 2016 10:22 pm

oosyrag wrote:Maybe the clamp expression is what you are looking for?

https://www.scirra.com/manual/126/system-expressions

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


thank you oosyrag for poingting me that i think it looks like the right thing but i have no idea in how to implement it so i can test it, could you give me an example so i can follow it? im new to this and i have no programing experience thanks a lot
B
38
S
22
G
52
Posts: 213
Reputation: 30,009

Post » Mon Feb 29, 2016 10:29 pm

Sorry I don't have C2 on this computer, but it will look like this:

Code: Select all
Every Tick  |  ArchObject - Set angle to clamp(angle(ArchObject.X, ArchObject.Y, Mouse.X, Mouse.Y),45,260)
Mistakes were made.
B
49
S
24
G
107
Posts: 1,581
Reputation: 60,303

Post » Mon Feb 29, 2016 10:55 pm

oosyrag wrote:Sorry I don't have C2 on this computer, but it will look like this:

Code: Select all
Every Tick  |  ArchObject - Set angle to clamp(angle(ArchObject.X, ArchObject.Y, Mouse.X, Mouse.Y),45,260)


wooow thats what i was looking for, i been going crazy with this thing, thanks huge thanks oosyrag
the only problem i have now is doesnt mirroor to the left like before, the player looks to the left but the arch keeps looking to the right, do i have to ad any extra code to do the same for when is facing the left side? i will carry on trying lets see if i can fix it, thanks for your time bro :D
B
38
S
22
G
52
Posts: 213
Reputation: 30,009

Post » Tue Mar 01, 2016 2:31 am

I manage to fix the mirroring but another problem now comes up when is mirrored at the left donsnt rotate smothly like when is at the right i cant figure out whats wrong
https://www.dropbox.com/s/ngawftt2f9sfcuo/EXAMPLE.capx?dl=0
B
38
S
22
G
52
Posts: 213
Reputation: 30,009

Post » Tue Mar 01, 2016 4:59 am

Ah this one is a little tricky, because using angle() on the other side will jump from 180 to -180, instead of giving you 360 degrees.

First thing, don't mirror the arch sprite, that will make things even more strange. For your condition, just check if the player is mirrored or not.

If not mirrored - clamp(angle(arch.X, arch.Y, Mouse.X, Mouse.Y),-25,25)

If mirrored - clamp(angle(mouse.X, mouse.Y, arch.X, arch.y)+180,155,205)
Mistakes were made.
B
49
S
24
G
107
Posts: 1,581
Reputation: 60,303

Post » Tue Mar 01, 2016 1:17 pm

oosyrag wrote:Ah this one is a little tricky, because using angle() on the other side will jump from 180 to -180, instead of giving you 360 degrees.

First thing, don't mirror the arch sprite, that will make things even more strange. For your condition, just check if the player is mirrored or not.

If not mirrored - clamp(angle(arch.X, arch.Y, Mouse.X, Mouse.Y),-25,25)

If mirrored - clamp(angle(mouse.X, mouse.Y, arch.X, arch.y)+180,155,205)



hoo yeaah is working perfect now :D :D thanks a lot for all your help oosyrag you saved me from loosing more hours, weeks just to fix this one thing jeje

do you mind if i ask you one last question, how do you learn this things? i mean if im stack like on this examples and i was totally lost as i didnt know were to look or the mathematical part, but you knew all this things were do you learned? do i have to learn any specific programming language or something else? what do you reconmend? thanks a lot bro, sorry for so many questions, take care
B
38
S
22
G
52
Posts: 213
Reputation: 30,009

Post » Tue Mar 01, 2016 6:11 pm

Well I didn't really know off the top of my head, I had to experiment a little for the second answer. It is mostly trial and error, and some basic math. So... school!

If you ever run into any programming problem, the best thing is to use debug mode to clearly see what the code is doing. Sometimes it doesn't work as you expect, as I found out with the angle() expression giving a negative value.

So basically I put the expressions angle(arch.X, arch.Y, Mouse.X, Mouse.Y) and angle(mouse.X, mouse.Y, arch.X, arch.y) into variables to see what they were exactly when I moved the mouse around. Then it was a matter of changing the numbers into something I could use by adding 180.

As for not mirroring, simpler is almost always better - easier to troubleshoot and fix. If you use mirror in this case, the up and down angles would be inverted. I'm sure you could still get it to work anyway, but it was just another layer of complexity to figure out.
Mistakes were made.
B
49
S
24
G
107
Posts: 1,581
Reputation: 60,303

Post » Tue Mar 01, 2016 8:19 pm

oosyrag wrote:Well I didn't really know off the top of my head, I had to experiment a little for the second answer. It is mostly trial and error, and some basic math. So... school!

If you ever run into any programming problem, the best thing is to use debug mode to clearly see what the code is doing. Sometimes it doesn't work as you expect, as I found out with the angle() expression giving a negative value.

So basically I put the expressions angle(arch.X, arch.Y, Mouse.X, Mouse.Y) and angle(mouse.X, mouse.Y, arch.X, arch.y) into variables to see what they were exactly when I moved the mouse around. Then it was a matter of changing the numbers into something I could use by adding 180.

As for not mirroring, simpler is almost always better - easier to troubleshoot and fix. If you use mirror in this case, the up and down angles would be inverted. I'm sure you could still get it to work anyway, but it was just another layer of complexity to figure out.


really cool staff i didnt know i can use the debug to find out whats causing the problem, that is something i will keep in mind from now on, i will have to experiment more with that to get use to it. the reason why i was asking is
becouse i see many times to use something with "expressions" is like we have to know some math and programing skills other ways we get stack example: clamp(angle(ArchObject.X, ArchObject.Y, Mouse.X, Mouse.Y),45,260) i would have never know how to figure out by my self the symbols ( () ) math etc.... thats why i was asking to see if i can learn something extra like programing etc.... to improve my experience with construct 2
and dont have to came back every time asking for help :)
just trying to do anything that i can to learn in the right way

huge thanks for all your explanations, an for your time, i learned many good things take care :D
B
38
S
22
G
52
Posts: 213
Reputation: 30,009


Return to How do I....?

Who is online

Users browsing this forum: Google [Bot], troxx, TwistedSaviour and 1 guest