Jump to content

Homing projectile


chimera27

Recommended Posts

I'm trying to make a projectile that will home in on the nearest enemy, but have no idea where to start. Is there any function that can give coords of the nearest mob or something like that? I really don't know what to do, there isn't much documentation on this...

Creator of Metroid Cubed! Power Suits, Beams, Hypermode and more!

width=174 height=100http://i.imgur.com/ghgWmA3.jpg[/img]

Link to comment
Share on other sites

Yep you can use an AABB, center it on the player and make it the size you want the range to be.

Use an Iterate over all entities inside the AABB, keep track of the one closest to the player.

Then fire projectile towards the target.

 

There is no documentation on how to do this kind of thing, there is documentation for most of the methods you can use.

Documentation doesn't tell you every scenario you can use something in, it just tells you how it's supposed to work and you will have to decide when and how to use it for your purposes.

 

Welcome to programming, it's more about designing the logic than it's about typing the code.

 

If you guys dont get it.. then well ya.. try harder...

Link to comment
Share on other sites

Yeah as I said there is NO method made for doing that specifically but some things such as the AABB could be used to accomplish this goal.

There are probably several other ways, but the best way that I currently know of is to use the bounding box for it :)

 

Good luck!

If you guys dont get it.. then well ya.. try harder...

Link to comment
Share on other sites

Hmm ok I got the AABB set up but am not sure what to do with it, IE how to redirect my projectile towards said entity. I don't see a way to get the coords of the entity either, did I do it right? This is what I did:

AxisAlignedBB bb = AxisAlignedBB.getBoundingBox(this.posX - 16, this.posY, this.posZ - 16, this.posX + 16, this.posY + 16, this.posZ + 16);


public void getNearestEntity(){
	List<IMob> entities = worldObj.getEntitiesWithinAABB(IMob.class, bb);
	Iterator iter = entities.iterator();
	while(iter.hasNext()){
		if(iter.next() instanceof IMob){
		IMob ent = (IMob)iter.next();
		//What do I do from here?
		}
		}
}

 

Sorry if it's really obvious and i'm just really stupid, i'm still not too experienced (IK java i'm just not used to working with it in this way)

Creator of Metroid Cubed! Power Suits, Beams, Hypermode and more!

width=174 height=100http://i.imgur.com/ghgWmA3.jpg[/img]

Link to comment
Share on other sites

Hi

 

So what you are basically trying to do is

In your entity onUpdate:

(1) create a bounding box centred around where the projectile currently is.

(2) find all entities within that bounding box

(3) loop through them all:

  (a) if this isn't a valid target, ignore it

  (b) calculate the distance from the target to the projectile.  If it's less than the shortest distance found so far, this is now your closest target.

(4) change motionX, motionY, motionZ to point to the closest target

 

You can probably copy the onUpdate code from EntityThrowable and tweak it to add the code above.

It looks like you've figured out (1) and (2) already, just need to code up the rest.  It won't do anything fancy like check whether the missile can actually see the target (blocks in the way), but you could add that later if you really want, once the basics are working?

 

-TGG

 

PS  It looks like this bloke is also doing something very similar to what you want, perhaps you should message him too...

http://www.minecraftforge.net/forum/index.php/topic,13829.0.html

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.