• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Rotating Turret and Player Angle

N

Necromedes

Guest
I want to put a turret on my player object that will have a limited turning angle (can only turn 45 degrees in either direction.) That's not the issue, I can code that without any problems but what is the issue is the next bit of code I want to implement.
I need the turret to have a limited turning angle BUT I want it to match the angle of the player as well. So that when the player turns, the turret will be locked to the players angle and still have limited motion.
I put together some very basic code and it... kind of works. I'll post the code here:
x = obj_ship_overhead.x
y = obj_ship_overhead.y

image_angle = point_direction(x, y, mouse_x, mouse_y) + obj_ship_overhead.image_angle
if image_angle > limit_l + obj_ship_overhead.image_angle{
image_angle = limit_l + obj_ship_overhead.image_angle
}
if image_angle < limit_r + obj_ship_overhead.image_angle{
image_angle = limit_r + obj_ship_overhead.image_angle
}

As you can see, I tried to simply add the players image_angle to the existing limiting variables (simply 45, 45) and as I said above, it kind of works. The issue is that as the player turns, the angle limits of the turret don't seem to actually match up with the player. So the range of the mouses position either moves up or down depending on where the player is facing.

I hope this makes sense to people. I just don't know any other way to explain it but hopefully you can get the idea from the code snippet.

Any suggestions?
 
L

Liam_Ihasz

Guest
Ok, so if I am understanding this correctly, you want the turrets to follow your player, but only while the player is within a limited angle from the turret. (so If the player walked a circle around the turret it could only follow him part way) So all you need to do is find the position of the player relative to the turret. if the player's coordinates are between 2 degrees (like 0 and 45) set var_follow (or whatever variable) to true, otherwise make it false. Then after that, you can check to see if var_follow is true, and if it is, do your movement code. That way, it only follows the player when he is within the given angle.
 
N

Necromedes

Guest
Not quite. Let me try to explain in a better manner.

Lets say you have your player and its sprite (A starship in this case) is set to 0 degrees. The turret would be placed say, on the right side of the ship. So the player is facing 0 degrees and the turret is able to turn within 45 degrees of its angle to the left and right (pointing towards the mouse,) which works just fine.
Now if the player ship turns its angle, I need to make it to where the angle of the turret not only matches up with the ship but also can still only rotate within that 45 degree angle.
The code I included above works to an extent but as the player turns, the range of rotation of the turret while pointing towards the mouse loses sync and no longer properly moves the with mouse anymore.

Hopefully this made a little more sense.

Note: Something I forgot to mention is that I have the view angle rotating to match the player. Not sure if that will be important later but I figured I'd throw that out there just in case.
 

CMAllen

Member
Looking at your code, you shouldn't be adding the ship's angle to the turret's angle. The code you're using will set the turret's image_angle to point directly at the mouse cursor, but only if the ship is at image_angle of 0. Given that its position is being sync'd to the ship's, this means it's a separate object with its own image_angle that exists wholly independent from the ship's. The only time you want to do anything like that is if the turret's image_angle, relative to the ship's image_angle, is outside its rotation range, which you're doing with the following if() statements. Unfortunately, you don't appear to be taking into account the 360/0 rollover for image_angle. That's going to cause problems whenever the rollover point falls inside a turret's rotation range. What you want to check against is the difference between the turret's image_angle and the ship's image_angle, normalized to an image_angle of zero for the ship. Someone better versed at the requisite math should probably help you there.


The next problem you're going to encounter is that when the ship moves, the turret might be first to execute its code. The turret will sync to where the ship still is from the previous step, and then the ship moves, leaving the turret out of sync. Not at all what you want, I'm sure. You can fix this by having the ship iterate through all its turrets after it finishes moving itself, and move those turrets using a with() statement, so the turrets don't actually move themselves at all. Alternatively, you can use an earlier step layer (being step, step, end step), for the ship or a later layer for the turrets and let the turret's handle their positions as you have now. Either works. It just ensures that the the proper order of execution is respected (ship first, then its turrets).
 

TheSnidr

Heavy metal viking dentist
GMC Elder
Try this:
Code:
x = obj_ship_overhead.x
y = obj_ship_overhead.y

image_angle = obj_ship_overhead.image_angle + median(-45, 45, angle_difference(point_direction(x, y, mouse_x, mouse_y), obj_ship_overhead.image_angle))
 
Top