Move an object with another but also by itself?

For questions about using Classic.

Post » Fri Sep 23, 2011 2:31 am

What I mean is,
is it possible to move objectA with objectB, however you can also move
objectB around as it's movement still stays relative to objectA.

So for random example, say I had an airplane that was moving along
the screen, but I wanted a bird to stick with the airplane as it moved,
however I also wanted to move this bird around that location.

Could anyone please let me know of a possible solution to this?
I'd greatly appreciate any advice :)!
Posts: 58
Reputation: 1,316

Post » Fri Sep 23, 2011 9:06 pm

Have a look at the image:

Object A is the one that moves, Object B is the one that moves around Object A. The green rectangle defines the region in which Object B moves around Object A.
You have to do two steps: Create a movement within a region with origin (0, 0) and set the boundaries relative to Object A's hotspot.
Let's say, the green rectangle represents a region of 200x300 pixel. The points P1 to P4 then are (0, 0), (199, 0), (0, 299), (199, 299). Object B has 7 private variables: nx, ny, ox, oy, cx, cy, t
cx and cy represent the current position within the region, nx and ny represent the new position that Object B has to reach, ox and oy the last position that was reached and t is the t-value for the lerp expression.

Some random starting position (you could as well just set the initial values of the PVs):
+ Start of layout
-> Object B: Set 'cx' to 80
-> Object B: Set 'cy' to 90
-> Object B: Set 'nx' to 80
-> Object B: Set 'ny' to 90
-> Object B: Set 'ox' to 80
-> Object B: Set 'oy' to 90
-> Object B: Set 'tx' to 1

If Object B reaches nx/ny set a new position (use 'Pick by evaluate'):
+ ObjectB: Pick by Round('cx') = 'nx' and Round('cy') = 'ny'
-> ObjectB: Set 't' to 0
-> ObjectB: Set 'ox' to 'nx'
-> ObjectB: Set 'oy' to 'ny'
-> ObjectB: Set 'nx' to Random(200)
-> ObjectB: Set 'ny' to Random(300)

While valid calculate the current position within the virtual region and make the region relative to Object A's position:
+ System: Always (every tick)
-> ObjectB: Add 0.5 * TimeDelta to 't'
-> ObjectB: Set 'cx' to Lerp('ox', 'nx', 't')
-> ObjectB: Set 'cy' to Lerp('oy', 'ny', 't')
-> ObjectB: Set position to ObjectA.X - 100 + 'cx', ObjectA.Y - 150 + 'cy'

This is just one movement style for Object B. You would have to adapt your own to this scheme. For example, the region is set centered around Object A's hotspot. You might want to have it not centered. Then calculate the "hotspot" of the region accordingly in the last action of the "always"-event. But the basics stay the same: "Move" the object within a virtual region. Then set the region relative to the first object's position.

Here is the .cap:
Posts: 1,821
Reputation: 8,289

Post » Thu Sep 29, 2011 12:53 pm

Thanks so very much for your incredibly deep response and example!!
Sorry this reply is so late, had a lot of things come up recently,
and this took forever to workout.

Eventually though, thanks to your demo :D, I did manage to workout
a solution. Your example certainly gave me the headstart I needed,
but still I found whenever I wanted to control the motion myself,
it would effect it's the location independently of objectA.
However finally, finally @[email protected] *collapses* /knows I fail,
I managed to get it down to a very simple solution :D....

Based on your offsetting the location from ObjectA,
I have a Sprite object with 8direction behavior, that stores it's
original location in a private variable on layer start.
Then it adds any change from that location, to the variable
that ObjectB is offset from ObjectA.

Here's the .cap -

Again, thank you so very much, I know this end solution feels blindly
simple for an experienced user, but I couldn't have gotten here without
your help, thanks :D!
Also I never knew about lerp, definitely something that should come
in handy in the future from what I read about it after some searching :).
Thanks for sharing your experience :3!sparkfeather2011-09-29 12:55:08
Posts: 58
Reputation: 1,316

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 1 guest