I had a working shape but when I try to change the coordinates it disappears.
Here is what I did.
This is class level variables:
private BasicEffect _effect;
private VertexPositionColor[] _vertices = new VertexPositionColor[5];
Then in Initialization method I put these:
float aspectRatio = (float)GraphicsDevice.Viewport.Bounds.Width / GraphicsDevice.Viewport.Bounds.Height;
Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), aspectRatio, 0.1f, 1000.0f);
Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 10), Vector3.Zero, Vector3.Up);
_effect = new BasicEffect(GraphicsDevice);
_effect.LightingEnabled = false;
_effect.TextureEnabled = false;
_effect.VertexColorEnabled = true;
_effect.Projection = projection;
_effect.View = view;
_effect.World = Matrix.Identity;
Color color = Color.Black;
_vertices[0] = new VertexPositionColor(new Vector3(-1, -1, 0), color);
_vertices[2] = new VertexPositionColor(new Vector3(-1, 1, 0), color);
_vertices[2] = new VertexPositionColor(new Vector3(1, -1, 0), color);
_vertices[3] = new VertexPositionColor(new Vector3(1, 1, 0), color);
And in draw method I put:
foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
{
// Apply the pass
pass.Apply();
// Draw the square
GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, _vertices, 0, 2);
}
This is working fine. but when I change it to this one it doesn't work anymore.
_vertices[1].Position = new Vector3(-0.10f, 1.37f, -3.0f);
_vertices[0].Position = new Vector3(-0.15f, 1.40f, -3.0f);
_vertices[2].Position = new Vector3(-0.00f, 1.40f, -3.0f);
_vertices[4].Position = new Vector3(0.15f, 1.40f, -3.0f);
_vertices[3].Position = new Vector3(0.10f, 1.37f, -3.0f);
another change that i made:
GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, _vertices, 0, 3);
any idea?
Thanks in advance
It may be that you are drawing your polygons backwards. Meaning you are declaring your vertices in the wrong direction and you may need to change the order. I am not sure which is the correct direction to declare vertices.
Alternatively you can turn off back face culling which will draw
both sides of polygons. This will have a performance hit 2 draws for each polygon. You could try turning this off first and if it fixes it then you know that my first point is the cause.
Related
I have a rectangle bounds (10, 20, 100, 200) and the CGPoints are StartPoint (0.5, 0.5) and EndPoints as (1, 1). From these points how needs to calculate the segments bounds ? I need to apply this bounds for CGGradient for start point and end points.
Eg Code :
GradientColor gradientColor1 = new GradientColor(){StartPoint = new CGPoint(0.5, 0), EndPoint= new CGPoint(0.5, 1)};
GradientStop stop1 = new GradientStop() { Color = UIColor.Red, Offset = 0.1f };
GradientStop stop2 = new GradientStop() { Color = UIColor.Blue, Offset = 0.9f };
can you please help me out of this?
Here an an example that will create a left to right linear gradient within the current CGContext.
using (var context = UIGraphics.GetCurrentContext ()) {
context.SaveState();
var startPoint = new CGPoint(rect.Left, 0);
var endPoint = new CGPoint(rect.Right, 0);
var components = new CGColor[] { UIColor.Red.CGColor, UIColor.Blue.CGColor };
using (var rgb = CGColorSpace.CreateDeviceRGB()) {
var gradient = new CGGradient(rgb, components);
context.DrawLinearGradient(gradient, startPoint, endPoint, CGGradientDrawingOptions.DrawsBeforeStartLocation);
};
context.RestoreState();
}
Changing the start and end points you can have the gradient paint right to left, up/down, diagonal, etc..
I am new with three.js and I trying to modify the position value of Z in Vector3 using Dat.Gui. This is my code, but I am not getting any luck.
var zhighpnts = gui.add(parameters, 'x').min(0).max(400).step(.01).name('High Nodes');
zhighpnts.onChange(function(jar){
var a = new THREE.Vector3( 400, jar, 400 );
var b = new THREE.Vector3( 0, 0, 0 );
var c = new THREE.Vector3( -400, jar, -400 );
});
Thanks!
Seems like you're trying to change vertices of a triangle.
Let's create one:
var geom = new THREE.Geometry();
geom.vertices.push(new THREE.Vector3(-10, 0, 0));
geom.vertices.push(new THREE.Vector3(0, 0, 0));
geom.vertices.push(new THREE.Vector3(10, 0, 0));
geom.faces.push(new THREE.Face3(0, 1, 2)); // CCW - it's important
var tri = new THREE.Mesh(geom, new THREE.MeshBasicMaterial({color: "yellow"}));
scene.add(tri);
then we'll set an object of parameters
var parameters = {y:0};
and get vertices of our triangle:
var a = geom.vertices[geom.faces[0].a]; // face's properties a, b, c
var b = geom.vertices[geom.faces[0].b]; // contain just indices of related vertices
var c = geom.vertices[geom.faces[0].c]; // thus we'll find them in array of vertices
the rest is not so difficult, so let's create our controller:
var gui = new dat.GUI(); // create instance of dat.GUI();
gui.add(parameters, "y", 0, 20) // add a controller for 'y' property of 'parameters'
.name("High nodes") // set controller's name
.onChange( // set controller's listener
function(value) {
a.y = value; // we want to change y-value of 'a'
c.y = value; // we want to change y-value of 'c'
}
);
and one more important thing, you have to set
geom.verticesNeedUpdate = true;
in your animation/render loop.
jsfiddle example. I hope it's helpful.
I'm having a problem trying to write a unit test to check collision detection. I simplify code as only this possible - I have a plane in (0, 0, 0) and I do raycasting from above this plane (from (0, 100, 0)) to bottom (0, -1, 0) and I suppose to find intersections with that plane but no luck.
console.clear();
var intersections,
from = new THREE.Vector3(0, 100, 0);
direction = new THREE.Vector3(0, -1, 0),
raycaster = new THREE.Raycaster();
var geometry = new THREE.PlaneGeometry(10, 10, 1, 1);
var ground = new THREE.Mesh(geometry);
ground.position.set(0, 0, 0);
ground.rotation.x = THREE.Math.degToRad(-90);
raycaster.set(from, direction);
intersections = raycaster.intersectObjects([ground]);
console.log(intersections);
What is wrong here? Why this simple code does not show any intersections? (r73).
jsfiddle example
You need to update the world transform of the ground mesh prior to raycasting. (Normally, the renderer does this for your in the render() call.)
console.clear();
var intersections,
from = new THREE.Vector3(0, 100, 0);
direction = new THREE.Vector3(0, -1, 0),
raycaster = new THREE.Raycaster();
var geometry = new THREE.PlaneGeometry(10, 10, 1, 1);
var ground = new THREE.Mesh(geometry);
ground.position.set(0, 0, 0);
ground.rotation.x = THREE.Math.degToRad(-90);
ground.updateMatrixWorld(); // add this
raycaster.set(from, direction);
intersections = raycaster.intersectObjects([ground]);
console.log(intersections);
three.js r.73
I want to update the position of my sprite. But it is moving downwards then jumps a little and then stops. It doesn't even move left or right. I want it to move left, right, up and down based on tilt.
Here is my code:-
Scene scene = new Scene();
scene.setBackground(new Background(1.09804f, 0.6f, 3.0f));
playerSprite = new AnimatedSprite(10, 10, 48, 64, pTextureRegion, getVertexBufferObjectManager());
playerSprite.animate(300);
PhysicsWorld physicsWorld = new PhysicsWorld(new Vector2( 0, SensorManager.GRAVITY_MOON), false);
FixtureDef PLAYER_FIX = PhysicsFactory.createFixtureDef(10.0f, 0.5f, 0.0f);
Body playerBody = PhysicsFactory.createBoxBody(physicsWorld, playerSprite, BodyType.DynamicBody, PLAYER_FIX);
physicsWorld.registerPhysicsConnector(new PhysicsConnector(playerSprite, playerBody, true, false));
scene.attachChild(playerSprite);
In your
PhysicsWorld physicsWorld = new PhysicsWorld(new Vector2( 0, SensorManager.GRAVITY_MOON), false);
Change SensorManager.Gravity_Moon which makes a constant gravity pull directed down for all your objects - to .Sensor_Orientation. Im not sure which one exactly you have to choose but there is your problem. Try some other .Sensor_xxx if the one I told you doesn't work.
public void onAccelerationChanged(AccelerationData pAccelerationData) {
// TODO Aiuto-generated method stub
Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY());
this.physicsWorld.setGravity(gravity);;
Vector2Pool.recycle(gravity);
I am drawing a traingleStrip using VertexPositionTexture and BasicEffect that extends from the first person perspective of the screen out a certain distance on the Z axis.
In a loop I pass start and end vectors to this code to create my vertices.
public VertexPositionTexture[] vertices = new VertexPositionTexture[4];
private int xPos = 4;
public waypointLineSegment(Vector3 startPoint, Vector3 endPoint)
{
this.texture = texture1;
vertices[0].Position = new Vector3(endPoint.X - xPos, endPoint.Y, endPoint.Z); //Upper Left Point
vertices[0].TextureCoordinate = new Vector2(0, 1);
vertices[1].Position = new Vector3(startPoint.X - xPos, endPoint.Y, startPoint.Z); //Lower Left point
vertices[1].TextureCoordinate = new Vector2(0, 0);
vertices[2].Position = new Vector3(endPoint.X + xPos, endPoint.Y, endPoint.Z); //Upper Right Point
vertices[2].TextureCoordinate = new Vector2(1, 1);
vertices[3].Position = new Vector3(startPoint.X + xPos, endPoint.Y, startPoint.Z); //Lower Right Point
vertices[3].TextureCoordinate = new Vector2(1, 0);
}
I initialize like this:
protected override void Initialize()
{
graphics.GraphicsDevice.RasterizerState = RasterizerState.CullNone;
basicEffect = new BasicEffect(graphics.GraphicsDevice);
base.Initialize();
}
I render the plane in this way:
Vector3 transformedReference = Vector3.Transform(new Vector3(0, 0, 10), aRotationInt);
Vector3 cameraLookat = firstPersonVector + transformedReference;
view = Matrix.CreateLookAt(firstPersonVector, cameraLookat, Vector3.Up);
basicEffect.View = view;
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, allVertices, 0, 2);
}
This works great. But, when I apply a rotation to the view, the strip will disappear every once in a while. Then it will reappear with a single degree of more or less rotation applied.
I cannot find a pattern. It might happen at 37 degrees or 315 etc. I have set the GraphicsDevice RasterizerState to CullMode.None. Can anyone help?: