How do I limit the turning angle of a turret?

Get help using Construct 2

Post » Tue Mar 18, 2014 11:37 am

I'm using the RTS Template and have a have a tank (base sprite and turret sprite) and I want to limit the rotation angle of the tank turret. Currently it spins 360 on the base, I want to limit this so that it can only spin 130 degrees to the right or left (cannot do a complete 360).

Can anyone suggest a way of doing this (I thought it might be a behaviour of the turret object, if it is, then it isn't obvious to a newb).

Posts: 34
Reputation: 364

Post » Tue Mar 18, 2014 1:13 pm

Can you post the capx ? It much easier to help you if you attach the capx !
Posts: 295
Reputation: 6,089

Post » Tue Mar 18, 2014 2:02 pm

Think you can do it like this, however this is just a theory so might not work correctly :D

But I think you can do it by adding two checks.

first you need to know the angle of the chassis, as its based on its angle to determine the limits of the turret.

You need to calculate both limits. will call these High limit and low limit. Which simply means that high limit is the clockwise limit, and the low limit the counter clockwise limit.

To calculate the high limit.
Chassis.angle + 130 (Turret limit)

This number you need to check if is more than 360.

Chassis.angle + 130 > 360

If that's the case you know that it have passed the 360 marker and will start to go from 0 and up.
So to calculate that one, you have to do the following,

(Chassis.angle + 130) - 360

That will give you the high limit, if Chassis.angle + 130 is more than 360.

Its pretty much the same for the lower limit.

Chassis.angle - 130 < 0
To calculate the lower limit if it passes 0 angle marker.

(Chassis.angle - 130) + 360

Will give you the lower limit.

Example using real numbers:

Chassis is moving upwards at an angle of 245 and the turret is just following along at the same angle.

Lets assume that an enemy shows up at angle 60 (Somewhere behind the tank)

So to see if the turret can actually hit it, we calculate the two limits.

High limit (Chassis.angle + 130 > 360)
245 + 130 > 360 = 375

Since that pass the marker we calculate the following:
375 - 360 = 15 High limit angle

Low limit (Chassis.angle - 130 < 0)
245 - 130 < 0 = 115

Since this doesn't pass the 0 angle marker we can use the calculated angle as the lower limit.

So the tank turret will be able to shoot at everything going from Low limit angle 115 to high limit angle of 15.

So in this case it wouldn't be able to attack the enemy.

Anyway its just a theory, I haven't actually tested if it works, but think it will :D
Posts: 1,182
Reputation: 6,838

Post » Wed Mar 19, 2014 9:21 am

@Joskin I'm using the RTS Template, so File > New > Template: Real Time Strategy (RTS)

@nimos100 I'll work through this and let you know how I go, thanks for taking the time to write such a detailed answer :)
Posts: 34
Reputation: 364

Post » Sun May 25, 2014 6:07 pm



Did you solved this problem?
Can you share solution?

Posts: 669
Reputation: 5,038

Return to How do I....?

Who is online

Users browsing this forum: plinkie and 25 guests