GML [Solved] Math/logic brickwall: lengthdir

JeffJ

Member
1: I have a rectangular area that can move around and rotate
2: I spawn a number of small objects in a random area within that area
3: These small objects have a direction and speed of their own
4: I need these objects to follow their "parent square" with respect to their current position

This works fine, but breaks the moment I try to make the following based on a vector rather than simple coordinates.

The thing is, it's pretty much like spawning a bullet at the tip of a gun with the lengthdir functions, except here the position isn't static in the same way, and for some reason I've just hit a logic brick wall now.

This is what I currently have in the small spawneable object's step event:
Code:
//objController is the rectangular area that moves and rotates around
direction=objController.angle-global.swordAnglePrev+90 //swordAnglePrev is objController's direction set in its end step event

deltaX=objController.x-objController.xprevious;
deltaY=objController.y-objController.yprevious;
x+=deltaX;
y+=deltaY;

myOffsetX=myStartX-objController.x;
myOffsetY=myStartY-objController.y;

lX=objController.x+lengthdir_x(myOffsetX,objController.image_angle);
lY=objController.y+lengthdir_y(myOffsetY,objController.image_angle);
x=lX
y=lY
What I'm trying to do here is first make it follow the coordinates. Then I take the object's starting position (basically its xstart/ystart) and subtract the rectangle's coordinates to get the distance from the start of the square to the object's position. I then use that distance to try and calculate a vector and offset the position using that, but the positions ends up being all over the place. I've tried several different variants of the above, and I'm clearly just drawing a logical blank.

If you'd prefer to see the whole project, here it is - it's just two objects and there are printed control instructions on the screen when you run it.

http://megafuzz.com/CustomParticle00.yyz


EDIT:

If I just do this in the step event:
Code:
direction=objController.angle-global.swordAnglePrev+90

deltaX=objController.x-objController.xprevious;
deltaY=objController.y-objController.yprevious;
x+=deltaX;
y+=deltaY;
It's pretty close to what I want, but then the issue is that the objects don't follow the rotations. I need them to not only follow based on the rotation, but also maintain their motion.
 
Last edited:

samspade

Member
I'm a little confused about a couple things. You say it breaks if you try to use vectors but you have it working without vectors? Are vectors necessary? Or do you mean it works when it is stationary, but not when mobile?

As far as movement, do you mean that it just isn't seeking properly period, or that in addition to seeking the point (for example, birds trying to land on a moving rectangle), or the main object's movement needs to be added to the sub object (for example, fish in a fish tank, you move or rotate the fish tank and the fish move with it)?

If you are using vectors, do you have vector scripts, including steering behaviors?
 

JeffJ

Member
I'm a little confused about a couple things. You say it breaks if you try to use vectors but you have it working without vectors? Are vectors necessary? Or do you mean it works when it is stationary, but not when mobile?

As far as movement, do you mean that it just isn't seeking properly period, or that in addition to seeking the point (for example, birds trying to land on a moving rectangle), or the main object's movement needs to be added to the sub object (for example, fish in a fish tank, you move or rotate the fish tank and the fish move with it)?

If you are using vectors, do you have vector scripts, including steering behaviors?
I don't think I could do this without a vector. What I need is for the object inside the area to follow along with the area's movement and rotation, while maintaining its own position relative to those movements. But I may be wrong. Currently this is what I'm trying to achieve with the lengthdir functions. Like I said, it's kind of like trying to ensure that a bullet spawns at the tip of a rotating and moving gun, except the positions here are random, plus the objects needs to still maintain their motion.

Maybe it would make more sense if you look at the project itself (linked in the OP), but otherwise here's a more visual explanation - let me know if this helps.
 

Attachments

RujiK

Member
I changed your create code of the particle object to this:
Code:
instance_create_depth(random(myW),random(myH),-1,objCustomParticle);
And then in the the particle object I put these
Code:
//CREATE:
myStartX=x;
myStartY=y;
Code:
//STEP EVENT
var dir = objController.myDir;

var xs = myStartX;
var ys = myStartY;

var origin_x = 0; //origin that the rectangle rotates on. In your case it's 0,0
var origin_y = 0;

