I'm going to give you guys a little background of what I'm trying to achieve. I'm creating a mob which moves only when not observed ( In FOV). It is a crucial part of the mob itself and cannot be completed without this method.
Even if you do not know java, this is a math problem what ever way you put it .
We are give the pitch and yaw of every entity including player. Although I'm not sure how to use these to create a ray which will represent this using the FOV. (I'm terrible at maths).
Below is the only method used to seeing if a 2 entities can see each other. Although this has a full range of view. So becomes pointless when trying to figure out if the entity is in the FOV of the other.
I'm going to give you guys a little background of what I'm trying to achieve. I'm creating a mob which moves only when not observed ( In FOV). It is a crucial part of the mob itself and cannot be completed without this method.
Even if you do not know java, this is a math problem what ever way you put it .
We are give the pitch and yaw of every entity including player. Although I'm not sure how to use these to create a ray which will represent this using the FOV. (I'm terrible at maths).
Below is the only method used to seeing if a 2 entities can see each other. Although this has a full range of view. So becomes pointless when trying to figure out if the entity is in the FOV of the other.
public void onUpdate(){
super.onUpdate();
//Get Player
EntityPlayer ep = ModLoader.getMinecraftInstance().thePlayer;
//if within FOV and can be seen, stop movement
if(isInFieldOfVision(this, ep) && canEntityBeSeen(ep))
stopMoveMent = true; //persistent variable that determines whether mob will move
else
stopMoveMent = false;
}
protected boolean isInFieldOfVision(EntityLiving e1, EntityLiving e2){
//save Entity 2's original rotation variables
float rotationYawPrime = e2.rotationYaw;
float rotationPitchPrime = e2.rotationPitch;
//make Entity 2 directly face Entity 1
e2.faceEntity(e1, 360F, 360F);
//switch values of prime rotation variables with current rotation variables
float f = e2.rotationYaw;
float f2 = e2.rotationPitch;
e2.rotationYaw = rotationYawPrime;
e2.rotationPitch = rotationPitchPrime;
rotationYawPrime = f;
rotationPitchPrime = f1;
//assuming field of vision consists of everything within X degrees from rotationYaw and Y degrees from rotationPitch, check if entity 2's current rotationYaw and rotationPitch within this X and Y range
float X = 60F; //this is only a guess, I don't know the actual range
float Y = 45F; //this is only a guess, I don't know the actual range
float yawFOVMin = e2.rotationYaw - X >= 0F ? e2.rotationYaw - X : 360F + e2.rotationYaw - X;
float yawFOVMax = e2.rotationYaw + X < 360F ? e2.rotationYaw + X : -360F + e2.rotationYaw + X;
//NOTE: I dont recall the range of rotationPitch; 0 to 180?
float pitchFOVMin = e2.rotationPitch - Y >= 0F ? e2.rotationPitch - Y : 180F + e2.rotationPitch - Y;
float pitchFOVMax = e2.rotationPitch + Y < 180F ? e2.rotationPitch + Y : -180F + e2.rotationPitch + Y;
if(rotationYawPrime >= yawFOVMin && rotationYawPrime <= yawFOVMax && rotationPitchPrime >= pitchFOVMin && rotationPitchPrime <= pitchFOVMax && e2.canEntityBeSean(e1))
return true;
else return false;
}
This is one way I think you could do it. Basically take advantage of the faceEntity method to determine what the rotationYaw and rotationPitch for the player needs to be for him/her to be directly looking at the mob. Then if the player's yaw and pitch is within a certain range from these values, you'll know that the player can see the mob.
public void onUpdate(){
super.onUpdate();
//Get Player
EntityPlayer ep = ModLoader.getMinecraftInstance().thePlayer;
//if within FOV and can be seen, stop movement
if(isInFieldOfVision(this, ep) && canEntityBeSeen(ep))
stopMoveMent = true; //persistent variable that determines whether mob will move
else
stopMoveMent = false;
}
protected boolean isInFieldOfVision(EntityLiving e1, EntityLiving e2){
//save Entity 2's original rotation variables
float rotationYawPrime = e2.rotationYaw;
float rotationPitchPrime = e2.rotationPitch;
//make Entity 2 directly face Entity 1
e2.faceEntity(e1, 360F, 360F);
//switch values of prime rotation variables with current rotation variables
float f = e2.rotationYaw;
float f2 = e2.rotationPitch;
e2.rotationYaw = rotationYawPrime;
e2.rotationPitch = rotationPitchPrime;
rotationYawPrime = f;
rotationPitchPrime = f1;
//assuming field of vision consists of everything within X degrees from rotationYaw and Y degrees from rotationPitch, check if entity 2's current rotationYaw and rotationPitch within this X and Y range
float X = 60F; //this is only a guess, I don't know the actual range
float Y = 45F; //this is only a guess, I don't know the actual range
float yawFOVMin = e2.rotationYaw - X >= 0F ? e2.rotationYaw - X : 360F + e2.rotationYaw - X;
float yawFOVMax = e2.rotationYaw + X < 360F ? e2.rotationYaw + X : -360F + e2.rotationYaw + X;
//NOTE: I dont recall the range of rotationPitch; 0 to 180?
float pitchFOVMin = e2.rotationPitch - Y >= 0F ? e2.rotationPitch - Y : 180F + e2.rotationPitch - Y;
float pitchFOVMax = e2.rotationPitch + Y < 180F ? e2.rotationPitch + Y : -180F + e2.rotationPitch + Y;
if(rotationYawPrime >= yawFOVMin && rotationYawPrime <= yawFOVMax && rotationPitchPrime >= pitchFOVMin && rotationPitchPrime <= pitchFOVMax && e2.canEntityBeSean(e1))
return true;
else return false;
}
This is one way I think you could do it. Basically take advantage of the faceEntity method to determine what the rotationYaw and rotationPitch for the player needs to be for him/her to be directly looking at the mob. Then if the player's yaw and pitch is within a certain range from these values, you'll know that the player can see the mob.
Ok I did a simple test with this using 80 for x and y and this is what printed out. But the mob didn't return true when I was staring at it.
Oh, woops. I just checked, and both Yaw and Pitch MUST be between -180F and 180F. The game never sets these variables to a number beyond this range. I also forgot to modify the code for when the max or minimum surpasses the -180(exclusive) to 180(inclusive) limit. Here's a slight edit to what I posted earlier (some things simplified):
Where are you getting those numbers from? Did you have the game automatically display these values when you look at the mob? It looks like the Yaw is outside of the -180F to 180F range I mentioned. According to those numbers, your pitch is fine, but your yaw is 388.9867F.
Can I see the code for your mob?
EDIT: Ok, I did some more research into the game's code. Apparently rotationYaw must be between 0 and 360F.
Here is an update to the code segment I posted in my last post:
I think all you need to do is call Render.shouldRender(); and it should return true if the entity should be rendered on the screen.
Keep in mind that rendering classes are client side only. To make this compatible with multiplayer, you would have to send a packet to the server that says "hey, I see this entity", and when the client no longer sees the entity, you need to send another packet that says "hey, I can't see this entity anymore". Also, if the client disconnects from the server (I.E.) bad connection, and no other client can see the entity, be sure to update accordingly.
Also note that if the player's FOV changes, it will affect this. If you don't want that to happen, you can create your own Frustrum/ICamera.
EDIT: I'm almost certain that you will have to create your own Frustrum/ICamera because of F5 mode.
Even if you do not know java, this is a math problem what ever way you put it .
We are give the pitch and yaw of every entity including player. Although I'm not sure how to use these to create a ray which will represent this using the FOV. (I'm terrible at maths).
Below is the only method used to seeing if a 2 entities can see each other. Although this has a full range of view. So becomes pointless when trying to figure out if the entity is in the FOV of the other.
public boolean canEntityBeSeen(Entity entity)
{
return worldObj.rayTraceBlocks(Vec3D.createVector(posX, posY + (double)getEyeHeight(), posZ), Vec3D.createVector(entity.posX, entity.posY + (double)entity.getEyeHeight(), entity.posZ)) == null;
}
Credit to this thread.
I suppose this boils down to something like this:
This is one way I think you could do it. Basically take advantage of the faceEntity method to determine what the rotationYaw and rotationPitch for the player needs to be for him/her to be directly looking at the mob. Then if the player's yaw and pitch is within a certain range from these values, you'll know that the player can see the mob.
Builder Mobs 0.53
My Other Mods Here
Ok I did a simple test with this using 80 for x and y and this is what printed out. But the mob didn't return true when I was staring at it.
Yaw: 461.91034 Pitch: 3.8659248 yawFovMin: 376.64978 yawFovMax: 176.64978 pitchFovMin: 135.12354 pitchFovMax: 115.12354
Oh, woops. I just checked, and both Yaw and Pitch MUST be between -180F and 180F. The game never sets these variables to a number beyond this range. I also forgot to modify the code for when the max or minimum surpasses the -180(exclusive) to 180(inclusive) limit. Here's a slight edit to what I posted earlier (some things simplified):
That should work, provided the test yaw and pitch stay within the -180 and 180 range.
Builder Mobs 0.53
My Other Mods Here
Yaw: 216.61664 Pitch: -2.2079544 yawFovMin: 328.9867 yawFovMax: 448.9867 pitchFovMin: -43.08863 pitchFovMax: 76.91137
while looking at the mob.
Where are you getting those numbers from? Did you have the game automatically display these values when you look at the mob? It looks like the Yaw is outside of the -180F to 180F range I mentioned. According to those numbers, your pitch is fine, but your yaw is 388.9867F.
Can I see the code for your mob?
EDIT: Ok, I did some more research into the game's code. Apparently rotationYaw must be between 0 and 360F.
Here is an update to the code segment I posted in my last post:
Hopefully that works.
Builder Mobs 0.53
My Other Mods Here
I think all you need to do is call Render.shouldRender(); and it should return true if the entity should be rendered on the screen.
Keep in mind that rendering classes are client side only. To make this compatible with multiplayer, you would have to send a packet to the server that says "hey, I see this entity", and when the client no longer sees the entity, you need to send another packet that says "hey, I can't see this entity anymore". Also, if the client disconnects from the server (I.E.) bad connection, and no other client can see the entity, be sure to update accordingly.
Also note that if the player's FOV changes, it will affect this. If you don't want that to happen, you can create your own Frustrum/ICamera.EDIT: I'm almost certain that you will have to create your own Frustrum/ICamera because of F5 mode.
My Github ด้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้дด็็็็็้้้้้็็็็้้้้้็็็็็้้้้้็็็็็้้้้้็็็็็้้้้้