# Moving an object to block a friend from an enemy?

0 favourites
• 3 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
• Say you have three objects in a layout. Lets say one is a (slowly moving) bullet type object headed towards a target object. A third object is invulnerable to the 'bullet' and wants get between the bullet and the target to intercept, or block, the bullet and protect the target. How could one achieve this in C2?

So far I am using the custom movement behaviour for the 'blocker' object for other purposes, so a solution using that would be best.

Here is my pseudo code solution, but I don't know how to do this in C2?

* Find the bullet nearest to the target (in case there are more than one)

* Find the point of intersection between the line between target and bullet and the shortest line between the blocker and this line ( I hope that makes sense)

* Set angle of motion of blocker to that point

* Set the speed of the blocker to as fast as possible (limited by an instance variable of the blocker)

Note that this doesn't anticipate the bullet's motion, so isn't the most optimal solution for the blocker to save the target, but it would be a start.

I can't find the required information in order to construct a (complex!) expression that could be used to set the blockers angle of motion. How do I determine the location of the closest bullet? The location of the target I can do.

I know C2 is supposed to be 'programming free' but is it possible to in expressions to create temporary variables in some way? I need to set the blockers angle of motion, but first I need to work out the point to set the motion towards, then do some trig to get the angle. This would be a mighty ugly site as a single expression, even if I could find the required raw numbers to put it together. If I could insert a small 'script' in place of the expression that would be clearest.

Any ideas?

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Say you have three objects in a layout. Lets say one is a (slowly moving) bullet type object headed towards a target object. A third object is invulnerable to the 'bullet' and wants get between the bullet and the target to intercept, or block, the bullet and protect the target. How could one achieve this in C2?

So far I am using the custom movement behaviour for the 'blocker' object for other purposes, so a solution using that would be best.

Here is my pseudo code solution, but I don't know how to do this in C2?

* Find the bullet nearest to the target (in case there are more than one)

* Find the point of intersection between the line between target and bullet and the shortest line between the blocker and this line ( I hope that makes sense)

* Set angle of motion of blocker to that point

* Set the speed of the blocker to as fast as possible (limited by an instance variable of the blocker)

Note that this doesn't anticipate the bullet's motion, so isn't the most optimal solution for the blocker to save the target, but it would be a start.

I can't find the required information in order to construct a (complex!) expression that could be used to set the blockers angle of motion. How do I determine the location of the closest bullet? The location of the target I can do.

I know C2 is supposed to be 'programming free' but is it possible to in expressions to create temporary variables in some way? I need to set the blockers angle of motion, but first I need to work out the point to set the motion towards, then do some trig to get the angle. This would be a mighty ugly site as a single expression, even if I could find the required raw numbers to put it together. If I could insert a small 'script' in place of the expression that would be clearest.

Any ideas?

* Find the bullet nearest to the target (in case there are more than one)

(Assuming there is one target)

Bullet Pick Nearest To Target.X, Target.Y

Bullet is now picked (check the FAQ thread to see what that means) and you can do stuff with it, like store it's location

* Find the point of intersection between the line between target and bullet and the shortest line between the blocker and this line ( I hope that makes sense)

Use Distance(target.x,target.y, bullet.x, bullet.y) to find the distance between bullet and the target, then use mathemagic to take accelarations, speeds and whatevers into account and create a formula to find the desired target X,Y location.

* Set angle of motion of blocker to that point

Custom movement (probably) has 'Set angle of motion' action, use Angle(Blocker.x, blocker.y, targetLocation.x, targetLocation.Y) to set it towards your calculated point.

* Set the speed of the blocker to as fast as possible (limited by an instance variable of the blocker)

Custom movement (probably) has 'set speed' action to do this :)

• Surely if you made the blocker angle towards the bullet and then move forwards (x pixels depending on what speed you want) on every frame, then it'd work as you want anyway?

So every frame it would look for the bullet's current position and move towards it.

Would work better if the blocker had it's origin in the middle, that way the edge of the blocker shape would hit the bullet earlier.

Also you'll have to be careful about when you "pick nearest" cos if you do that every frame then the blocker will change his mind over and over about which bullet to head to. I suggest having a "TargetUID" instance variable on the blocker, set when a bullet is first in his range - so the first bullet that gets into range is what he will try to block. Once that bullet has gone, he will re-target the nearest again.

• 3 posts