- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试在我的游戏中实现基于 2D 瓦片的水。我开始让瓷砖出现在屏幕上等。我有一个绘制每种瓷砖类型的绘图功能。我遇到的问题是,当我调用此函数时,水的 Tiles 不会改变位置。这让我相信这段代码无法正常运行。在每个循环中调用此代码。这应该更新所有水砖的质量。由于某种原因,什么都没有发生。水保持在原来的位置。我的瓷砖在瓷砖类的 vector 中只是
Tiles()
{
TileProp // the type of tile (GROUND,AIR,WATER)
Mass
NewMass
}
void App::SimulateCompression()
{
float Flow = 0;
float remainingmass = 0;
int ID = 0;
//Calculate and apply flow for each block
for (int X = 0; X < MAP_WIDTH; X++)
{
for(int Y = 0; Y < MAP_HEIGHT; Y++)
{
//Skip inert ground blocks
if(TileList[ID].TileProp == TILE_GROUND) continue;
//Custom push-only flow
Flow = 0;
remainingmass = TileList[ID].Mass;
if(remainingmass <= 0) continue;
//The block below this one
if(TileList[Rect2Lin(TILE_SIZE,X,(Y-1))].TileProp != TILE_GROUND)
{
Flow = GetStableWaterState(remainingmass + TileList[Rect2Lin(TILE_SIZE,X,(Y-1))].Mass /*mass[x][y-1]*/) - TileList[Rect2Lin(TILE_SIZE,X,(Y-1))].Mass;
if(Flow > MinFlow){Flow *= 0.5; /*leads to smoother flow*/}
int tempA = Min(MaxSpeed, remainingmass);
if(Flow > tempA){Flow = tempA;}
if(Flow < 0){Flow = 0;}
TileList[ID].NewMass -= Flow;
TileList[Rect2Lin(TILE_SIZE,X,(Y-1))].NewMass += Flow;
remainingmass -= Flow;
}
if(remainingmass <= 0) continue;
//Left
if(TileList[Rect2Lin(TILE_SIZE,(X - 1),Y)].TileProp != TILE_GROUND)
{
//Equalize the amount of water in this block and it's neighbour
Flow = (TileList[ID].Mass - TileList[Rect2Lin(TILE_SIZE,(X - 1),Y)].Mass)/4;
if(Flow > MinFlow){Flow *= 0.5;}
if(Flow > remainingmass){Flow = remainingmass;}
if(Flow < 0){Flow = 0;}
TileList[ID].NewMass -= Flow;
TileList[Rect2Lin(TILE_SIZE,(X - 1),Y)].NewMass += Flow;
remainingmass -= Flow;
}
if(remainingmass <= 0) continue;
//Right
if(TileList[Rect2Lin(TILE_SIZE,(X + 1),Y)].TileProp != TILE_GROUND)
{
//Equalize the amount of water in this block and it's neighbour
Flow = (TileList[ID].Mass - TileList[Rect2Lin(TILE_SIZE,(X + 1),Y)].Mass)/4;
if(Flow > MinFlow){Flow *= 0.5;}
if(Flow > remainingmass){Flow = remainingmass;}
if(Flow < 0){Flow = 0;}
TileList[ID].NewMass -= Flow;
TileList[Rect2Lin(TILE_SIZE,(X + 1),Y)].NewMass += Flow;
remainingmass -= Flow;
}
if(remainingmass <= 0) continue;
//Up. Only compressed water flows upwards
if(TileList[Rect2Lin(TILE_SIZE,X,(Y + 1))].TileProp != TILE_GROUND)
{
Flow = remainingmass - GetStableWaterState(remainingmass + TileList[Rect2Lin(TILE_SIZE,X,(Y + 1))].Mass);
if (Flow > MinFlow){Flow *= 0.5;}
int tempB = Min(MaxSpeed, remainingmass);
if(Flow > tempB){Flow = tempB;}
if(Flow < 0){Flow = 0;}
TileList[ID].NewMass -= Flow;
TileList[Rect2Lin(TILE_SIZE,X,(Y + 1))].NewMass += Flow;
remainingmass -= Flow;
}
ID++;
}
}
ID = 0;
//Copy the new mass values
for (int X = 0; X < MAP_WIDTH; X++)
{
for (int Y = 0; Y < MAP_HEIGHT; Y++)
{
TileList[ID].Mass = TileList[ID].NewMass;
ID++;
}
}
ID = 0;
for(int X = 0; X < MAP_WIDTH; X++)
{
for(int Y = 0; Y < MAP_HEIGHT; Y++)
{
//Skip ground blocks
if(TileList[ID].TileProp == TILE_GROUND) continue;
//Flag/unflag water blocks
if(TileList[ID].Mass > MinMass)
{
TileList[ID].TileProp = TILE_WATER;
}else
{
TileList[ID].TileProp = TILE_AIR;
}
ID++;
}
}
//Remove any water that has left the map
for(int X = 0; X < MAP_WIDTH; X++)
{
TileList[X].Mass = 0;
TileList[Rect2Lin(TILE_SIZE,X,MAP_HEIGHT - 1)].Mass = 0;
}
for(int Y = 0; Y < MAP_HEIGHT; Y++)
{
TileList[Rect2Lin(TILE_SIZE,0,Y)].Mass = 0;
TileList[Rect2Lin(TILE_SIZE,(MAP_WIDTH - 1),Y)].Mass = 0;
}
}
最佳答案
Ok, so ID is going to 0 because after ID hits 34 It just leaves the two nested for loops... Why would it do that?
//Calculate and apply flow for each block
for (int X = 0; X < MAP_WIDTH; X++)
{
for(int Y = 0; Y < MAP_HEIGHT; Y++)
{
//Skip inert ground blocks
if(TileList[ID].TileProp == TILE_GROUND) continue;
...
ID++;
}
}
TileList[34]
可能是地砖。在这一点上,您一遍又一遍地点击第一个 if
(因为您永远不会在循环的最后到达 ID++
),直到您用尽 for
-循环。
试试这个:
//Calculate and apply flow for each block
for (int X = 0; X < MAP_WIDTH; X++)
{
for(int Y = 0; Y < MAP_HEIGHT; Y++)
{
int ID = Rect2Lin(TILE_SIZE,X,Y));
//Skip inert ground blocks
if(TileList[ID].TileProp == TILE_GROUND) continue;
...
}
}
编辑:
好的,这适用于我的系统:
#include <GL/glut.h>
#include <vector>
using namespace std;
// simple Eigen::Matrix work-alike
template< typename T >
class Matrix
{
public:
Matrix( const size_t rows, const size_t cols )
: mStride( cols )
, mHeight( rows )
, mStorage( rows * cols )
{}
T& operator()( const size_t row, const size_t col )
{
return mStorage[ row * mStride + col ];
}
const T& operator()( const size_t row, const size_t col ) const
{
return mStorage[ row * mStride + col ];
}
size_t rows() const { return mHeight; }
size_t cols() const { return mStride; }
private:
vector< T > mStorage;
size_t mStride;
size_t mHeight;
};
struct Cell
{
enum Type{ AIR, GROUND, WATER };
Cell()
: mType( AIR )
, mMass( 0 )
, mNewMass( 0 )
{}
Type mType;
float mMass;
float mNewMass;
};
const float MaxMass = 1.0f;
const float MinMass = 0.0001f;
const float MaxCompress = 0.02f;
const float MaxSpeed = 1.0f;
const float MinFlow = 0.01f;
//Take an amount of water and calculate how it should be split among two
//vertically adjacent cells. Returns the amount of water that should be in
//the bottom cell.
float get_stable_state_b( float total_mass )
{
if ( total_mass <= 1 )
{
return 1;
}
else if ( total_mass < 2*MaxMass + MaxCompress )
{
return (MaxMass*MaxMass + total_mass*MaxCompress)/(MaxMass + MaxCompress);
}
else
{
return (total_mass + MaxCompress)/2;
}
}
template< typename T >
T constrain( const T& val, const T& minVal, const T& maxVal )
{
return max( minVal, min( val, maxVal ) );
}
typedef Matrix< Cell > State;
void stepState( State& cur )
{
for( size_t y = 1; y < cur.rows()-1; ++y )
{
for( size_t x = 1; x < cur.cols()-1; ++x )
{
Cell& center = cur( y, x );
// Skip inert ground blocks
if( center.mType == Cell::GROUND )
continue;
// Custom push-only flow
float Flow = 0;
float remaining_mass = center.mMass;
if( remaining_mass <= 0 )
continue;
// The block below this one
Cell& below = cur( y-1, x );
if( below.mType != Cell::GROUND )
{
Flow = get_stable_state_b( remaining_mass + below.mMass ) - below.mMass;
if( Flow > MinFlow )
{
//leads to smoother flow
Flow *= 0.5;
}
Flow = constrain( Flow, 0.0f, min(MaxSpeed, remaining_mass) );
center.mNewMass -= Flow;
below.mNewMass += Flow;
remaining_mass -= Flow;
}
if ( remaining_mass <= 0 )
continue;
// Left
Cell& left = cur( y, x-1 );
if ( left.mType != Cell::GROUND )
{
// Equalize the amount of water in this block and it's neighbour
Flow = ( center.mMass - left.mMass ) / 4;
if ( Flow > MinFlow )
{
Flow *= 0.5;
}
Flow = constrain(Flow, 0.0f, remaining_mass);
center.mNewMass -= Flow;
left.mNewMass += Flow;
remaining_mass -= Flow;
}
if ( remaining_mass <= 0 )
continue;
// Right
Cell& right = cur( y, x+1 );
if ( right.mType != Cell::GROUND )
{
// Equalize the amount of water in this block and it's neighbour
Flow = ( center.mMass - right.mMass ) / 4;
if ( Flow > MinFlow )
{
Flow *= 0.5;
}
Flow = constrain(Flow, 0.0f, remaining_mass);
center.mNewMass -= Flow;
right.mNewMass += Flow;
remaining_mass -= Flow;
}
if ( remaining_mass <= 0 )
continue;
// The block above this one
Cell& above = cur( y+1, x );
if( above.mType != Cell::GROUND )
{
Flow = remaining_mass - get_stable_state_b( remaining_mass + above.mMass );
if( Flow > MinFlow )
{
//leads to smoother flow
Flow *= 0.5;
}
Flow = constrain( Flow, 0.0f, min(MaxSpeed, remaining_mass) );
center.mNewMass -= Flow;
above.mNewMass += Flow;
remaining_mass -= Flow;
}
}
}
for( size_t y = 0; y < cur.rows(); ++y )
{
for( size_t x = 0; x < cur.cols(); ++x )
{
cur( y, x ).mMass = cur( y, x ).mNewMass;
}
}
for( size_t y = 0; y < cur.rows(); ++y )
{
for( size_t x = 0; x < cur.cols(); ++x )
{
Cell& center = cur( y, x );
if( center.mType == Cell::GROUND )
{
center.mMass = center.mNewMass = 0.0f;
continue;
}
if( center.mMass > MinMass )
{
center.mType = Cell::WATER;
}
else
{
center.mType = Cell::AIR;
center.mMass = 0.0f;
}
}
}
// Remove any water that has left the map
for( size_t x = 0; x < cur.cols(); ++x )
{
cur( 0, x ).mMass = 0;
cur( cur.rows()-1, x ).mMass = 0;
}
for( size_t y = 0; y < cur.rows(); ++y )
{
cur( y, 0 ).mMass = 0;
cur( y, cur.cols()-1 ).mMass = 0;
}
}
void showState( const State& state )
{
glPolygonMode( GL_FRONT, GL_LINE );
glBegin( GL_QUADS );
glColor3ub( 0, 0, 0 );
for( size_t y = 0; y < state.rows(); ++y )
{
for( size_t x = 0; x < state.cols(); ++x )
{
glVertex2f( x+0, y+0 );
glVertex2f( x+1, y+0 );
glVertex2f( x+1, y+1 );
glVertex2f( x+0, y+1 );
}
}
glEnd();
glPolygonMode( GL_FRONT, GL_FILL );
glBegin( GL_QUADS );
for( size_t y = 0; y < state.rows(); ++y )
{
for( size_t x = 0; x < state.cols(); ++x )
{
if( state( y, x ).mType == Cell::AIR )
continue;
float height = 1.0f;
if( state( y, x ).mType == Cell::GROUND )
{
glColor3ub( 152, 118, 84 );
}
else
{
glColor3ub( 0, 135, 189 );
height = min( 1.0f, state( y, x ).mMass );
}
glVertex2f( x+0, y );
glVertex2f( x+1, y );
glVertex2f( x+1, y + height );
glVertex2f( x+0, y + height );
}
}
glEnd();
}
State state( 20, 20 );
void mouse( int button, int button_state, int x, int y )
{
float pctX = (float)x / glutGet( GLUT_WINDOW_WIDTH );
float pctY = 1.0f - ( (float)y / glutGet( GLUT_WINDOW_HEIGHT ) );
size_t cellX = pctX * state.cols();
size_t cellY = pctY * state.rows();
Cell& cur = state( cellY, cellX );
if( button_state == GLUT_UP )
return;
if( button == GLUT_LEFT_BUTTON )
{
cur.mType = ( cur.mType == Cell::GROUND ? Cell::AIR : Cell::GROUND );
cur.mMass = cur.mNewMass = 0.0f;
}
if( button == GLUT_RIGHT_BUTTON )
{
cur.mType = Cell::WATER;
cur.mMass = cur.mNewMass = 1.0f;
}
}
void display()
{
static bool firstTime = true;
if( firstTime )
{
firstTime = false;
for( size_t y = 0; y < state.rows(); ++y )
{
for( size_t x = 0; x < state.cols(); ++x )
{
state( y, x ).mType = (Cell::Type)( rand() % 3 );
state( y, x ).mMass = 1.0f;
state( y, x ).mNewMass = 1.0f;
}
}
}
glClearColor( 1, 1, 1, 1 );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, state.cols(), 0, state.rows(), -1, 1);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
stepState( state );
showState( state );
glutSwapBuffers();
}
void timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "Cells" );
glutDisplayFunc( display );
glutMouseFunc( mouse );
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
}
关于c++ - 基于瓦片的水的二维元胞自动机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13198765/
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在制作一个游戏,世界地图将由称为 Tiles 的 block 创建。所有图 block 都保存在单个 PNG 文件中,类似于下面发布的文件: 我需要分割这个图像并将所有这些 block 分别存储在
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Android custom control to display map tiles 我是 Java 的新
我使用 raster2pgsql 将栅格文件导入到 PostGIS 中,我设置了 -t 50x50,它生成了大约 500 行。我知道这些 -t 将光栅分成小块,并且 rid 可以为它们编制索引。我看到
我正在我的移动应用(Cordova 应用)中使用 Google map 。其中我在 map 上制作一些多边形作为边界。我希望用户也可以在离线模式下查看边界/多边形。对于离线 map ,我正在使用 Le
我现在有这个代码:http://jsfiddle.net/DK67k/2/这里是 2D 瓦片 map ,当你点击瓦片时,你会得到坐标。但是要获得精确坐标,您需要单击左上图 block (图 block
我正在开发一款 Mario 游戏,需要有关如何为图 block map 创建命中检测的帮助和建议。 目前,玩家可以步行/跳跃穿过方 block 。我现在在地面上添加了一个固定检测,我希望用常规的命中检
我正在尝试以灰度显示通过 Cartopy 的 img_tiles 模块下载的图 block 。那是因为我想覆盖彩色编码的卫星数据,并且我更喜欢背景尽可能不分散注意力。 source code 中记录的
问题来了。我有大小为 1 的矩形 Canvas 。因此它的坐标系为 (0.0 ... 1.0 - x 和 0.0 ... 1.0 - y)。 我也有一些瓷砖。瓷砖也是矩形的。它们有不同的大小,瓷砖的数
由于 - (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView 在从缓存中加载图 block 时未调用,是否有办法知道何时加载了所有图 block
我阅读了 google android api 并且对 map 图 block 是什么感到困惑,因为该 api 描述了 google map 如何处理“下载 map 图 block ”。(来源)- h
我一直在尝试在我的 flutter 应用程序的卡片最左边放置一个线条指示器。我尝试了很多东西但没有用。 有一个确切的问题here ,这实际上是我一直试图解决的同一个问题。 公认解决方案的问题是它限制了
最近我正在使用 Phaser.js 开发一个游戏,我开始需要我的瓦片 map 对象,我来看看这个 video . 唯一的问题是看到这个的“ Sprite 制造对象”example ,这个方法用到了ma
我对 libgdx 瓦片 map 中单位比例的实现感到很困惑。例如,如果我将我的单位比例指定为 1/16f,并让一个 Actor 在我的逻辑世界中从 (0,0) 移动到 (0,1),那么 Sprite
我已经为此工作好几天了。我在关卡编辑器中创建了一个瓦片 map 。它可以很好地加载到我的代码中,但是当我遍历这些图 block 时,它们都没有显示为具有定义。不确定我做错了什么。 一切运行正常,但它不
所以我正在尝试构建一个由图 block 组成的基于 3D 的世界。 我已经使用平面几何形状和高度值等成功地做到了这一点。但现在我已经到了可能不得不改变一切的地步。 问题是我想要一个图 block 具有
我遵循将瓦片 map 转换为视网膜显示的指南,方法是将宽度和高度的大小更改为对象的大小和位置的两倍。但正常显示的结果与视网膜显示不同 正常显示正确但视网膜显示不正确 这是非视网膜瓦片 map 这是视网
我正在用 SFML 制作一个 2D 游戏,我正在尝试弄清楚如何与瓦片 map 中的瓦片发生碰撞。我已经编写了一些代码,但问题是我卡在了正在碰撞的方 block 中,如下图所示。我无法向任何方向移动,播
我正在创建一个 HTML5 平台游戏,使用对象进行碰撞检测并使用 2d 图 block map 来渲染关卡。这就是全部工作。 我想使用相同的二维数组动态构建对象数组,以允许玩家根据需要构建 map ,
我正在尝试使用 libgdx 为我的家人制作一个简短的圣诞节游戏,当向前穿过关卡时,屏幕边缘会闪烁,但当向后移动时,没有闪烁,这很烦人。 Here是我的意思的演示。 此外,这是我的代码: if (di
我是一名优秀的程序员,十分优秀!