var x0 = origin_x + objController.x;
var y0 = origin_y + objController.y;

x = x0+(xs-origin_x)*dcos(dir)+(ys-origin_y)*dsin(dir)
y = y0-(xs-origin_x)*dsin(dir)+(ys-origin_y)*dcos(dir)
And a few changes in event user 0:
Code:
//x=global.cpX;
//y=global.cpY;
//myStartX=x-objController.x;
//myStartY=y-objController.y;

myStartX = random(objController.myW)
myStartY = random(objController.myH)
dirCurrent=random_range(dirMin,dirMax);
spdCurrent=random_range(spdMin,spdMax);
sizCurrent=random_range(sizMin,sizMax);
lifCurrent=random_range(lifMin,lifMax);
direction=dirCurrent;
speed=spdCurrent;
image_xscale=sizCurrent;
image_yscale=sizCurrent;
Weeee:
 

JeffJ

Member
That looks very promising! But would this actually be able to also accommodate the fact that the particles needs to maintain their own motion (direction+speed)?

Ideally, it should look like this (but obviously with the particles following the rotation):

 

samspade

Member
That looks very promising! But would this actually be able to also accommodate the fact that the particles needs to maintain their own motion (direction+speed)?

Ideally, it should look like this (but obviously with the particles following the rotation):

No that I understand the problem. I don't know enough to answer. It does look good though as is. If that is supposed to be something coming off of something else I also think it looks more realistic like that as well.
 

JeffJ

Member
For relative motion, instead of adding onto x and y, add onto "myStartX" and "myStartY"
I'm not sure what you mean? I assume you're talking about these two lines, right?
Code:
x = x0+(xs-origin_x)*dcos(dir)+(ys-origin_y)*dsin(dir)
y = y0-(xs-origin_x)*dsin(dir)+(ys-origin_y)*dcos(dir)

No that I understand the problem. I don't know enough to answer. It does look good though as is. If that is supposed to be something coming off of something else I also think it looks more realistic like that as well.
Thanks, but it's going to be used in a situation where it won't look good unless the following is immediate, hence why I'm really trying to achieve this effect. :)
 

RujiK

Member
No, I'm not talking about those two lines. Those lines effect x and y. Did you forget about your variables myStartX and myStartY? Just add to them mathematically for relative movement.

Meaning:
For ABSOLUTE movement you do something like this:
Code:
x+=x_velocity;
y+=y_velocity;
but because you want RELATIVE motion, do this:
Code:
myStartX+=x_velocity;
myStartY+=y_velocity;
Put the above code in the step event and it should work. You will need to define x_velocity and y_velocity of course.
 

JeffJ

Member
No, I'm not talking about those two lines. Those lines effect x and y. Did you forget about your variables myStartX and myStartY? Just add to them mathematically for relative movement.

Meaning:
For ABSOLUTE movement you do something like this:
Code:
x+=x_velocity;
y+=y_velocity;
but because you want RELATIVE motion, do this:
Code:
myStartX+=x_velocity;
myStartY+=y_velocity;
Put the above code in the step event and it should work. You will need to define x_velocity and y_velocity of course.
Ah, yes, much better!

One last thing, though. With this method I can't seem to control the particle's direction as before? As it is now, they're sort of flying down at a slightly odd angle. I'm trying to make them rise upwards from the controller between 85 and 95 degrees as before, but can't seem to achieve the same result as before by setting their direction anymore. And doing something like
Code:
var dir = objController.image_angle+90;
Makes it even worse, with them not even really being aligned with the controller anymore, but I'm pretty sure it's (once again) down to me not understanding the math behind it.
 

JeffJ

Member
Do you want motion in 85-95 degrees in relation to the rectangle or in relation to the screen? In either case, here is an example that should explain both:
http://www.filedropper.com/relativemotion

It is GMS 1.4 but it should import to GMS 2.0 with no problems.
Seriously, I can't thank you enough! You've really been a huge help, and put waaay more effort into helping than I'd ever have imagined any stranger on a forum would!

When it comes to these kinds of logic/math based issues, my brain has a tendency to hit a wall at some point, and that example you just provided was the final push for me to break through that wall. It really helped tremendously. Thank you for bearing with me and helping so much!
 
Top