How to change the position of cutplane? HelixToolkit.Wpf - helix-3d-toolkit

I successfully cut an STL model, but I can't change the position of the cut plane.
Structure:
Viewport -> CuttingPlaneGroup -> CuttingPlanes -> Plane3D.
The STL model is in the CuttingPlaneGroup.
Does anyone know how to dynamically change the cut plane position with a slider?
If I change the position of the Plane, nothing happens.
var nj = sender as Slider;
cutPlane.Position.Offset(0, nj.Value, 0);
It seems that once you cut the STL, you can't change it anymore.
Do I have to delete the STL and reload it? That would be harsh.
The STL model is not frozen nor sealed.
EDIT:
In the source code I found if you change the "Operation" or "IsEnabled" property, it forces the model to update. Changing the normal and operation is nearly 2x faster executing than changing the IsEnabled. Still, it takes 400-500 ms to update.
EDIT2:
I took the source code and exposed private void ApplyCuttingGeometries(bool forceUpdate = false) to public void. The result is same as changing the operation (~400ms).
The code now looks like:
var nj = sender as Slider;
cp.Position = new Point3D(0, nj.Value, 0); // cp = Plane3D
ctp.ApplyCuttingGeometries(true); //ctp = CuttingPlaneGroup
Any better solution?

The cut plane in wpf version is basically create a new cut model every time.
You don't need to use cutting group. You can take a look the cut plane group source code and implement the model update yourself.

Related

Raycasting in threeJs, when target is over object

I'm fairly new to three.js and trying to get a better understanding of ray casting. I have used it so far in a game to show when an object collides with another on the page which works perfectly. In the game I'm building this is to take health from the hero as it crashes into walls.
I am now trying to implement a target which when hovered over some objects it will auto shoot. However the target only registers a target hit once the object passes through the target mesh rather than when its ray is cast through it.
to further detail this, the ray is cast from the camera through the target, if the target is on a mesh (stored as an object array) then I want it to trigger a function.
within my update function I have this:
var ray = new THREE.Raycaster();
var crossHairClone = crossHair.position.clone();
var coards = {};
coards.x = crossHairClone.x
coards.y = crossHairClone.y
ray.setFromCamera(coards, camera);
var collisionResults = ray.intersectObjects( collidableMeshList );
if ( collisionResults.length > 0 ) {
console.log('Target Hit!', collisionResults)
}
The console log is only triggered when the collidableMeshList actually touches the target mesh rather than when it is aiming at it.
How do I extend the ray to pass through the target (which I thought it was already doing) so that if anything hits the ray then it triggers my console log.
Edit
I've added a URL to the game in progress. There are other wider issues with the game, my current focus is just the target ray casting.
Game Link
Many thanks to everyone who helped me along the way on this one but I finally solved it, only not through the means that I had expected to. Upon reading into the setFromCamera() function it looks like it is mainly used around mouse coards which in my instance was not what I wanted. I wanted a target on the page and the ray to pass through this. For some reason my Ray was shooting vertically up from the centre of the whole scene.
In the end I tried a different approach and set the Rays position and direction directly rather rely on setting it from the cameras position.
var vector = new THREE.Vector3(crossHair.position.x, crossHair.position.y, crossHair.position.z);
var targetRay = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());
var enemyHit = targetRay.intersectObject( enemy );
if ( enemyHit.length > 0 ) {
console.log('Target Hit!', targetRay)
}
I'll leave this up for a while before accepting this answer in case anyone has a better approach than this or has any corrections over what I have said in regards to setFromCamera().

How to do interchangeable armor on character (low poly models)

