Judging the intersection point of moving objects

Blitz3D Forums/Blitz3D Programming/Judging the intersection point of moving objects

Makepool(Posted 2005) [#1]
Imagine you have a game where an AI player fires a projectile weapon that takes time for the projectile to travel the distance to whatever the NPC is shooting at. Now imagine the NPC is shooting at a target moving laterally to the NPC. Does any body know the equation to work out what angle the NPC should shoot at to hit the moving target?


DJWoodgate(Posted 2005) [#2]
Well here is a link I found to some projectile aiming code. Intended for use in Unreal but you might be able to convert it to your situation.

http://wiki.beyondunreal.com/wiki/Projectile_Aiming


sswift(Posted 2005) [#3]
I dunno if this will help you at all, but that is the sort of problem I was working on at one point in my tank game:

			; Calculate if any bullets will hit this player.
	
				Px#  = EntityX#(ThisPlayer\Avatar, True)
				Py#  = EntityY#(ThisPlayer\Avatar, True)
				Pz#  = EntityZ#(ThisPlayer\Avatar, True)

				PVx# = ThisPlayer\Vx#
				PVz# = ThisPlayer\Vz#

				PSpeed# = Sqr#(PVx#*PVx# + PVz#*PVz#)

				Bullet_Will_Collide = False
		
				For ThisBullet.Bullet = Each Bullet			

	
					Bx#  	= EntityX#(ThisBullet\Avatar, True)
					Bz#  	= EntityZ#(ThisBullet\Avatar, True)
					BVx# 	= ThisBullet\Vx#
					BVz# 	= ThisBullet\Vz#
					BSpeed# = Sqr#(BVx#*BVx# + BVz#*BVz#)	


					; Check to see if the bullet's path intersects the computer's path, and calculate where.
					If Lines_Intersect(Px#, Pz#, Px#+PVx#, Pz#+PVz#, Bx#, Bz#, Bx#+BVx#, Bz#+BVz#)


						; Are both objects heading towards the point where their paths intersect?
						If (Intersection_AB# >= 0) And (Intersection_CD# >= 0)


							; Because the length of the line segments is equal to the distance that the entities will travel
							; over a period of 1 second, the realtive intersection points tell us the time which each entity
							; will reach the collision point.  Ie, if Intersection_AB# is 1.0, then the computer will reach
							; the intersection point in exactly one second.


							; Will the computer reach the intersection point in the near future?
							If (Intersection_AB# < 2.0)
			
				
								; Calculate the difference in the arrival time of the two entites at the intersection point.
								Arrival_Time_Diffrence# = Abs(Intersection_AB# - Intersection_CD#)
				
					
								; Will both entites arrive at almost the same time at the intersection point of their paths?
								If (Arrival_Time_Diffrence# < 2.0)
												
								
									; It's very likely that the computer is going to collide with the bullet if it keeps
									; moving in this direction.
						
									;EntityColor ThisPlayer\Avatar, 0, 255, 0
					
									Bullet_Will_Collide = True	
					
								EndIf
					
							EndIf		
				
						EndIf				
					
					EndIf			
											
				Next



Some thoughts:

For each point along the player's line of motion, there is a time it takes for the player to get there.

And each those points will also have a time for the bullet to get there.

You need to find the point where these two times are equal.

You need to find the point on the line which the player and bullet will be at at the same point in time.


Andy(Posted 2005) [#4]
Create a pivot(a) and attach another pivot(b) to it.

Move pivot b further out on one axis the faster the model moves and always orient it in the direction the model is moving. point the gun towards pivot b.

Andy


Viperfish(Posted 2005) [#5]
I'm working on a space shooter in which all craft have completely free 360 degree movment. And I had this problem. When the enemy shoot (at the player) how do they know how much to lead the target by?

It took me a long time but it can be solved with 1 formula.

Create a pivot in front of the target entity. Call it TargetLeader. This entity is always directly infront and moves in and out depending on the following formula. The attacking entity will by firing at this pivot. Create a field in the attacking entity's type called 'leader' ie 'e\leader'.

You will need the following variables.

myspeed (forward speed of the target entity.)
EnemyBulletSpeed (forward speed of the attacking entity's projectile)

Here's the formula ('pvt' is the target pivot).
e\leader = (e\leader/((e\leader/myspeed)-(EntityDistance(e\entity,TargetLeader)/EnemyBulletSpeed)+(EntityDistance(e\entity,pvt)/EnemyBulletSpeed)))*(EntityDistance(e\entity,pvt)/EnemyBulletSpeed)
PositionEntity targetleader,0,0,e\leader


The value assigned to 'e/leader' by this formula is the distance the TargetEntity should be set infront of the target. Then orientate your attacking craft towards the TargetLeader. If the attacking craft fires at this point it will hit every time.

How does it work? I wrote this a few months ago now. But basically it works out;
1. The time it will take fot the targetentity to reach the TargetLeaders position set last frame.
2. the time it will take for the enemybullet to travel to the target entity's actual position.
3. The time is will take for the enemybullet to travel to TargetLeaders Position.

It first works out how long it will take for the attacking crafts bullet to reach the targets current position.(EntityDistance(e\entity,pvt)/EnemyBulletSpeed)
This target is moving so it will get there too late and miss. If we fire at the Target Leader how long will it take? (EntityDistance(e\entity,TargetLeader)/EnemyBulletSpeed)
Then it works out how far the target will have travelled in that time (EntityDistance(e\entity,pvt)/EnemyBulletSpeed). With these three values we can now calculate the distance TargetLeader needs to be infront of the target.

It's a complicated formula and there is probably an easier way. Some methods, like Andy's method above takes the entity's speed into consideration but it doesn't take into consideration the attackers distance or the speed of the projectile. The further away the attacker is the further out the leader needs to be. The faster the projectile the closer it needs to be. This formula wraps it all together.

Anyway, I'm rambling now and this may not even be what you need. But, I hope it helps.

EDIT: Make sure you set an initial value to e\leader. It can be anything as long as it's not zero. If you leave it at zero it will never change!


Makepool(Posted 2005) [#6]
Thanks for the responses, I don't have time to test any off the ideas given as I'm really busy with uni work but in the next few weeks I hope to get things sorted.


DJWoodgate(Posted 2005) [#7]
Well I have just unravelled Johns formula and it certainly seems pretty good at getting a solution for objects moving at a fixed speed and constant direction. Here is a simple testbed.




Viperfish(Posted 2005) [#8]
Heres a link to a download of my game in which the enemy AI uses the above formula. It's a couple of months old now and has gone through a lot of changes since this upload. But you will be able to see the AI in action. When you leave the space station, start shooting at other craft and pretty soon you will be fired at and turned into space junk.

http://www.blitzcoder.com/cgi-bin/showcase/showcase_showentry.pl?id=bigjohnno03142005145609&comments=no


Chroma(Posted 2005) [#9]
I was also working on some code like this for an AA gun. This is what I came up with:

1. find the speed and vector of the object being shot at
2. find the distance between the shooter and victim
3. calculate the time it will take for the bullet to arrive
4. find the predicted spot based on speed, vector, time
5. aim and shoot at the predicted spot