
当我的AABB物理引擎解析一个交叉点时,它通过找到穿透力较小的轴来实现,然后"推出"该轴上的实体.
考虑到"向左跳跃"的例子:
我怎么解决这个问题?
public void Update()
{
Position += Velocity;
Velocity += World.Gravity;
List<SSSPBody> toCheck = World.SpatialHash.GetNearbyItems(this);
for (int i = 0; i < toCheck.Count; i++)
{
SSSPBody body = toCheck[i];
body.Test.Color = Color.White;
if (body != this && body.Static)
{
float left = (body.CornerMin.X - CornerMax.X);
float right = (body.CornerMax.X - CornerMin.X);
float top = (body.CornerMin.Y - CornerMax.Y);
float bottom = (body.CornerMax.Y - CornerMin.Y);
if (SSSPUtils.AABBIsOverlapping(this, body))
{
body.Test.Color = Color.Yellow;
Vector2 overlapVector = SSSPUtils.AABBGetOverlapVector(left, right, …Run Code Online (Sandbox Code Playgroud) 我正在制作一个C++ 11游戏,其中分数被发送到服务器.我将得分存储为一个简单的浮点数,因此使用像Cheat Engine这样的软件的人可以在将得分发送到服务器之前轻松更改得分值.
如何保护我的游戏免受此类攻击?
我有一个OOP实体组件系统,目前的工作方式如下:
// In the component system
struct Component { virtual void update() = 0; }
struct Entity
{
bool alive{true};
vector<unique_ptr<Component>> components;
void update() { for(const auto& c : components) c->update(); }
}
// In the user application
struct MyComp : Component
{
void update() override { ... }
}
Run Code Online (Sandbox Code Playgroud)
要创建新的实体和组件,我使用C++的常规new和delete:
// In the component system
struct Manager
{
vector<unique_ptr<Entity>> entities;
Entity& createEntity()
{
auto result(new Entity);
entities.emplace_back(result);
return *result;
}
template<typename TComp, typename... TArgs>
TComp& …Run Code Online (Sandbox Code Playgroud) 我想创建在中定义的assert宏的自定义版本,<cassert>在断言失败时显示错误消息。
所需用法:
custom_assert(AClass<T1, T2>::aBoolMethod(), "aBoolMethod must be true");
测试实现有缺陷:
#define custom_assert(mCondition, mMessage) ...
// This fails because mCondition may have commas in it
#define custom_assert(..., mMessage)
// Not sure about this either - mMessage may be an expression containing commas
// as well
Run Code Online (Sandbox Code Playgroud)
如何正确实现一个自定义断言,该自定义断言将布尔表达式(可能带有逗号)作为第一个参数,并将字符串表达式(可能带有逗号)作为第二个参数?
还是有一种不使用宏来实现断言的方法?
我正在为我的gamedev项目制作一个非常小的游戏内GUI库,但是我很难找到一种处理焦点的干净方法.
我的库支持嵌套的ListBoxes和小部件层次结构,但在处理小部件焦点时,我似乎无法找到防止奇怪行为的方法.示例表格:
|------------|
| Form [X] |
|------------|
| |
| [Button01] |
| |
| [List1][v] |
| |
| [Button02] |
| |
|------------|
Run Code Online (Sandbox Code Playgroud)
我的设计有一个Context存储Widget对象列表的对象.A Widget可以有任意数量的孩子.我有设施来递归迭代a的所有孩子/父母Widget.
我的焦点逻辑目前是:
如果上下文繁忙(拖动,调整大小,编辑...),请不要更改焦点
如果在上下文之外按下鼠标或上下文未聚焦,则将所有内容取消聚焦
找到最受压迫的孩子,如果有的话
找到层次结构中"最深"的按下子项
除了小部件之外的所有内容都不会聚焦到最深的孩子,并关注上下文
但是,这在许多情况下都失败了,特别是对于嵌套列表和TextBoxes.我试图在网上找到一个好的解决方案,但找不到任何文章/教程.我不确定是否应该专门关注特殊小部件,或者是否有"好"算法在任何情况下都能正常工作.
GUI库通常如何处理小部件焦点?
是否有特殊说明专门关注ListBoxes或ComboBoxes等小部件?
在构建一个基于lambda的小型元编程库时,我有必要在C++ 14泛型lambda中使用递归来实现左折叠.
我自己的解决方案是将lambda本身作为其参数之一传递,如下所示:
template <typename TAcc, typename TF, typename... Ts>
constexpr auto fold_l_impl(TAcc acc, TF f, Ts... xs)
{
// Folding step.
auto step([=](auto self)
{
return [=](auto y_acc, auto y_x, auto... y_xs)
{
// Compute next folding step.
auto next(f(y_acc, y_x));
// Recurse if required.
return static_if(not_empty(y_xs...))
.then([=]
{
// Recursive case.
return self(self)(next, y_xs...);
})
.else_([=]
{
// Base case.
return next;
})();
};
});
// Start the left-fold.
return step(step)(acc, xs...);
}
Run Code Online (Sandbox Code Playgroud)
step是从递归开始的"主要"lambda.它返回一个具有所需左折签名的函数(累加器,当前项,剩余项......) …
在一个内部更新值(给定键和新值)的规范方法是boost::hana::map什么?
我尝试使用boost::hana::replace_if但它不起作用,map因为它不是Functor- 我可以通过将其转换map为a tuple然后再转换为a 来实现map它,但听起来效率低下.
我目前正在使用的替代方案是调用map::erase_key后跟map::insert.
是否有任何replace或update为此目的而设计的功能可能会丢失?或者这是更新价值的"规范"方式?
我正在尝试检测可调用对象是否为二进制(即,它是否operator()采用两个参数).我想对lambdas执行此检查,包括泛型lambdas和约束泛型lambda (例如,使用尾随std::enable_if_t返回类型).
注意:这不是"普通lambda的arity"的副本.我只关心检查泛型lambda是否是二进制的,并且我已经可以为不受约束的泛型lambda或不使用其体中的参数的泛型lambda做到这一点.
我目前的方法是应用Kris Jusiak的Boost.DI C++ Now 2015谈话中描述的技术之一:any_type.它基本上是一个可以隐式转换为任何其他类的类.
struct any_type
{
template <typename T>
constexpr operator T() const noexcept
{
return {};
}
};
Run Code Online (Sandbox Code Playgroud)
在定义之后any_type,我正在使用检测习惯来检查是否可以使用两个参数调用特定的可调用对象:
template<class T>
using is_binary_callable_impl = decltype(std::declval<T>()(any_type{}, any_type{}));
template <typename T>
using is_binary_callable = std::experimental::is_detected<is_binary_callable_impl, T>;
Run Code Online (Sandbox Code Playgroud)
这种方法适用于非泛型和通用lambda ...
auto unary_nongeneric = [](int){};
auto unary_generic = [](auto){};
auto binary_nongeneric …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码段:
template <typename>
struct dependent_false { static constexpr auto value = false; };
struct foo
{
foo() { }
template <typename T>
foo(const T&) { static_assert(dependent_false<T>::value, ""); }
};
struct proxy
{
operator foo() { return foo{}; }
};
int main()
{
(void) foo{proxy{}};
}
Run Code Online (Sandbox Code Playgroud)
编译时-std=c++17:
clang++ (trunk)成功编译代码;
g++(trunk)无法编译代码 - 它实例化foo(const T&).
编译时-std=c++11,两个编译器都拒绝代码.C++ 17中新的prvalue实现规则可能会影响此处的行为.
这里的正确行为是什么?
标准是否保证foo::foo(const T&)将(或不会)实例化?
标准是否保证隐式转换运算符优先于调用foo::foo(const T&),而不管它是否被实例化?
考虑一个类模板S:
[ s.hpp]
template <typename>
struct S
{
void f() { /* ... */ }
void g() { /* ... */ }
};
Run Code Online (Sandbox Code Playgroud)
该类模板还附带一个源文件,其中包含以下专业化内容S<int>::g():
[ s.cpp]
template <>
void S<int>::g() { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
S被实例化很多次S<int>。为了通过避免多次实例化来加快编译时间S<int>,我想在头文件中引入显式实例化声明( extern template)s.hpp,并在源文件中引入相应的显式实例化定义s.cpp:
[ s.hpp] (修改的)
template <typename>
struct S
{
void f() { /* ... */ }
void g() { /* ... */ } …Run Code Online (Sandbox Code Playgroud)