I'm currently working on RPG graphically very similar to Runescape (old school), but with my own game system. I am not sure what is the best way to have interchangeable armor system. I am going to create low poly models, as i said, similar to runescape models like here, and want to have possibility to have random variations of armor like here.
There are two ways as I understand, first is just to change mesh of my character model, the second one is to create model of my character and use the bones weights to armor models. I know there is whole topic about the first option here (couldn't make 3rd link) forum.unity3d.com/threads/stitch-multiple-body-parts-into-one-character.16485/, but really? Every qeustion about something similar leads to this topic. But its like 8 years old and for Unity 2.0...Is there simplier way to achieve same results but don't study masterprompt's exhausting explanation which I'm not gonna be able to implement anyway. And for the second way I can't find any hints, except manual and script on unity web for bone weight, which is way too general for my use and my skills. I have spent looking for some solutions (tutorials) for hours, but google keeps popping the same topics, which are 4-8 years old...
Any advice how to choose the best way for changing armors and how to gain any knowledge about these issues? Thank You.
So what I'm doing in my projects is I duplicate my base character .blend file (rig, model) and I remove most of the mesh except for the body part that I want to make the clothing for (chest/legs). I pull it out a bit so the vertices doesn't overlap with base mesh and just save it.
Then in Unity I create a new prefab out of the model I just saved. The hierarchy of a prefab looks like this: parent game object with BoneReplacer.cs script -> child game object with skinned mesh renderer of my clothing.
Then all I have to do is just drag this prefab onto my character prefab so the target variable in this script points to my character's skinned mesh renderer.
This probably isn't the best way to do this but it works for me, maybe it will help you get your head around it and create some better solution.
Heres the BoneReplacer.cs script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoneReplacer : MonoBehaviour
{
public GameObject target;
SkinnedMeshRenderer myRenderer;
Transform[] newBones;
SkinnedMeshRenderer targetRenderer;
Dictionary<string, Transform> boneMap = new Dictionary<string, Transform>();
void Start()
{
target = transform.parent.parent.gameObject;
targetRenderer = target.GetComponentInChildren<SkinnedMeshRenderer>();
foreach (Transform _bone in targetRenderer.bones)
{
boneMap[_bone.gameObject.name] = _bone;
}
myRenderer = gameObject.GetComponent<SkinnedMeshRenderer>();
newBones = new Transform[myRenderer.bones.Length];
for (int i = 0; i < myRenderer.bones.Length; i++)
{
GameObject _bone = myRenderer.bones[i].gameObject;
if (!boneMap.TryGetValue(_bone.name, out newBones[i]))
{
Debug.Log("Unable to map bone ~" + _bone.name + "~ to target skeleton!");
break;
}
}
myRenderer.bones = newBones;
myRenderer.rootBone = targetRenderer.rootBone;
}
}

Make a rigged character's head rotate in sync with a quaternion in Unity

I have a face detection app, and I want a character's head to rotate according to the detected face's pose.
I've managed to get the rotation of the detected face in the form of a quaternion, but I'm unsure about how I'm supposed to translate the data from the quaternion into 3D points for the reference points of the rigged character which I believe will decide the rotation.
Let's say I have this character: http://i.imgur.com/3pcRoYx.png
One solution could be to just cut off the head and make it an own object and then set the rotation of that object according to the quaternion, but I don't want that. I want an intact character.
Is it possible to move the reference points in the head with the data from a quaternion? Or have I gotten it wrong how rigged characters turn their heads? I haven't animated before.
You can apply rotation to a single bone. Get that bone in your script. Keep a var in your class to store the last quaternion in and every update, compare it to that and rotate by the different. I don't have the actual editor here but try this psuedocode.
class NeckRotator {
public GameObject Neck;
private Quaternion LastFace;
void Start(){
LastFace = Neck.transform.Rotation;
}
void Update(){
var DetectedFace = ... // Whatever you do to get this
var Change = Quaternion.Inverse(DetectedFace) * LastFace; // Found this online real quick
Neck.Rotate(Change);
LastFace = Neck.transform.Rotation;
}
}
I've done something like that before to rotate a neck of an NPC to look at a player. It should work for your deal as well.

How to give control back to animation in Unity3D once altering objects location

I have a character made of up child objects that are animated using Unity3D's animation system.
While the player is walking, I can programmatically move the hand object up to catch a ball using the following code.
hand.position.y = ball.transform.position.y;
I need the hand object to go back to following the walk animation after it touches the ball, but instead it just stays at the exact position since it was set.
You want to use inverse kinematics and let Unity do the work of figuring out positioning for you. Here's a quick-and-dirty (untested) example for catching a ball (it's in C#, but it should be pretty similar for UnityScript):
// in a script on the same GameObject as your animation controller
bool isCatching;
Transform ball;
void OnAnimatorIK (int layer) {
if (isCatching) {
// set position and rotation weights for your catching hand
animator.SetIKPosition(AvatarIKGoal.RightHand, ball.position);
animator.SetIKRotation(AvatarIKGoal.RightHand, ball.rotation);
} else {
// return your position and rotation weights back to their defaults (probably 0f?)
}
}
You'll need to do some work (possibly raycasting or just checking distance and direction) to determine when to set the isCatching flag to true, and you'll want to play with the weights for position and rotation to make it look natural. The IK manual entry has more detailed information.

XNA: Identifying identical sprites created with for loop

G'day all,
In short, I'm using a for loop to create a bunch of identical sprites that I want to bounce around the screen. The problem is how do I write a collision detection process for the sprites. I have used the process of placing rectangles around sprites and using the .intersects method for rectangles but in that case I created each sprite separately and could identify each one uniquely. Now I have a bunch of sprites but no apparent way to pick one from another.
In detail, if I create an object called Bouncer.cs and give it the movement instructions in it's update() method then create a bunch of sprites using this in Game.cs:
for (int i = 1; i < 5; ++i)
{
Vector2 position = new Vector2(i * 50, i * 50);
Vector2 direction = new Vector2(i * 10, i * 10);
Vector2 velocity = new Vector2(10);
Components.Add(new Bouncer(this, position, direction, velocity, i));
}
base.Initialize();
I can draw a rectangle around each one using:
foreach (Bouncer component1 in Components)
{
Bouncer thing = (Bouncer)component1;
Rectangle thingRectangle;
thingRectangle = new Rectangle((int)thing.position.X, (int)thing.position.Y, thing.sprite.Width, thing.sprite.Height);
But now, how do I check for a collision? I can hardly use:
if (thingRectangle.Intersects(thingRectangle))
I should point out I'm a teacher by trade and play with coding to keep my brain from turning to mush. Recently I have been working with Python and with Python I could just put all the sprites into a list:
sprites[];
Then I could simply refer to each as sprite[1] or sprite[2] or whatever its index in the list is. Does XNA have something like this?
Please let me know if any more code needs to be posted.
Thanks,
Andrew.
One solution, which I use in my game engine, is to have a Logic code run inside the objects for every game Update, ie. every frame. It seems you already do this, according to the variable names, which indicate you run some physics code in the objects to update their positions.
You might also want to create the collision rectangle inside the Bouncer's constructor so it's more accessible and you make good use of object oriented programming, maybe even make it an accessor, so you can make it update every time you call it instead of manually updating the bounding/collision box. For example:
public Rectangle #BoundingBox {
get { return new Rectangle(_Position.X, _Position.Y, width, height); }
}
Whichever way works, but the collision checks can be run inside the Bouncer object. You can either make the reference list of the Bouncer objects static or pass it to the objects itself. The code for collisions is very simply:
foreach(Bouncer bouncer in Components) //Components can be a static List or you can pass it on in the constructor of the Bouncer object
{
if (bouncer.BoundingBox.Intersects(this.BoundingBox))
{
//they collided
}
}

Resources