Moving an object to block a friend from an enemy?

Get help using Construct 2

Post » Fri Mar 15, 2013 8:43 am

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?
B
3
Posts: 4
Reputation: 383

Post » Fri Mar 15, 2013 8:54 am

[QUOTE=Bogdanovist] 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?[/QUOTE]


* 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 :)
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Fri Mar 15, 2013 1:18 pm

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.
B
20
S
6
G
2
Posts: 157
Reputation: 3,109


Return to How do I....?

Who is online

Users browsing this forum: Calfuso, ETgirl79, totoe, Yahoo [Bot] and 22 guests