Java 3D: Get intersection of two Shape3D's? - java-3d

Is it possible to get the intersection co-ords of two shapes3d's?
(One is a Line and the other is a QuadArray)
Just wondering if there is a simple way before I try a hack and slash method?
-I'm pretty new to Java 3D so I may have missed a function which does exactly what I need.

The Easy way is to use Bounds and intersect method :
Bounds quadArrayBounds = quadArrayShape.getBounds();
if (quadArrayBounds.intersect(new Point3d(startLine.x, startLine.y, startLine.z),
new Vector3d(endLine.x, endLine.y, endLine.z))){
// Action to do if Intesect is true
}

Related

Maya: Having trouble writing a script to cut a mesh into equal pieces

I want to split a mesh into sections based on a number of vertices. Essentially, I want a mesh cut into sections of 300 verts each with a remainder section of whatever is left over.
I've done this for the most part (i can get verts/faces, etc) but I'm having trouble figuring a graceful way of iterating through the extracted meshes.
I'm using polyChipOff which has no return value for the faces it chipped, so it's entirely new objects that are created that i have no handle to so i can't just continue chipping away from the previous piece as it no longer exists.
Any advice on how to go about this better?
I've thought of either iterating through all meshes in the scene for new ones (cache them at the start) or using a scriptJob to detect new objects being made. Both of those seem very hacky so was curious if anyone had advice.
You can try this method:
import maya.cmds as cmds
shape = cmds.listRelatives(p=True)
object = cmds.listRelatives(a, p=True)
selectedFace = cmds.ls(sl=True)
cmds.select(object[0] + '.f[:]', tgl=True)
unselecetedFace = cmds.ls(sl=True)
duplicated = cmds.duplicate(object, un=True)[0]
cmds.delete(duplicated, ch=True)
cmds.delete(selectedFace)
for i in range(len(unselecetedFace)):
unselecetedFace[i] = unselecetedFace[i].replace(object[0],duplicated)
cmds.delete(unselecetedFace)
cmds.select(duplicated)

Flipping QuadTree in recursive abstract code

I'm trying to flip a quad tree about the vertical axis recursively, but without using a particular PL. In which case I've written the following, but I'm 100% it's not actually good, and I can't quite sure I understand it as well.
flip(quadtree) {
if (singleNode)
return quadtree
else return formQuadTree(flip(NW(quadtree)), flip(NE(quadtree)), flip(SW(quadtree)), flip(SE(quadtree)))
Any suggestions?
Replace:
formQuadTree(flip(NW(quadtree)),flip(NE(quadtree)),flip(SW(quadtree)),flip(SE(quadtree))).
With:
formQuadTree(flip(NE(quadtree)),flip(NW(quadtree)),flip(SE(quadtree)),flip(SW(quadtree))).
What exactly don't you understand?
formQuadTree( topLeft, topRight, bottomLeft, bottomRight ) - forms your quad tree. Every time you enter recursively into flip( quadTree ) you go deeper and deeper inside the inductive step and return the base case, flipping each node that has 4 children quadTrees.
So once all your recursive calls get to return quadtree, all your inductive nodes have been arranged correspondingly.

physicsBody rotation limitation

I've got the following node set as follow.
box.physicsBody.allowsRotation = YES;
No mater what force applies to this box, I want it to never fall down or rotate more than a certain angle, just like a tree or a spring that might bend all the way down but has a tendency to go back to it's initial angle or position.
Is there a any trick or method to that?
I was thinking maybe joining an invisible un-rotatable physics body under the box and use a joint spring method for that which I have no Idea how that works!
Or, maybe there be a trick to play with the pivot. Any idea?
In the update: method you will have to continuously check the desired node's properties. In your case it is going to be the zRotation property of your node.
Add this code in your update: method:
if(yourNode.zRotation > 0.5)
yourNode.zRotation = 0.5;
if(yourNode.zRotation < -0.5)
yourNode.zRotation = -0.5;
Change the 0.5 value to your desired value or replace it with a static const float.

Box2d - Changing contact filter on the fly

Im using cocos2d (iOS) and box2d to create a game.
I have come to the point where I need to change the contact filter mid simulation and wondering how to go about this.
I need to use maskbits and categorybits, which is fine, im just unsure how to apply them to a b2body mid game.
I'm thinking I may need to retrieve the original b2fixture or b2fixturedef of the b2body on initialization, alter the values accordingly then call a method to refresh - world.Refilter()?
Does this sound somewhat accurate?
Any advice is surely appreciated
Oliver.
b2Filter filter;
for ( b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext() ) {
f->GetFilterData( &filter );
filter.categoryBits = ...;
filter.maskBits = ...;
filter.groupIndex = ...;
f->SetFilterData( &filter );
}
Obviously this would change the filter settings for all fixtures on a body - if you want to be more selective you will have to be able to tell which fixture is which somehow. Eg. if you know it's the only circle fixture you could just look at the type of the fixture to decide, otherwise you would have to make use of the fixture's user data I guess.
You can iterate over all bodies in the b2World. On each body you can iterate over it's fixture and change it's filter. To identify your bodies you can use userData.
The answer from iforce2d might be obsolete. I got the following code working properly with box2d v2.2.1, cocos2D v2.0.0 using Xcode v4.5.2 (here I assume I have a pointer to a b2Body named 'body' with only one fixture, i.e., I don't iterate over all fixtures on the body):
b2Fixture *fix = body->GetFixtureList();
b2Filter filter = fix->GetFilterData();
filter.groupIndex = -1*kPlayerGroupIndex;
fix->SetFilterData(filter);
In the above code I prevent 'body' from colliding with my player body which also has the same groupIndex value, i.e., -1*kPlayerGroupIndex, where kPlayerGroupIndex is a positive integer constant. Any bodies with this negative groupIndex never collide with one another. You could also update the categoryBits or maskBits accordingly to prevent collisions.
GetFilterData(&filter) and SetFilterData(&filter) returned errors for me given the version numbers I quoted above.

Need advice on wordy naming of methods

I'm writing an API for creating geometric shapes, and I'm running into some difficulties naming my methods.
Let's take a simple case: Creating a circle. Most of us might be familiar with a method like graphics.drawEllipse(x, y, w, h). To draw a circle, you need to know the top left coordinate, and the width and height of the circle.
My API is intended to make it easy for a developer to draw shapes using a variety of information, without doing a lot of math - which is trivial for circles, but more complicated for other shapes. For example, you should also be able to draw a circle given its center coordinates and radius, or the top left and bottom right coordinates.
So I have a Circle class with factory methods like:
Circle.createWithCenterAndRadius(cx, cy, r)
Circle.createWithBoundingBox(x1, y1, x2, y2)
Circle.createWithWidthAndHeight(x, y, w, h)
I feel like there might be a "code smell" here, but I'm not sure. On the one hand, these factory methods are necessarily descriptive. On the other hand, I can forsee these method names getting out of control. For example, how would I name a Triangle factory method that creates a triangle given a point, the length of one side, an angle, and the length of another side? Triangle.createWithPointSideAngleAndSide(x, y, side1, angle, side2)? Is that just evil?
If you were to use this API, would method names like this be okay to you? Do you have advice on how I can make the method names more sane?
You might change your circle methods to
Circle.FromCenterAndRadius(...)
Circle.FromBoundingBox(...)
Circle.FromWidthAndHeight(...)
It implies that you're creating circles from their different representations in a kind of concise way...
It is ok in any language that doesn't support named parameters. If the language supports named parameters, I like more the short Create and just have obvious parameters names.
For a language with named parameters, you would:
Circle.Create(
centerX = cx,
centerY = cy,
radius = r
);
Another more involved option, would be a fluent interface like (but that is probably too much):
circleBuilder.Center(cx,cy).Radius(r)
circleBuilder.Center(x,y).Width(w).Height(y)
circleBuilder.BoundWith().Left(x1,y1).Right(x2,y2)
Center returns an instance of an intermediate class that only allows Radius or Width. And BoundWith returns one that only allows Left.
I think there is nothing wrong with your descriptive methods - they are the compact and describe exactly what's going on. The users of the library will have no doubt about the function of your methods, neither the maintanance programmers.
You could also apply some design pattern here if you are really worried about exposing a large number of factory methods - like having factory methods with property classes. You could have a CircleProperties class with properties like CenterX, CenterY, Radius, (bool)UseCenterX, (bool)UseCenterY etc and then you pass this to the public factory method which will figure out which (private) factory method to use.
Assuming C#:
var circleProperties = new CircleProperties()
{
CenterX = 10,
CenterY = -5,
Radius = 8,
UseCenterX = true,
UseCenterY = true,
UseCenterRadius = true
};
var circle = Circle.Create(circleProperties);
My first instinct is to have more types, which would allow for more intuitive method overloading.
// instead of Circle.createWithCenterAndRadius(cx, cy, r)
Circle.create( new Point(cx,xy), r);
// instead of Circle.createWithBoundingBox(x1, y1, x2, y2)
Circle.create( new Point(x1,y1), new Point(x1,y1) );
// or even...
Circle.create( new Box(p1,p2));
// instead of Circle.createWithWidthAndHeight(x, y, w, h)
Circle.create( new Point(x,y), w, h);
As well as Point, you could define Distance (which would allow for different units)
If this style suits you, consider why you need a factory method instead of a constructor.
Circle c = new Circle(new Point(cx,xy), r);
For languages that don't support named parameters, would it be cleaner to make the method name something very simple like Circle.create and then just add an additional input flag string (like "center" or "bounding") that indicated how the input values should be interpreted for cases that are hard to discriminate based only on input variable number and type? Drawbacks to this would be that it requires extra logic inside of the method to handle different types of input arguments and also requires that the user remember the flag options.
I would have methods CreateTriangle and have the overloads show the different pieces of information required.
E.g.
Circle.CreateCircle(cx, cy, r)
Circle.CreateCircle(point1, point2)
Circle.CreateCircle(point, width, height)
Yes, this is more of a meta-answer, but I suggest you take a peek at how naming is done in Apple's Cocoa.
Your instinct is correct--the entire pattern of creating things this way is--iffy.
Unless these are used just once or twice, they are going to become pretty messy. If you were creating a shape with 5 circles and 3 triangles, it would be a mess.
Anything beyond a trivial example would probably be best done with some kind of data-driven implementation.
Towards those ends, having it take a string, hash or XML to define your shapes might be extremely useful.
But it all depends on how you expect them to be used.
I have the same kind of issues with creating Swing controls in Java. You end up with line after line of "new Button()" followed by a bunch of .set property calls as well as a line of code to copy the value to an object (or add a listener), and a line to reset the value..
That kind of boilerplate should never happen in code, so I usually try to find a way to drive it with data, binding the controls to objects dynamically--and towards that end, a descriptive string-based language would be very helpful.
I know, I know. This sounds completely crazy for you C/C++/Java people, but the examples given in the question and in all those answers clearly demonstrate what a bad, bad convention CamelCaseNaming really is.
Let's take another look at the original example:
Circle.createWithCenterAndRadius(cx, cy, r)
Circle.createWithBoundingBox(x1, y1, x2, y2)
Circle.createWithWidthAndHeight(x, y, w, h)
And now let's get rid of that camel case notation
Circle.create_with_center_and_radius(cx, cy, r)
Circle.create_with_bounding_box(x1, y1, x2, y2)
Circle.create_with_width_and_height(x, y, w, h)
This may seem terribly unfamilar, but be honest: which version is easier to read?