Help with length direction (lengthdir_x and lengthdir_y) (SOLVED)

Fredrik

Member
Hey!
As you can se in the video I have an apple placed on a raft, and when I rotate the raft I want the apple's position to stay relative to the raft.
As you can se the apple's position is on the front end of the raft which is because the length value in both of the lengthdir functions are set to 10 (as seen below).

Code:
GML:
if place_meeting(x,y,parent_vehicle)
    {
    var nearest = instance_nearest(x,y,parent_vehicle);
    x = nearest.x + lengthdir_x(10,nearest.dir);
    y = nearest.y + lengthdir_y(10,nearest.dir);
    } else {scr_item_in_water(0.75,2,0);}
What I want is for the apple to stay on the position I dropped it on and not just 10, if that makes sense. I tried setting the length values to be the distance between the raft's position and the apple's position, but I couldnt get it to work.
Cant be that hard. Anyone got a clue?

Sorry for the bad quality.
 
Last edited:

Nidoking

Member
I tried setting the length values to be the distance between the raft's position and the apple's position, but I couldnt get it to work.
How did you do that? It obviously won't do that with 10.

Also, why are you using instance_nearest and not instance_place?
 

TheouAegis

Member
As Nidoking said, use instance_place() instead of place_meeting()+instance_nearest(). For starters, instance_nearest() won't always return the instance found with place_meeting(). In your case, it will, but that's a moot point. Second, you are running a function to see if there is an instance collided with at the target location, then running a separate function that loops through all the instances in the room again to find that same instance you found already. Just use instance_place(), which does all of that in one go.

Second, you need to replace "10" with a variable (or two) that you set when the apple (or whatever) is dropped onto the raft. If the raft can be rotated prior to dropping anything onto it, then you'll also need to account for the raft's current rotation. In that case, you'd subtract the angle of the raft at the time the item was placed onto it from the direction argument in lengthdir.
 

Fredrik

Member
I actually figured it out. When the apple is dropped onto the raft I set the distance and direction with

GML:
dir = point_direction(obj.x,obj.y,x,y) - obj.dir;
dis = point_distance(obj.x,obj.y,x,y);
 
Top