C# 二维长方体实体碰撞
我有一个严重的问题,碰撞的重力部分,它是基于平台的,但适用 我所做的关于碰撞的一切都包含重力 当实体触地时,它会不断地将自身重新设置为不触地,不断地变为“真-假-真-假…”,同时,实体的Y速度也在0.0和-0.3之间切换 我需要它总是真实的,如果实体在地面上,而不是看起来反弹 此外,如果用户手动控制实体的Y速度,如果它与边界框的角碰撞,而角方向上有X速度,Y速度直接朝向它(X和Y之间反之亦然),则它会卡住、崩溃,然后决定在整个边界框中卡住自己C# 二维长方体实体碰撞,c#,collision,gravity,C#,Collision,Gravity,我有一个严重的问题,碰撞的重力部分,它是基于平台的,但适用 我所做的关于碰撞的一切都包含重力 当实体触地时,它会不断地将自身重新设置为不触地,不断地变为“真-假-真-假…”,同时,实体的Y速度也在0.0和-0.3之间切换 我需要它总是真实的,如果实体在地面上,而不是看起来反弹 此外,如果用户手动控制实体的Y速度,如果它与边界框的角碰撞,而角方向上有X速度,Y速度直接朝向它(X和Y之间反之亦然),则它会卡住、崩溃,然后决定在整个边界框中卡住自己 namespace StackOverFlowExa
namespace StackOverFlowExample
{
class Entity
{
/* ... */
public Collision2D Col; // The collision box for the Entity
public bool isGrounded; // Returns True if the Entity is on the ground
private List<Collision2D> TestObjt; // Allows the Entity access to all of the other Collision2D instances
public Vector2 Pos; // Entity's position
public Vector2 V; // Entity's velocity
// Declare the movement & collision Timer
private Timer movementTimer = new Timer( 1 );
private void UpdateCollision()
{
// Reset grounding
this.Col.Direction = 0;
this.isGrounded = false;
// this.isAllowingInput = false;
foreach ( Collision2D testObjt in this.TestObjt )
{
if ( this.Col.CheckCol( testObjt ) )
testObjt.CheckCol( this.Col, this );
}
}
private void UpdatePosition()
{
this.Pos = Vector2.Add( this.Pos, this.V );
this.Col.Sync( Convert.ToInt32( this.Pos.X ), Convert.ToInt32( this.Pos.Y ) );
}
private void UpdateGravity()
{
if ( this.isGrounded == false )
{
this.V.Y = this.V.Y + 0.3F;
} else this.V.Y = 0;
}
private void movementEvent( object source, ElapsedEventArgs e )
{
// Call Methods
this.UpdatePosition();
this.UpdateCollision();
this.UpdateGravity();
}
}
class Collision2D
{
public Vector2 Pos1; // Top, Left
public Vector2 Pos2; // Bottom, Right
public int Direction; // Direction of collision
public void Sync( int NewX, int NewY )
{
float W = Pos2.X - Pos1.X; // Generate Width
float H = Pos2.Y - Pos1.Y; // Generate Height
this.Pos1.X = NewX;
this.Pos1.Y = NewY;
this.Pos2.X = NewX + W;
this.Pos2.Y = NewY + H;
}
// Returns true if there is a collision
public bool CheckCol( Collision2D objt )
{
bool r = true;
if ( this.Pos2.Y < objt.Pos1.Y || this.Pos1.Y > objt.Pos2.Y || this.Pos2.X < objt.Pos1.X || this.Pos1.X > objt.Pos2.X )
r = false;
return r;
}
// Collision actions on Entity
public void CheckCol( Collision2D objt, Entity ent )
{
switch ( ent.Col.GetDirection( this ) )
{
// Ceiling Collision Actions
case 1:
ent.Pos.Y = ent.Pos.Y - ent.V.Y;
break;
// Floor Collision Actions
case 2:
ent.Pos.Y = ent.Pos.Y - ent.V.Y;
ent.isGrounded = true; // Place Entity on the ground for Gravity actions
break;
// Right Wall Collision Actions
case 3:
ent.Pos.X = ent.Pos.X - ent.V.X;
break;
// Left Wall Collision Actions
case 4:
ent.Pos.X = ent.Pos.X - ent.V.X;
break;
}
// Resync the collision bounding box for the entity
ent.Col.Sync( Convert.ToInt32( ent.Pos.X ), Convert.ToInt32( ent.Pos.Y ) );
}
public int GetDirection( Collision2D objt )
{
int r;
r = 0;
if ( objt.Pos2.Y - this.Pos1.Y < this.Pos2.Y - objt.Pos1.Y &&
objt.Pos2.Y - this.Pos1.Y < this.Pos2.X - objt.Pos1.X &&
objt.Pos2.Y - this.Pos1.Y < objt.Pos2.X - this.Pos1.X )
r = 1; // Up
if ( this.Pos2.Y - objt.Pos1.Y < objt.Pos2.Y - this.Pos1.Y &&
this.Pos2.Y - objt.Pos1.Y < this.Pos2.X - objt.Pos1.X &&
this.Pos2.Y - objt.Pos1.Y < objt.Pos2.X - this.Pos1.X )
r = 2; // Down
if ( objt.Pos2.X - this.Pos1.X < this.Pos2.X - objt.Pos1.X &&
objt.Pos2.X - this.Pos1.X < this.Pos2.Y - objt.Pos1.Y &&
objt.Pos2.X - this.Pos1.X < objt.Pos2.Y - this.Pos1.Y )
r = 3; // Right
if ( this.Pos2.X - objt.Pos1.X < objt.Pos2.X - this.Pos1.X &&
this.Pos2.X - objt.Pos1.X < this.Pos2.Y - objt.Pos1.Y &&
this.Pos2.X - objt.Pos1.X < objt.Pos2.Y - this.Pos1.Y )
r = 4; // Left
return r;
}
}
}
名称空间堆栈溢出示例
{
类实体
{
/* ... */
public Collision2D Col;//实体的冲突框
public bool isground;//如果实体在地面上,则返回True
private List TestObjt;//允许实体访问所有其他Collision2D实例
公共向量2 Pos;//实体的位置
公共向量2 V;//实体的速度
//声明移动和碰撞计时器
专用定时器移动定时器=新定时器(1);
私有void UpdateCollision()
{
//复位接地
该方向=0;
this.isGrounded=false;
//this.isAllowingInput=false;
foreach(在this.testObjt中为Collision2D testObjt)
{
if(this.Col.CheckCol(testObjt))
testObjt.CheckCol(this.Col,this);
}
}
私有void UpdatePosition()
{
this.Pos=Vector2.Add(this.Pos,this.V);
this.Col.Sync(Convert.ToInt32(this.Pos.X)、Convert.ToInt32(this.Pos.Y));
}
私有void UpdateGravity()
{
if(this.isground==false)
{
这个.V.Y=这个.V.Y+0.3F;
}否则,V.Y=0;
}
私有void movementEvent(对象源,ElapsedEventArgs e)
{
//调用方法
this.UpdatePosition();
this.UpdateCollision();
this.UpdateGravity();
}
}
类冲突2D
{
公共向量2 Pos1;//左上角
公共向量2 Pos2;//右下角
public int Direction;//冲突方向
公共无效同步(int-NewX,int-NewY)
{
float W=Pos2.X-Pos1.X;//生成宽度
float H=Pos2.Y-Pos1.Y;//生成高度
this.Pos1.X=NewX;
this.Pos1.Y=NewY;
this.Pos2.X=NewX+W;
this.Pos2.Y=NewY+H;
}
//如果发生冲突,则返回true
公共bool CheckCol(冲突2D objt)
{
布尔r=真;
if(this.Pos2.Yobjt.Pos2.Y | this.Pos2.Xobjt.Pos2.X)
r=假;
返回r;
}
//实体上的碰撞操作
公共无效检查列(冲突2D对象,实体ent)
{
开关(ent.Col.GetDirection(本))
{
//天花板碰撞动作
案例1:
ent.Pos.Y=ent.Pos.Y-ent.V.Y;
打破
//地板碰撞动作
案例2:
ent.Pos.Y=ent.Pos.Y-ent.V.Y;
ent.isground=true;//将实体放置在地面上进行重力作用
打破
//右墙碰撞动作
案例3:
ent.Pos.X=ent.Pos.X-ent.V.X;
打破
//左墙碰撞动作
案例4:
ent.Pos.X=ent.Pos.X-ent.V.X;
打破
}
//重新同步实体的碰撞边界框
ent.Col.Sync(Convert.ToInt32(ent.Pos.X)、Convert.ToInt32(ent.Pos.Y));
}
公共int GetDirection(冲突2D objt)
{
INTR;
r=0;
if(objt.Pos2.Y-this.Pos1.Y