Jump to content

[1.7.10] local entity coordinates to global coordinates


MCRaichu

Recommended Posts

Hi,

I want to spawn an entity (throwable) at a certain position on another entity. To be precise, the model of my mech has a weapon and I want to spawn the projectile at the weapon (makes sense right). I have the offset of the weapon in the mechs/model coordinates system, but to spawn it I need global coordinates and my trigonometry skills are not the best. Does anyone have an idea where I could find an example for something similar?

 

Any help is appreciated.

McRaichu

It doesn't work, I don't know why.

It works, I don't know why.

Link to comment
Share on other sites

http://lmgtfy.com/?q=local+to+global+coordinate+transformation

 

Long story short: it involves a lot of math, and its a lot of math that has existing packages to help you with.  But doing it by hand is quite possible and really not that hard, as long as you understand how to multiply matricies (the MSDN link is probably the most useful).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Why not look at EntityArrow code?

 

If you look at the constructor for EntityArrow, it has this code for setting the initial position and motion of the arrow:

 

        this.setLocationAndAngles(p_i1756_2_.posX, p_i1756_2_.posY + (double)p_i1756_2_.getEyeHeight(), p_i1756_2_.posZ, p_i1756_2_.rotationYaw, p_i1756_2_.rotationPitch);

        this.posX -= (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F);

        this.posY -= 0.10000000149011612D;

        this.posZ -= (double)(MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F);

        this.setPosition(this.posX, this.posY, this.posZ);

        this.yOffset = 0.0F;

        this.motionX = (double)(-MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI));

        this.motionZ = (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI));

        this.motionY = (double)(-MathHelper.sin(this.rotationPitch / 180.0F * (float)Math.PI));

        this.setThrowableHeading(this.motionX, this.motionY, this.motionZ, p_i1756_3_ * 1.5F, 1.0F);

 

 

 

I'm not actually sure why it calls setLocationAndAngles() because the code that follows basically changes the position and angles.

 

Anyway, what it is doing is fairly simple:

- it sets the posY to 1.0D. Since a player's eye is at 1.75D I guess that might be right to be a bit lower than eye level.

- it sets the posX and posZ based on the rotationYaw, simply using trigonometry (cosine and sine functions) to pick up each dimensional component of the angle. The key here is the 0.16D multiplier -- this is how far the offset is from the entity's center. You'll probably need to change this based on the size of your entity.

 

You'll note that it is basically shooting from the middle of the body, but slightly in front.

 

For you then, the question is whether your weapon is in the middle of the front of the entity or not. If your weapon is in the middle it is easy, just adjust the 0.16D to value that matches distance from center of your model to the weapon. Also adjust the height from 1.0D to whatever is the weapon height.

 

If your weapon is off-center then you have a bit more to do. You have to take the offset distance and use a trig function on the rotationYaw again. I think it would actually be the opposite function as used for the position since you wan't to offset at right angles to the way the entity is looking.

 

You'll probably have to play around with it, but the key is that the rotationYaw and some trig functions are likely required.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

@ Draco18s: haven't seen a lmgtfy link in a while...

 

@ jabelar: basically that's what I have done for the last 4 days...here is some code that work if the weapon is on the right-hand side...but i don't know how to adapt it for a general case. My main problem is that the rotation is sometime positive sometimes negative and just using the absolute value doesn't work. i tried.(w2X and w2Z) are the offsets)

                EntityBlasterBolt bolt = new EntityBlasterBolt(world, player);
	bolt.setLocationAndAngles(bolt.posX, bolt.posY, bolt.posZ, player.rotationYaw, player.rotationPitch);

	//generate offset based on angle
	double rotation = bolt.rotationYaw;

	double hypothe = Math.sqrt((w2X*w2X)+(w2Z*w2Z));
	double weaponAngle = Math.acos(w2Z/hypothe) * (180.0/Math.PI);

	double tempAngle = rotation + (90.0 - weaponAngle);
	if(tempAngle > 360.0)
		tempAngle -= 360.0;

	bolt.posX -= (double)(MathHelper.cos((float)tempAngle / 180.0F * (float)Math.PI) * (hypothe));
	bolt.posY -=  w2Y;
	bolt.posZ -= (double)(MathHelper.sin((float)tempAngle / 180.0F * (float)Math.PI) * (hypothe));
	bolt.setPosition(bolt.posX, bolt.posY, bolt.posZ);
        float f = 0.4F;
        bolt.rotationPitch -= 3;
        bolt.motionX = (double)(-MathHelper.sin(bolt.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(bolt.rotationPitch / 180.0F * (float)Math.PI) * f);
        bolt.motionZ = (double)(MathHelper.cos(bolt.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(bolt.rotationPitch / 180.0F * (float)Math.PI) * f);
        bolt.motionY = (double)(-MathHelper.sin((bolt.rotationPitch) / 180.0F * (float)Math.PI) * f);
        bolt.setThrowableHeading(bolt.motionX, bolt.motionY, bolt.motionZ, bolt.speed, 1.0F);
        

It doesn't work, I don't know why.

It works, I don't know why.

Link to comment
Share on other sites

My main problem is that the rotation is sometime positive sometimes negative and just using the absolute value doesn't work. i tried.(w2X and w2Z) are the offsets)

 

sin(10) = 0.174

sin(-10) = -0.174

 

You can't just arbitrarily modify the rotation value.  If you absolutely must use a positive value, add 360 (in degrees, or 2*PI in radians).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

With "sometimes positive sometimes negative" i meant the return of bolt.rotationYaw. it returns values from 0 to 360 and from 0 to -360 depending if the entity is turning right or left. this screws with my head and therefore my coding. So yeah using only positive values is easier but that doesn't help me understand the functionality behind it.

It doesn't work, I don't know why.

It works, I don't know why.

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.