Attach object on a fix rotation point

T

Trontastic

Guest
Hello, first off I'd like to thank you all for fueling such a great community, I've learned a lot just by reading through the available threads here. However I'm still having some trouble with a concept. I'm working on a prototype for a top down shooter, in the shooter I intend to have multiple items and guns for the player to use. The issue I'm facing is that I cannot seem to get the gun to attach and rotate around the player object on a fixed point without it rotating itself. I've uploaded a couple of images of what I'm trying to achieve.

I initially had the draw event for the gun in the player object but I've realized that this will not work when it comes to adding new weapons later, so I've been attempting to have the gun draw itself at this point near the player. Here is the code I've tried to get working.

STEP
if (instance_exists(obj_player)) {
x = attached.x + lengthdir_x(offsetDist, offsetDir + attached.image_angle);
y = attached.y + lengthdir_y(offsetDist, offsetDir + attached.image_angle);
image_angle = offsetDir + obj_player.image_angle + initialAngle;
}

Originally I had the gun being draw by the player object with

DRAW
draw_sprite_ext(spr_gun,0,(x + lengthdir_x(48, image_angle) - lengthdir_y(48, image_angle)),(y + lengthdir_y(48, image_angle) + lengthdir_x(48, image_angle)),.4,.4,image_angle,c_white,1);

But realized that this would pose problems later even if I got the gun to rotate properly. So I'm looking for any input on the best way to achieve this goal.

I tried a similar draw event within the gun object itself but this didn't work. I've read some topics on this subject but none came with visual aids to reference what was actually being accomplished and the math seems to be a bit more than I can get my head around at this time. I get the concepts but I can't seem to apply them properly.

Any assistance would be greatly appreciated.andreithree.png andreitwo.png andreiorigin.png
 
G

Gre_Lor12

Guest
Hey dude! Welcome to the GMK Community!

Since the gun is a seperate object, the origin point around the sprite needs to be moved to the EDGE of the sprite.

The origin defines where the rotation and position is set from it is most likely this that will sort it.
So just go to the gun sprite, and move the origin to the edge of the sprite (the edge against the player)

Hope this helps! Reply and tell me how you got on! :D
 

Roderick

Member
You could actually set the origin outside the image, by typing into the fields. By positioning it properly, you could even set it to the same origin as the player, and just rotate it with image_angle
 
T

Trontastic

Guest
Dear lord, such a simple fix haha, I appreciate your help man. It works perfectly now. I've posted the exact code I used that worked so that anyone else who has this issue can use it. I've been struggling with this for a weeks, then took a couple weeks off and worked on another game, came back to this and just had to ask the question. But, being new to this, gotta make the newbie mistakes to not make them again in the future.

Much appreciated.

obj_player_skin: CREATE

///Create Gun
with (instance_create(x+32,y+46,obj_gun))
{
image_angle = other.image_angle;
attached = other.id;
offsetDir = x - other.x;
offsetDist = y - other.y;
initialAngle = -32;
}

///Create Gun
with (instance_create(x-32,y+46,obj_gun))
{
image_angle = other.image_angle;
attached = other.id;
offsetDir = x - other.x;
offsetDist = y - other.y;
initialAngle = +32;
}

obj_gun: END STEP

if (instance_exists(obj_player_skin)) {
x = attached.x + lengthdir_x(offsetDist, offsetDir + attached.image_angle);
y = attached.y + lengthdir_y(offsetDist, offsetDir + attached.image_angle);
image_angle = offsetDir + obj_player_skin.image_angle + initialAngle;
}


Roderick, how could this be done? It sounds like an interesting prospect that would make applying new guns to new skins easier with code. Do you have an example, or an idea how to modify my current code to do this?

Thanks to you both.
 

Attachments

Roderick

Member
Roderick, how could this be done? It sounds like an interesting prospect that would make applying new guns to new skins easier with code. Do you have an example, or an idea how to modify my current code to do this?
There are two ways. One involves just creating a sprite with a LOT of white space, so that there's room for the player sprite inside the blank space. The other is to put the origin outside the sprite, so that it does the same thing.
The latter method is better, because it wastes less memory on big empty sprites.

I put together a quick demo to show how to do these here.
 
T

Trontastic

Guest
Oh wow I see what you mean. This should help a lot when I add machine guns and actually need to rotate my player image a bit to give the proper effect for a shoulder cradled weapon. I really appreciate this, and your code is very simple and straightforward.

Thank you again Roderick.
 
S

SuG4Ru

Guest
I delete this.... I make new thread.
 
Last edited by a moderator:
Hi, not sure if you got this solved. But I often use this;

Code:
var X1 = x; // could be the player object's x, regardless, this will be the rotation point
var Y1 = y; // could be the player object's y, regardless, this will be the rotation point
var X2 = x + someoffset; // distance from the rotation point X1 above
var Y2 = y + someoffset; // distance from the rotation point Y1 above
var tdist = point_distance (X1, Y1, X2, Y2);
var tdir  = point_direction(X1, Y1, X2, Y2) + image_angle;
new_x = X1 + lengthdir_x(tdist, tdir); // the new x location after rotations (could be the gun)
new_y = Y1 + lengthdir_y(tdist, tdir); // the new y location after rotations (could be the gun)
 
S

SuG4Ru

Guest
Yep i solved. But thanks it realy helpfull your thread but first must understand what is what. After this is everythink work good
Code:
// This is code what i use and work good.

image_angle = obj_player_player1.image_angle;

x = obj_player_player1.x + lengthdir_x(100, 315 + obj_player_player1.image_angle);
y = obj_player_player1.y + lengthdir_y(100, 315 + obj_player_player1.image_angle);
image_angle = 0 + obj_player_player1.image_angle + 0;

//But i thing this is best for understanding


// Variables

image_angle           = obj_player_player1.image_angle  ;  // Copying image_angle of another object in this case image angle of object1 is apply to image_angle object2
object_to_stick       = obj_player_player1              ;  // Here add object you want to stick object1 stick to object2 [object 2 is where this code add]
angle_of_object_use   = obj_player_player1              ;  // If you want copy direction of some object and apply to this object
direction_of_object   = 315                             ;  // degreases in which way from object 1 want to show
distance_of_object_x  = 100                             ;  // distance from object
distance_of_object_y  = 100                             ;  // distance from object
angle_object          = 0                               ;  // If you want rotate object2 permanently 
initial_angle         = 0                               ;  // I think its same like angle_object

// Piece of the code

x = object_to_stick.x + lengthdir_x(distance_of_object_x, direction_of_object + object_to_stick.image_angle);
y = object_to_stick.y + lengthdir_y(distance_of_object_y, direction_of_object + object_to_stick.image_angle);
image_angle = angle_object + angle_of_object_use.image_angle + initial_angle;


//  For write possition
//
//        D        B
//        |       /
//        |      /
//        |     /
//        |    /
//        |   /
//        |  /
//        | /
//        |/_______C
//        A
//
//   A = object_to_stick       // From Variables
//   B = direction_of_object   // Which degreases is show
//   C = distance_of_object_x  // distance of object
//   D = distance_of_object_y  // distance of object
If i misstake or have something wrong just answere and i repair.

But still have a little problem when i moving the sticking object change distance. In speed 40 is distance 1/2 and in speed 10 is a distance 1/4 less.
 
Last edited by a moderator:
Top