- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
有没有办法制作一个原语并反复使用它?例如:如果我制作一个立方体,我可以创建 100 个立方体并制作一个 10x10 的网格吗?我试过使用 for 循环并在每个循环中更新 x 和 z 坐标,但它只移动在开始时创建的一个立方体。我的类(class)是使用书中的示例创建的。我知道如何通过更改 PositionCube 方法中的坐标来围绕该区域移动立方体。我可以在主游戏类中做什么来创建一个简单的 10x10 网格?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Cube_Chaser
{
class Cube
{
private GraphicsDevice device;
private Texture2D texture;
public Vector3 location;
private Vector3 position;
private VertexBuffer cubeVertexBuffer;
private List<VertexPositionTexture> vertices = new List<VertexPositionTexture>();
public Cube(GraphicsDevice graphicsDevice, Vector3 playerLocation, float minDistance, Texture2D texture)
{
device = graphicsDevice;
this.texture = texture;
PositionCube(playerLocation, minDistance);
BuildFace(new Vector3(0, 0, 0), new Vector3(0, 1, 1));
BuildFace(new Vector3(0, 0, 1), new Vector3(1, 1, 1));
BuildFace(new Vector3(1, 0, 1), new Vector3(1, 1, 0));
BuildFace(new Vector3(1, 0, 0), new Vector3(0, 1, 0));
BuildFaceHorizontal(new Vector3(0, 1, 0), new Vector3(1, 1, 1));
BuildFaceHorizontal(new Vector3(0, 0, 1), new Vector3(1, 0, 0));
cubeVertexBuffer = new VertexBuffer(device, VertexPositionTexture.VertexDeclaration, vertices.Count, BufferUsage.WriteOnly);
cubeVertexBuffer.SetData<VertexPositionTexture>(vertices.ToArray());
this.position = position;
}
private void BuildFace(Vector3 p1, Vector3 p2)
{
vertices.Add(BuildVertex(p1.X, p1.Y, p1.Z, 1, 0));
vertices.Add(BuildVertex(p1.X, p2.Y, p1.Z, 1, 1));
vertices.Add(BuildVertex(p2.X, p2.Y, p2.Z, 0, 1));
vertices.Add(BuildVertex(p2.X, p2.Y, p2.Z, 0, 1));
vertices.Add(BuildVertex(p2.X, p1.Y, p2.Z, 0, 0));
vertices.Add(BuildVertex(p1.X, p1.Y, p1.Z, 1, 0));
}
private void BuildFaceHorizontal(Vector3 p1, Vector3 p2)
{
vertices.Add(BuildVertex(p1.X, p1.Y, p1.Z, 0, 1));
vertices.Add(BuildVertex(p2.X, p1.Y, p1.Z, 1, 1));
vertices.Add(BuildVertex(p2.X, p2.Y, p2.Z, 1, 0));
vertices.Add(BuildVertex(p1.X, p1.Y, p1.Z, 0, 1));
vertices.Add(BuildVertex(p2.X, p2.Y, p2.Z, 1, 0));
vertices.Add(BuildVertex(p1.X, p1.Y, p2.Z, 0, 0));
}
private VertexPositionTexture BuildVertex(float x, float y, float z, float u, float v)
{
return new VertexPositionTexture(new Vector3(x, y, z), new Vector2(u, v));
}
public void PositionCube(Vector3 playerLocation, float minDistance)
{
location = new Vector3(.5f, .5f, .5f);
}
public void Draw(Camera camera, BasicEffect effect)
{
effect.VertexColorEnabled = false;
effect.TextureEnabled = true;
effect.Texture = texture;
Matrix center = Matrix.CreateTranslation(new Vector3(-0.5f, -0.5f, -0.5f));
Matrix scale = Matrix.CreateScale(0.05f);
Matrix translate = Matrix.CreateTranslation(location);
effect.World = center * scale * translate;
effect.View = camera.View;
effect.Projection = camera.Projection;
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
device.SetVertexBuffer(cubeVertexBuffer);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, cubeVertexBuffer.VertexCount / 3);
}
}
}
}
最佳答案
我接受了@nico-schetler 的回答并为您创建了类(class)。
立方体.cs
class Cube
{
private GraphicsDevice device;
private VertexBuffer cubeVertexBuffer;
public Cube(GraphicsDevice graphicsDevice)
{
device = graphicsDevice;
var vertices = new List<VertexPositionTexture>();
BuildFace(vertices, new Vector3(0, 0, 0), new Vector3(0, 1, 1));
BuildFace(vertices, new Vector3(0, 0, 1), new Vector3(1, 1, 1));
BuildFace(vertices, new Vector3(1, 0, 1), new Vector3(1, 1, 0));
BuildFace(vertices, new Vector3(1, 0, 0), new Vector3(0, 1, 0));
BuildFaceHorizontal(vertices, new Vector3(0, 1, 0), new Vector3(1, 1, 1));
BuildFaceHorizontal(vertices, new Vector3(0, 0, 1), new Vector3(1, 0, 0));
cubeVertexBuffer = new VertexBuffer(device, VertexPositionTexture.VertexDeclaration, vertices.Count, BufferUsage.WriteOnly);
cubeVertexBuffer.SetData<VertexPositionTexture>(vertices.ToArray());
}
private void BuildFace(List<VertexPositionTexture> vertices, Vector3 p1, Vector3 p2)
{
vertices.Add(BuildVertex(p1.X, p1.Y, p1.Z, 1, 0));
vertices.Add(BuildVertex(p1.X, p2.Y, p1.Z, 1, 1));
vertices.Add(BuildVertex(p2.X, p2.Y, p2.Z, 0, 1));
vertices.Add(BuildVertex(p2.X, p2.Y, p2.Z, 0, 1));
vertices.Add(BuildVertex(p2.X, p1.Y, p2.Z, 0, 0));
vertices.Add(BuildVertex(p1.X, p1.Y, p1.Z, 1, 0));
}
private void BuildFaceHorizontal(List<VertexPositionTexture> vertices, Vector3 p1, Vector3 p2)
{
vertices.Add(BuildVertex(p1.X, p1.Y, p1.Z, 0, 1));
vertices.Add(BuildVertex(p2.X, p1.Y, p1.Z, 1, 1));
vertices.Add(BuildVertex(p2.X, p2.Y, p2.Z, 1, 0));
vertices.Add(BuildVertex(p1.X, p1.Y, p1.Z, 0, 1));
vertices.Add(BuildVertex(p2.X, p2.Y, p2.Z, 1, 0));
vertices.Add(BuildVertex(p1.X, p1.Y, p2.Z, 0, 0));
}
private VertexPositionTexture BuildVertex(float x, float y, float z, float u, float v)
{
return new VertexPositionTexture(new Vector3(x, y, z), new Vector2(u, v));
}
public void Draw( BasicEffect effect)
{
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
device.SetVertexBuffer(cubeVertexBuffer);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, cubeVertexBuffer.VertexCount / 3);
}
}
}
CubeDrawable.cs
public class DrawableList<T> : DrawableGameComponent
{
private BasicEffect effect;
private Camera camera;
private class Entity
{
public Vector3 Position { get; set; }
public Matrix Orientation { get; set; }
public Texture2D Texture { get; set; }
}
private Cube cube;
private List<Entity> entities = new List<Entity>();
public DrawableList (Game game, Camera camera, BasicEffect effect)
: base( game )
{
this.effect = effect;
cube = new Cube (game.GraphicsDevice);
this.camera = camera;
}
public void Add( Vector3 position, Matrix orientation, Texture2D texture )
{
entities.Add (new Entity() {
Position = position,
Orientation = orientation,
Texture = texture
});
}
public override void Draw (GameTime gameTime )
{
base.Draw (gameTime);
foreach (var item in entities) {
effect.VertexColorEnabled = false;
effect.TextureEnabled = true;
effect.Texture = item.Texture;
Matrix center = Matrix.CreateTranslation(new Vector3(-0.5f, -0.5f, -0.5f));
Matrix scale = Matrix.CreateScale(0.05f);
Matrix translate = Matrix.CreateTranslation(item.Position);
effect.World = center * scale * translate;
effect.View = camera.View;
effect.Projection = camera.Projection;
cube.Draw (effect);
}
}
}
用法
camera = new Camera (graphics.GraphicsDevice);
effect = new BasicEffect (graphics.GraphicsDevice);
cubes = new DrawableList<Cube> (this, camera, effect);
Components.Add (cubes);
for (int i=0 ; i < 50; i++)
{
cubes.Add (new Vector3( i*0.5f, 50.0f, 50.0f), Matrix.Identity, logoTexture);
}
关于c# - 通过类重用 xna 原语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16536974/
我对 JS 还很陌生,目前正在学习对象,对原语有点困惑。在 W3schools 上,javascript 原语定义为: “原始值是没有属性或方法的值。 原始数据类型是具有原始值的数据。 JavaScr
我可以用 java 做这个吗? private static boolean isRight(){ return new Random.nextBolean(); } boolean test
OpenCV 原语是否基于 CUDA Nvidia 性能原语 (NPP)? 我所说的原语是指在 NPP 库中实现的相同原语,例如:boxFilter、Mirror、Convolution... 我想知
这个问题在这里已经有了答案: How many integers can I create in 1GB memory? (3 个答案) 关闭 6 年前。 引用自 Algorithms 4th ed
我正在尝试编写我的第一个显示三角形的 DirectX 10 程序。一切都编译得很好,渲染函数被调用,因为背景变成了黑色。但是,我尝试使用三角形 strip 基元绘制的三角形根本不显示。 初始化函数:
什么是 UI 原语? if you find yourself wishing that you had a different set of UI primitives to work with,
我知道这个问题已经在 Stakoverflow 中被问过多次。但是没有一个解决方案对我有帮助。谁能告诉我做错了什么 我有一个 Controller Action 如下 Function WF
我正在尝试从一个程序中读取一个二进制文件,该程序将日志(某种)写入一个dat文件,我已经很好地找到了使用Java的格式。我这样加载它: DataInputStream in = new DataInp
我有代码对 MVC Controller 方法进行 AJAX 调用,如果我这样做,它会顺利工作: var obj = '{"titlename":"whatever"}'; $
我有一个对象: class Data{ int a; int b; AtomicIntegerArray c = new AtomicIntegerArray(10000); }
我的应用程序具有以下 Javascript 原语: var bearerToken = "a"; var expirationDate = "b"; var firstName = "c"; var
我经常发现自己需要让组件接受任何有效的 HTML 属性以使底层 HTML 元素使用它们。 class Input extends React.Component { // Here I use o
我尝试使用 javascriptserializer 反序列化我的 json,但收到错误“无效的 json 原语” 其他帖子提到要验证 json,我也这样做了。 JavaScriptSerialize
有没有办法制作一个原语并反复使用它?例如:如果我制作一个立方体,我可以创建 100 个立方体并制作一个 10x10 的网格吗?我试过使用 for 循环并在每个循环中更新 x 和 z 坐标,但它只移动在
扩展和装箱 Java 原语。 我知道不可能将包装类从一个扩展到另一个,因为它们不是来自同一继承树。为什么不能将基元扩展为另一种基元类型并自动装箱扩展的基元? 鉴于可以将字节参数传递给需要 int 的方
你能帮我解决这个问题吗?我有一个 asp.net 应用程序,在此我使用 Javascript 序列化程序序列化数据集,然后转换为列表。该代码如下所示。 JavaScriptSerializer jso
好的,所以当我尝试运行我的应用程序时遇到这个问题: Invalid JSON primitive: . public static void ReloadConfig() { if (!Fil
我正在尝试通过以下 jQuery Ajax 调用将字符串化的 JSON 对象发送到 MVC 方法: $.ajax({ type: "POST", url:
当我在 C# 中使用 JavaScriptSerializer 时,出现“无效的 JSON 原语”异常。我假设问题出在我的 json 输入字符串上,但我没有看到问题。 JavaScriptSerial
我知道您可以直接从 Scala 访问 java 原语 val javaDouble = new java.lang.Double(1.0) 这是否意味着我们正在通过包装器或直接访问原语?语法 new
我是一名优秀的程序员,十分优秀!