My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package toxi.physics.constraints;

import toxi.geom.Sphere;
import toxi.geom.Vec3D;
import toxi.physics.VerletParticle;

/**
* This class implements a spherical constraint for 3D
* {@linkplain VerletParticle}s. The constraint can be configured in two ways: A
* bounding sphere not allowing particles to escape or alternatively does not
* allow particles to enter the space occupied by the sphere.
*
* @author toxi 16 Mar 2009
*/
public class SphereConstraint implements ParticleConstraint {

public Sphere sphere;

public boolean isBoundingSphere;

public final static boolean INSIDE = true;
public final static boolean OUTSIDE = false;

/**
* Creates a new instance using the sphere definition and constraint mode
* given.
*
* @param sphere
* sphere instance
* @param isBoundary
* constraint mode. Use {@linkplain #INSIDE} or
* {@linkplain #OUTSIDE} to specify constraint behaviour.
*/
public SphereConstraint(Sphere sphere, boolean isBoundary) {
this.sphere = sphere;
this.isBoundingSphere = isBoundary;
}

/**
* Creates a new instance using the sphere definition and constraint mode
* given.
*
* @param origin
* sphere origin
* @param radius
* sphere radius
* @param isBoundary
* constraint mode. Use {@linkplain #INSIDE} or
* {@linkplain #OUTSIDE} to specify constraint behaviour.
*/
public SphereConstraint(Vec3D origin, float radius, boolean isBoundary) {
sphere = new Sphere(origin, radius);
this.isBoundingSphere = isBoundary;
}

/*
* (non-Javadoc)
*
* @see
* toxi.physics.constraints.ParticleConstraint#apply(toxi.physics.VerletParticle
* )
*/
public void apply(VerletParticle p) {
boolean isInside = sphere.containsPoint(p);
if ((isBoundingSphere && !isInside) || (!isBoundingSphere && isInside)) {
p.set(sphere.add(p.subSelf(sphere).normalizeTo(sphere.radius)));
}
}

}

Change log

r356 by toxmeister on Jan 17, 2010   Diff
fixing SphereConstraint to work with
spheres not located at world origin, minor
updates to PlaneConstraint &
RectConstraint
Go to: 
Project members, sign in to write a code review

Older revisions

r201 by toxmeister on Mar 22, 2009   Diff
made applyConstraints() public, slight
refactoring in SphereConstraint and
added Javadocs
r198 by toxmeister on Mar 16, 2009   Diff
adding SphereConstraint for 3D
particles physics and refactored
particle constraints into a 1:M
relationship
All revisions of this file

File info

Size: 2189 bytes, 70 lines
Powered by Google Project Hosting