How do I lock the angle of rotation at a certain degree

0 favourites
  • 14 posts
From the Asset Store
Avoid obstacles and survive as long as you can!
  • Hi

    I m creating a game wherein my character is holding a missile launcher in his hand, and the launcher rotates according to the mouse pointer. But in my case it rotates 360 degrees, I just want it to rotate around 110 degrees only and then stop. I have seen a few other threads but didn't get the exact technique of doing it. If people are finding it hard to understand than just think of a cannon, there are games where the cannon moves according to the mouse, but only till a set angle, not 360 degrees. I have attached a capx(not my original game work, just a block file). I want the rotation to be locked at a certain angle, can anyone please help me with it.

    Thanks,

    Karan

  • Here is a start. If the canon goes beyond 55 degrees from 0 either direction it snaps back to 0 degrees.

  • I don't want it that way, I want it to lock at the 55 degree angle if its more than that, or at the 0 degree angle if its less than that, something like that.

  • the clamp system expression could help you:

    Clamp(A, Min, Max) : return A if A is between Min and Max, Min is A is inferior to Min, Max if A is superior to Max

    However, angles are cyclic, so I don't know how the engine would react in that case

  • Aphrodite,

    Just thinking out loud: Wouldn't using startangle+degrees for max and startangle-degrees for min solve that issue?

  • Aphrodite,

    Just thinking out loud: Wouldn't using startangle+degrees for max and startangle-degrees for min solve that issue?

    Not sure, since I think C2's angle logic passes from 0 to 350 and not to -10 for exemple (not 100% sure about that), so if you clamp it to -10, it'll never clamp it

    However, by using angle() I think you should be able to go through:

    I suggest using a function with One parameter:

    On call Function "Normal_Angle" : Set return value to Angle(0, 0, cos(Function.Param(0), sin(Function.Param(0))

    Set angle to : clamp(Function.Call("Normal_Angle", Self.Angle), Function.Call("Normal_Angle", Min_Angle), Function.Call("Normal_Angle", Max_Angle))

    Where Min_angle and Max_Angle are the limit angles, that should work even if C2 changes it's way to interpret angles I think, not tested

  • > Aphrodite,

    >

    > Just thinking out loud: Wouldn't using startangle+degrees for max and startangle-degrees for min solve that issue?

    >

    Not sure, since I think C2's angle logic passes from 0 to 350 and not to -10 for exemple (not 100% sure about that), so if you clamp it to -10, it'll never clamp it

    However, by using angle() I think you should be able to go through:

    I suggest using a function with One parameter:

    On call Function "Normal_Angle" : Set return value to Angle(0, 0, cos(Function.Param(0), sin(Function.Param(0))

    Set angle to : clamp(Function.Call("Normal_Angle", Self.Angle), Function.Call("Normal_Angle", Min_Angle), Function.Call("Normal_Angle", Max_Angle))

    Where Min_angle and Max_Angle are the limit angles, that should work even if C2 changes it's way to interpret angles I think, not tested

    Yeah, guess you're right. I believe last time I tried something with clamp and angle I made sure that in the calculation the startangle was considered to be 180 and did the calculation from that, so the clamp would always be returned between 0 and 360. And calculating it back to the desired ingame angle.

    I've been out of C2 for a while, so stepping back on the bicycle ..

  • Don't think its working for me

    Can anyone modify the capx and show me how it works???

  • Capx using the code Aphrodite suggested:

  • Thanks a lot for your replies@all, I'll check the capx as soon as I reach home..

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I wasn't able to open LittleStain's file since it was made with a Beta version of C2, so maybe you already have a solution you like. If not here is a 2 line solution.

    Basically I have the system check every tick to see if the angle between the sprite and the mouse is within a range that I specify. If it is I update the angle of the sprite. So as the mouse moves out of range the angle of the sprite is no longer updated and left at the last angle instead of snapping back to 0 as in my previous example.

  • I wasn't able to open LittleStain's file since it was made with a Beta version of C2, so maybe you already have a solution you like. If not here is a 2 line solution.

    Basically I have the system check every tick to see if the angle between the sprite and the mouse is within a range that I specify. If it is I update the angle of the sprite. So as the mouse moves out of range the angle of the sprite is no longer updated and left at the last angle instead of snapping back to 0 as in my previous example.

    Haha, you've just proven myself and aphrodite wrong.

    We were concerned the negative angle wouldn't work as expected, but seeing your example it does.

  • Thank you all for providing me the solution to my issues. I will definitely make a good game thanks to your help...

  • I'm looking forward to playing it.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)