Rotation problem with my turret

M

Midastouch

Guest
Hello everyone,

I try to place turrets on my grid and i have a problem.
This is a screenshoot of my problem

1601443452580.png
As you can see the gun don't rotate around the center of my bloc
My grid is 32 / 32.
And my instance turret have 2 sprites :
- the first, the bloc don't move
- the second, the gun rotate in direction of whose it focus

The bloc is a sprite 32/32 (origin 0/0)
The gun is a sprite 32/15 (origin 0/0) i change this part in draw event with "sprite_set_offset"

This is the code in step event
Code:
var enemy_nearest
enemy_nearest = instance_nearest(x,y,obj_parent_enemy)

if enemy_nearest != noone {
    distance_to_player = sqrt(sqr(x - enemy_nearest.x) + sqr(y - enemy_nearest.y));//update the distance to player

//get the player's dimensions which can then be used to figure
//out the best angle to start shooting at
player_width = enemy_nearest.sprite_width
player_height = enemy_nearest.sprite_height
player_average_size = (player_width + player_height) /2;

//Check if player is within the turret's line of sight
//AND not behind an obstruction
if distance_to_player <= los{
    xPointOfInterest = enemy_nearest.x;//update x point of interest with player's x coordinate
    yPointOfInterest = enemy_nearest.y;//update y point of interest with player's y coordinate

    active = true;//become active
}
else//otherwise
{
    active = false;//deactivate
}

ANG = point_direction(x, y, enemy_nearest.x, enemy_nearest.y) - image_angle;

if (ANG >= 360)//if the angle ever goes over 360
{
    ANG = ANG mod 360;//minus 360 from it. 361 degrees becomes 1 degree, 400 becomes 40, etc
}

if (active == true)//if active
{
    if (ANG < (player_average_size/4) || ANG > (360 - player_average_size/4))//work out if the player is is an acceptable angle away from the turret's gun on either side of its direction
    {
        can_shoot = true;//enable shooting
        image_speed = spd;
    }
    else//otherwise
    {
        can_shoot = false;//continue to deactivate shooting
    }
}
else
{
    can_shoot = false;
}

if (can_shoot == false)
{
    if (image_speed > 0)
    {
        image_speed -= 0.2;
    }
}

pointdir = point_direction(x, y, xPointOfInterest, yPointOfInterest);//find the last known direction to the player
image_angle += sin(degtorad(pointdir - image_angle)) * rspeed;//rotate smoothly

if can_shoot == true && cool_off == false {
    audio_play_sound(snd_shoot,10,false)
    var bullet_id;
    bullet_id = instance_create_depth(x + lengthdir_x(lenX, image_angle) - lengthdir_y(lenY, image_angle), y + lengthdir_y(lenX, image_angle) + lengthdir_x(lenY, image_angle),-1,obj_bullet_1);
    bullet_id.direction = image_angle;
    bullet_id.image_angle = bullet_id.direction;
    bullet_id.speed = 25;
    cool_off = true;
    alarm[0] = room_speed * 0.2;//pause for a moment
}}
This is the code in draw event
GML:
draw_sprite(spr_base_turret,-1,x,y)
sprite_set_offset(sprite_index,9,9); // to rotate around the center of the sprite gun[/U][/B]
draw_self();

I don't see how i can put my sprite at the center of my bloc but keep his origin in order to rotate normally.
 
Top