Dynamic Light effect with Cocos2d-x 4.0 and C++ - c++

I've been trying to create a dynamic light effect with cocos2d-x, but I cannot find a good tutorial for the 4.0 version.
For the context, I'm developping a top-down game for as a personnal project. The light would spread directly in front of the player like a flash-light, lighting every object in front of him and "colliding" with the walls and entities
The Shader hint
I've found multiple repos creating a custom light effect with cocos2d-x, but they all use the 3.0 version. The 4.0 version changed how the OpenGL code is managed in the Cocos backend.
Here's the repos I've found :
https://github.com/CodeAndWeb/cocos2d-x-dynamic-lighting
https://github.com/zerodarkzone/Cocos2d-x-lights
https://github.com/wantnon2/EffectNodes-for-cocos2dx
I've also read the Cocos2d-x Gitbook which explains what exactly changed in the CCSprite file.
Since I'm a beginner to Cocos2d-x and shader rendering, I did not understand what I had to change to make the old repos work ..
The polygon hint
I tried to create a VisionZone that would be a Cocos2d-x Node. The node would have a physic body colliding with the walls. On collision, the shape would update and adapt its shape.
Here's what the code would look like :
VisionZone.h
class VisionZone : public Node {
public:
static std::vector<Vec2> sm_shapeCollisionPoints;
static VisionZone *create(Vec2 origin, int visionDetail);
bool init() override;
void update(float delta) override;
void UpdateWithPlayer(Vec2 playerPos);
private:
void CreateNewPhysicBody(std::vector<Vec2> &points);
void UpdateShapeAndRedraw();
static bool IsInLine(Vec2 segmentStart, Vec2 segmentEnd, Vec2 point);
void CompareWithContactPoints();
void Rotate();
std::vector<Vec2> m_nonCollidedSegmentsEnds;
std::vector<Vec2> m_collidedSegmentsEnds;
Vec2 m_origin;
DrawNode *m_pVisionZoneDrawer;
int m_visionDetail;
VisionZone.cpp
std::vector<Vec2> VisionZone::sm_shapeCollisionPoints = {};
VisionZone *VisionZone::create(Vec2 origin, int visionDetail) {
auto *_obj = new(std::nothrow) VisionZone();
if (_obj && _obj->init()) {
_obj->m_origin = origin;
_obj->m_visionDetail = visionDetail;
int mid = std::floor(_obj->m_visionDetail * 0.5);
for (int i = -(mid); i <= mid; i++) {
_obj->m_nonCollidedSegmentsEnds.emplace_back(
static_cast<float>(_obj->m_origin.x + float(i) * 1.3f), _obj->m_origin.y + 300
);
}
_obj->m_collidedSegmentsEnds = _obj->m_nonCollidedSegmentsEnds;
std::vector<Vec2> _points = _obj->m_nonCollidedSegmentsEnds;
_points.emplace_back(_obj->m_origin);
_obj->CreateNewPhysicBody(_points);
} else
CC_SAFE_DELETE(_obj);
return _obj;
}
bool VisionZone::init() {
if (!Node::init()) return false;
m_pVisionZoneDrawer = DrawNode::create();
m_pVisionZoneDrawer->setPosition(m_origin);
assert(m_pVisionZoneDrawer);
addChild(m_pVisionZoneDrawer);
return true;
}
void VisionZone::update(float delta) {
Node::update(delta);
CompareWithContactPoints();
Rotate();
UpdateShapeAndRedraw();
}
void VisionZone::UpdateWithPlayer(Vec2 playerPos) {
m_origin = playerPos;
setPosition(m_origin);
}
void VisionZone::CreateNewPhysicBody(std::vector<Vec2> &points) {
points.push_back(m_origin);
PhysicsBody *_body = PhysicsBody::createEdgePolygon(
&points.front(),
int(points.size()),
PHYSICSBODY_MATERIAL_DEFAULT,
1
);
_body->setCategoryBitmask(vision_zone_collision_bitmask);
_body->setCollisionBitmask(map_collision_bitmask);
_body->setContactTestBitmask(true);
_body->setDynamic(false);
setPhysicsBody(_body);
}
bool VisionZone::IsInLine(Vec2 segmentStart, Vec2 segmentEnd, Vec2 point) {
float _startToPoint = sqrt(pow((segmentStart.x - point.x), 2) + pow((segmentStart.y - point.y), 2));
float _pointToEnd = sqrt(pow((point.x - segmentEnd.x), 2) + pow((point.y - segmentEnd.y), 2));
float _startToEnd = sqrt(pow((segmentStart.x - segmentEnd.x), 2) + pow((segmentStart.y - segmentEnd.y), 2));
return (_startToPoint + _pointToEnd == _startToEnd);
}
void VisionZone::CompareWithContactPoints() {
if (sm_shapeCollisionPoints.empty()) {
m_collidedSegmentsEnds = {m_origin, m_nonCollidedSegmentsEnds.front(), m_nonCollidedSegmentsEnds.back()};
return;
}
for (Vec2 &_nonCollidedEnd: m_nonCollidedSegmentsEnds) {
for (Vec2 &_contactPoint: sm_shapeCollisionPoints) {
if (IsInLine(m_origin, _nonCollidedEnd, _contactPoint)) {
Vec2 _midPoint = (m_nonCollidedSegmentsEnds.front() + m_nonCollidedSegmentsEnds.back()) / 2;
if (IsInLine(m_nonCollidedSegmentsEnds.front(), _midPoint, _contactPoint)) {
m_collidedSegmentsEnds = {
m_origin, sm_shapeCollisionPoints.front(), sm_shapeCollisionPoints.back(),
m_nonCollidedSegmentsEnds.back()
};
} else if (IsInLine(_midPoint, m_nonCollidedSegmentsEnds.back(), _contactPoint)) {
m_collidedSegmentsEnds = {
m_origin, m_nonCollidedSegmentsEnds.front(), sm_shapeCollisionPoints.front(),
sm_shapeCollisionPoints.back()
};
}
}
}
}
}
void VisionZone::Rotate() {
float _distanceX = InputManager::GetCursorPosX() - getPositionX();
float _distanceY = InputManager::GetCursorPosY() - getPositionY();
float _angle = atan2(_distanceY, _distanceX) * 180.f / static_cast<float>(M_PI);
setRotation(_angle);
}
void VisionZone::UpdateShapeAndRedraw() {
PhysicsShape *_newShape = getPhysicsBody()->addShape(PhysicsShapePolygon::create(
m_collidedSegmentsEnds.data(), int(m_collidedSegmentsEnds.size()))
);
assert(_newShape);
m_pVisionZoneDrawer->clear();
m_pVisionZoneDrawer->drawSolidPoly(
&m_collidedSegmentsEnds.front(),
m_collidedSegmentsEnds.size(),
Color4F(1.f, 0.94, 0.7, 1)
);
}
CollisionManager.cpp
#include "CollisionManager.h"
#include "Utility/Bitmasks.h"
USING_NS_CC;
EventListenerPhysicsContact *CollisionManager::m_contactListener = nullptr;
void CollisionManager::Init() {
m_contactListener = EventListenerPhysicsContact::create();
m_contactListener->onContactBegin = [](PhysicsContact &contact) { return ContactBeginCallback(contact); };
m_contactListener->onContactSeparate = [](PhysicsContact &contact) { return ContactSeparateCallback(contact); };
}
bool CollisionManager::ContactBeginCallback(PhysicsContact &contact) {
PhysicsBody *_bodyA = contact.getShapeA()->getBody();
PhysicsBody *_bodyB = contact.getShapeB()->getBody();
const bool visionAndWallCondition = ((_bodyA->getCategoryBitmask() == vision_zone_collision_bitmask &&
_bodyB->getCategoryBitmask() == map_collision_bitmask) ||
(_bodyB->getCategoryBitmask() == vision_zone_collision_bitmask &&
_bodyA->getCategoryBitmask() == map_collision_bitmask));
if (visionAndWallCondition) {
for (Vec2 _p: contact.getContactData()->points) VisionZone::sm_shapeCollisionPoints.push_back(_p);
return true;
}
return false;
}
bool CollisionManager::ContactSeparateCallback(PhysicsContact &contact) {
PhysicsBody *_bodyA = contact.getShapeA()->getBody();
PhysicsBody *_bodyB = contact.getShapeB()->getBody();
const bool visionAndWallCondition = ((_bodyA->getCategoryBitmask() == vision_zone_collision_bitmask &&
_bodyB->getCategoryBitmask() == map_collision_bitmask) ||
(_bodyB->getCategoryBitmask() == vision_zone_collision_bitmask &&
_bodyA->getCategoryBitmask() == map_collision_bitmask));
if (visionAndWallCondition) {
VisionZone::sm_shapeCollisionPoints.clear();
return true;
}
return false;
}
GameLayer.cpp
bool GameLayer::init() {
if (!Layer::init()) return false;
CollisionManager::Init();
_eventDispatcher->addEventListenerWithSceneGraphPriority(CollisionManager::GetContactListener(), this);
// ... SOME CODE ... //
m_visionZone = VisionZone::create(player->getPosition(), 100);
addChild(m_visionZone, 1);
return true;
}
void GameLayer::update(float delta) {
Node::update(delta);
m_visionZone->UpdateWithPlayer(m_player->getPosition());
}
I am not sure about this method though ... I feels like it is not optimal at all ?
Really curious to have some feedback on this, thanks in advance, sorry for the quantity of code :)
Let me know if you need more informations, code or links

Related

Cocos2d-x collision won't trigger OnContactBegin callback

I've been trying to prevent the collision between the Player and a VisionZone (a cone in front of the player that represent a flash light effect). The VisionZone is spawning on top of the player.
My problem is : since they both have physic bodies, they collide and the VisionZone pushes the player. This is not what I want.
The VisionZone and the Player have the same entity_collision_bitmask, I thought this would actually prevent them from colliding in the begining ?
Since it didn't work, I tried using a CollisionManager, but I'm never getting into the OnContactBegin callback. The Init()method is called in the init()method of the scene and I call this line :
_eventDispatcher->addEventListenerWithSceneGraphPriority(CollisionManager::GetContactListener(), this);
CollisionManager.cpp
EventListenerPhysicsContact *CollisionManager::m_contactListener = nullptr;
void CollisionManager::Init() {
m_contactListener = EventListenerPhysicsContact::create();
m_contactListener->onContactBegin = [](PhysicsContact &contact) { return ContactBeginCallback(contact); };
m_contactListener->onContactSeparate = [](PhysicsContact &contact) { return ContactSeparateCallback(contact); };
}
bool CollisionManager::ContactBeginCallback(PhysicsContact &contact) {
PhysicsBody *_bodyA = contact.getShapeA()->getBody();
PhysicsBody *_bodyB = contact.getShapeB()->getBody();
const bool visionAndWallCondition = ((_bodyA->getCategoryBitmask() == vision_zone_collision_bitmask &&
_bodyB->getCategoryBitmask() == map_collision_bitmask) ||
(_bodyB->getCategoryBitmask() == vision_zone_collision_bitmask &&
_bodyA->getCategoryBitmask() == map_collision_bitmask));
if (visionAndWallCondition) {
for (Vec2 _p: contact.getContactData()->points) VisionZone::sm_shapeCollisionPoints.push_back(_p);
return true;
}
return false;
}
bool CollisionManager::ContactSeparateCallback(PhysicsContact &contact) {
PhysicsBody *_bodyA = contact.getShapeA()->getBody();
PhysicsBody *_bodyB = contact.getShapeB()->getBody();
const bool visionAndWallCondition = ((_bodyA->getCategoryBitmask() == vision_zone_collision_bitmask &&
_bodyB->getCategoryBitmask() == map_collision_bitmask) ||
(_bodyB->getCategoryBitmask() == vision_zone_collision_bitmask &&
_bodyA->getCategoryBitmask() == map_collision_bitmask));
if (visionAndWallCondition) {
VisionZone::sm_shapeCollisionPoints.clear();
return true;
}
return false;
}
NB : For the map_collision_bitmask, it will be added to the walls physicBodies so the VisionZone collides with them, using Ray Tracing.
VisionZone.cpp
void VisionZone::CreateNewPhysicBody(std::vector<Vec2> &points) {
points.push_back(m_origin);
PhysicsBody *_body = PhysicsBody::createEdgePolygon(
&points.front(),
int(points.size()),
PHYSICSBODY_MATERIAL_DEFAULT,
1
);
_body->setCategoryBitmask(entity_collision_bitmask);
_body->setCollisionBitmask(map_collision_bitmask);
_body->setContactTestBitmask(contact_test_bitmask);
_body->setDynamic(true);
_body->setGravityEnable(false);
setPhysicsBody(_body);
}
Bitmasks.h
constexpr int vision_zone_collision_bitmask = 0x01;
constexpr int map_collision_bitmask = 0x02;
constexpr int entity_collision_bitmask = 0x03;
constexpr int contact_test_bitmask = 0x04;

Calling Variables in another function (c++)

So I've gotta create a whole bunch of Variables for some colours for a customisable menu I'm making. I wanted to know if there was a way to call the variables from the first function into other functions (there are alot) These variables have to be used in over 100 locations across about 10 different functions and I really don't want to redefine all the sh*t over again every single time. (Looks messy and is a pain if I need to make changes. Heres my Menu Colour Function
Color MenuColor(int MenuAlpha_Main)
{
Color Theme;
Color Basic = Color(c_config::get().menu_color_r, c_config::get().menu_color_g, c_config::get().menu_color_b, MenuAlpha_Main);
Color Background = Color(c_config::get().menu_background_r, c_config::get().menu_background_g, c_config::get().menu_background_b, MenuAlpha_Main);
Color MiscSelectedTab_Colour = Color(c_config::get().MiscSelectedTab_r, c_config::get().MiscSelectedTab_g, c_config::get().MiscSelectedTab_b, MenuAlpha_Main);
Color MiscSelectedTab_Highlight_Colour = Color(c_config::get().MiscSelectedTab_Highlight_r, c_config::get().MiscSelectedTab_Highlight_g, c_config::get().MiscSelectedTab_Highlight_b, MenuAlpha_Main);
Color MiscUnSelectedTab_Colour = Color(c_config::get().MiscUnSelectedTab_r, c_config::get().MiscUnSelectedTab_g, c_config::get().MiscUnSelectedTab_b, MenuAlpha_Main);
Color MiscUnSelectedTab_Highlight_Colour = Color(c_config::get().MiscUnSelectedTab_Highlight_r, c_config::get().MiscUnSelectedTab_Highlight_g, c_config::get().MiscUnSelectedTab_Highlight_b, MenuAlpha_Main);
/////rainbow sync//////
static unsigned int last_time;
last_time = GetTickCount();
Color rainbow;
rainbow.FromHSV(fmod(last_time * 0.0002f, 1.f), 1.f, 0.5f);
//////////////////////
// Oh fuck, time for the customizable shit
Color MiscSelectedTab;
Color MiscSelectedTab_Highlight;
Color MiscUnSelectedTab;
Color MiscUnSelectedTab_Highlight;
if (c_config::get().menu_colour_style == 0) {
Theme = Basic; //Normal Style
}
else if (c_config::get().menu_colour_style == 1) {
Theme = rainbow; //Rainbow
}
else if (c_config::get().menu_colour_style == 2) {
//This shit is done below
}
return Theme;
if (c_config::get().menu_colour_style == 0 || c_config::get().menu_colour_style == 0 && !c_config::get().fullmenuhighlight) {
MiscSelectedTab = Background;
MiscSelectedTab_Highlight = Theme;
MiscUnSelectedTab = Background;
MiscUnSelectedTab_Highlight = Theme;
}
else if (c_config::get().menu_colour_style == 0 || c_config::get().menu_colour_style == 0 && c_config::get().fullmenuhighlight) {
MiscSelectedTab = Theme;
MiscSelectedTab_Highlight = Theme;
MiscUnSelectedTab = Theme;
MiscUnSelectedTab_Highlight = Theme;
} // MISC SUBTAB (Misc/Colours)
else if (c_config::get().menu_colour_style == 2) {
MiscSelectedTab = MiscSelectedTab_Colour;
MiscSelectedTab_Highlight = MiscSelectedTab_Highlight_Colour;
MiscUnSelectedTab = MiscUnSelectedTab_Colour;
MiscUnSelectedTab_Highlight = MiscUnSelectedTab_Highlight_Colour;
}
return MiscSelectedTab;
return MiscSelectedTab_Highlight;
return MiscUnSelectedTab;
return MiscUnSelectedTab_Highlight;
}
I'm still fairly fresh to cpp so please don't judge. And the return values would need to be referenced in a function like this
void miscsubtab(int& current_players_esp_subtab, int tab_amount, Vector _pos, int MenuAlpha_Main)
{
int in_sizew_esp_player_subtabs = GroupBoxSize_Width - 8;
static std::string ESP_Player_SubTabs_Names[2] = { "Misc", "Colours" };
for (int i = 0; i < tab_amount; i++)
{
RECT text_size2 = g_pSurface->GetTextSizeRect(Globals::SmallText, ESP_Player_SubTabs_Names[i].c_str());
int tab_area[4] = {
_pos.x + 9 + (i * (in_sizew_esp_player_subtabs / tab_amount)), _pos.y + 52 + 5,
(in_sizew_esp_player_subtabs / tab_amount), 20
};
if (GetAsyncKeyState(VK_LBUTTON) && g_pSurface->MouseInRegion(tab_area[0], tab_area[1], tab_area[2],
tab_area[3]))
current_players_esp_subtab = i;
if (current_players_esp_subtab == i)
{
g_pSurface->FilledRect(tab_area[0], tab_area[1], tab_area[2], tab_area[3], MiscSelectedTab); //HERE
g_pSurface->FilledRect(tab_area[0], tab_area[1] + tab_area[3], tab_area[2], 3, MiscSelectedTab_Highlight); //HERE
g_pSurface->DrawT(tab_area[0] + (((in_sizew_esp_player_subtabs / tab_amount) / 2) - (text_size2.right / 2)),
tab_area[1] + (tab_area[3] / 2) - (text_size2.bottom / 2),
Color(143, 143, 143, MenuAlpha_Main), Globals::SmallText, false,
ESP_Player_SubTabs_Names[i].c_str());
}
else
{
g_pSurface->FilledRect(tab_area[0], tab_area[1], tab_area[2], tab_area[3], MiscUnSelectedTab); //HERE
g_pSurface->FilledRect(tab_area[0], tab_area[1] + tab_area[3], tab_area[2], 3, MiscUnSelectedTab_Highlight); //HERE
g_pSurface->DrawT(tab_area[0] + (((in_sizew_esp_player_subtabs / tab_amount) / 2) - (text_size2.right / 2)),
tab_area[1] + (tab_area[3] / 2) - (text_size2.bottom / 2),
Color(143, 143, 143, MenuAlpha_Main), Globals::SmallText, false,
ESP_Player_SubTabs_Names[i].c_str());
}
}
}
Any help would be greatly appreciated.
Thanks -Kenny
You can define a seperate Theme struct for it and use all colors from a Theme variable.
class Theme {
public:
enum Style {BASIC, RAINBOW, CUSTOM};
Style style;
Color MiscSelectedTab;
Color MiscSelectedTab_Highlight;
Color MiscUnSelectedTab;
Color MiscUnSelectedTab_Highlight;
}
Theme MenuColor(int MenuAlpha_Main)
{
Theme theme;
...
if (c_config::get().menu_colour_style == 0) {
theme.style = Theme::BASIC; //Normal Style
}
else if (c_config::get().menu_colour_style == 1) {
theme.style = Theme::RAINBOW; //Rainbow
}
else if (c_config::get().menu_colour_style == 2) {
theme.style = Theme::CUSTOM;
//This shit is done below
}
....
return theme;
}
...
Theme myTheme = MenuColor(5);
myTheme.MiscSelectedTab;
myTheme.MiscSelectedTab_Highlight;
myTheme.MiscUnSelectedTab;
myTheme.MiscUnSelectedTab_Highlight;

Why setPosition work in some condition but doesn't work in other condition?

i make a game let's say ThrowBall, the Player can pick up the ball spawned and throw it into target get the score added then the ball return it's position and repeat. The problem is when Ball dragged by Player (i make the Ball as child of Player) into target, the Ball return it's position correctly into desired position, but the odd happens when it collided after i send the Ball into target by applying impulse it won't return the ball into desired position, why is this happening?
i'm running this game for android, i'm running this code in VS'17 using cocos2d-x-3.17. I tried changing impulse into force, moveby. I tried making the ball stop (setVelocity to zero before setPosition). I tried changing Point into Vect, vec2. I tried to break point debug, the code do read setPosition but do nothing.
GameScene.cpp
bool GameScene::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto pickupListener = EventListenerPhysicsContact::create();
pickupListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(pickupListener, this);
return true;
}
bool GameScene::onContactBegin(cocos2d::PhysicsContact &contact)
{
PhysicsBody *a = contact.getShapeA()->getBody();
PhysicsBody *b = contact.getShapeB()->getBody();
if (
(BALL_COLLISION_BITMASK == a->getCollisionBitmask() && PLAYER_COLLISION_BITMASK == b->getCollisionBitmask()) ||
(BALL_COLLISION_BITMASK == b->getCollisionBitmask() && PLAYER_COLLISION_BITMASK == a->getCollisionBitmask())
)
{
// make the ball follow Player
isfollow = true;
}
else if (
(BALL_COLLISION_BITMASK == a->getCollisionBitmask() && TARGET_COLLISION_BITMASK == b->getCollisionBitmask()) ||
(BALL_COLLISION_BITMASK == b->getCollisionBitmask() && TARGET_COLLISION_BITMASK == a->getCollisionBitmask())
)
{
isfollow = false;
// add score
score++;
__String *tempScore = __String::createWithFormat("%i", score);
scoreLabel->setString(tempScore->getCString());
// return spawn ball
ball->returnPos();
return false;
}
void GameScene::throwBall() {
if (isfollow == true) {
isfollow = false;
ball->getSprite()->getPhysicsBody()->applyImpulse(Vec2(100000, 100000));
}
}
Ball.h
class Ball
{
public:
Ball(cocos2d::Layer *layer);
cocos2d::Sprite *getSprite() { return randomSpawn; };
void returnPos();
private:
cocos2d::Size visibleSize;
cocos2d::Vec2 origin;
cocos2d::Sprite *randomSpawn;
};
Ball.cpp
Ball::Ball(cocos2d::Layer *layer)
{
visibleSize = Director::getInstance()->getVisibleSize();
origin = Director::getInstance()->getVisibleOrigin();
randomSpawn = Sprite::create("res/ball.png");
randomSpawn->setPosition(Vec2(400, 80));
auto randomBallBody = PhysicsBody::createCircle(randomSpawn->getContentSize().width / 2);
randomBallBody->setCollisionBitmask(BALL_COLLISION_BITMASK);
randomBallBody->setContactTestBitmask(true);
randomBallBody->setGravityEnable(false);
randomSpawn->setPhysicsBody(randomBallBody);
layer->addChild(randomSpawn);
}
void Ball::returnPos()
{
randomSpawn->setPosition(Vec2(400, 80));
}
i want to make the object (Ball) return position when collided into Target and repeat. i'm sorry if the format is a mess, i'm new here, also it's not my full code, it's actually works fine i can run it, but only the setPosition won't work

LevelHelper Car Design Revolute joint anchor issue

I am trying to replicate the effect from this link Side way car movement I am using levelHelper to design the car. but unable to replicate the effect. I Think i am doing mistake in revolute joint. Here is the code i am trying.
#include "GamePlayScene.h"
#include "LevelManager.h"
#include "Constants.h"
#include "iostream"
void GamePlayScene::InitPhysics()
{
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
m_pB2World = new b2World(gravity);
m_pB2World->SetAllowSleeping(true);
m_pB2World->SetContinuousPhysics(true);
m_pDebugDraw = new GLESDebugDraw( LevelHelperLoader::pixelsToMeterRatio() );
PTM_RATIO = LevelHelperLoader::pixelsToMeterRatio();
m_pB2World->SetDebugDraw(m_pDebugDraw);
m_pLoader= new LevelHelperLoader("track7.plhs");
m_pLoader->addObjectsToWorld(m_pB2World,this);
m_pB2World->ClearForces();
//m_pB2World->DestroyJoint()
LHSprite * carBody = m_pLoader->spriteWithUniqueName("CarBody");
m_pCarBody = carBody->getBody();
m_pCarBody->GetFixtureList()->SetDensity(CAR_BODY_DENSITY);
m_pCarBody->ResetMassData();
m_pCarBody->GetFixtureList()->SetFriction(CAR_BODY_FRICTION);
m_pCarBody->GetFixtureList()->SetRestitution(CAR_BODY_RESTITUTION);
LHSprite * chasis = m_pLoader->spriteWithUniqueName("Chasis");
m_pChasis = chasis->getBody();
m_pChasis->GetFixtureList()->SetDensity(CHASIS_DENSITY);
m_pChasis->ResetMassData();
m_pChasis->GetFixtureList()->SetFriction(CHASIS_FRICTION);
m_pChasis->GetFixtureList()->SetRestitution(CHASIS_RESTITUTION);
//m_pChasis->remove
LHSprite * frontWheel = m_pLoader->spriteWithUniqueName("FrontWheel");
m_pFrontWheel = frontWheel->getBody();
m_pFrontWheel->GetFixtureList()->SetDensity(FRONT_WHEEL_DENSITY);
m_pFrontWheel->ResetMassData();
m_pFrontWheel->GetFixtureList()->SetFriction(FRONT_WHEEL_FRICTION);
m_pFrontWheel->GetFixtureList()->SetRestitution(FRONT_WHEEL_RESTITUTION);
LHSprite * rearWheel = m_pLoader->spriteWithUniqueName("RearWheel");
m_pRearWheel = rearWheel->getBody();
m_pRearWheel->GetFixtureList()->SetDensity(REAR_WHEEL_DENSITY);
m_pRearWheel->ResetMassData();
m_pRearWheel->GetFixtureList()->SetFriction(REAR_WHEEL_FRICTION);
m_pRearWheel->GetFixtureList()->SetRestitution(REAR_WHEEL_RESTITUTION);
LHSprite * frontDamper = m_pLoader->spriteWithUniqueName("FrontDamper");
m_pFrontDamper = frontDamper->getBody();
m_pFrontDamper->GetFixtureList()->SetDensity(FRONT_DAMPER_DENSITY);
m_pFrontDamper->ResetMassData();
m_pFrontDamper->GetFixtureList()->SetFriction(FRONT_DAMPER_FRICTION);
m_pFrontDamper->GetFixtureList()->SetRestitution(FRONT_DAMPER_RESTITUTION);
LHSprite * rearDamper = m_pLoader->spriteWithUniqueName("RearDamper");
m_pRearDamper = rearDamper->getBody();
m_pRearDamper->GetFixtureList()->SetDensity(REAR_DAMPER_DENSITY);
m_pRearDamper->ResetMassData();
m_pRearDamper->GetFixtureList()->SetFriction(REAR_DAMPER_FRICTION);
m_pRearDamper->GetFixtureList()->SetRestitution(REAR_DAMPER_RESTITUTION);
b2PolygonShape * shape = (b2PolygonShape *)m_pChasis->GetFixtureList()->GetShape();
//shape->SetAsBox(
LHJoint * frontPrismJoint = frontDamper->jointWithUniqueName("FrontDamperChasis");
LH_JOINT_TYPE type2 = frontPrismJoint->getType();
if(type2 == LH_PRISMATIC_JOINT)
{
//m_pFrontPrismJoint = (b2PrismaticJoint *)frontPrismJoint;
//b2PrismaticJointDef prismJointDef;
frontPrismJointDef = new b2PrismaticJointDef();
//b2Vec2 anchor = m_pFrontDamper->GetWorldCenter() - m_pChasis->GetWorldCenter();
frontPrismJointDef->Initialize(m_pChasis,m_pFrontDamper, m_pFrontDamper->GetWorldCenter(), b2Vec2(0, 1));
frontPrismJointDef->enableLimit = FRONT_PRISM_JOINT_ENABLE_LIMIT;
frontPrismJointDef->enableMotor = FRONT_PRISM_JOINT_ENABLE_MOTOR;
frontPrismJointDef->lowerTranslation = FRONT_PRISM_JOINT_LOWERTRANSLATION;
frontPrismJointDef->upperTranslation = FRONT_PRISM_JOINT_UPPERTRANSLATION;
frontPrismJointDef->maxMotorForce = FRONT_PRISM_JOINT_MAXMOTORFORCE;
//b2Joint * joint = (b2Joint *)frontPrismJoint;
//frontPrismJointDef->
m_pFrontSpring = (b2PrismaticJoint *)m_pB2World->CreateJoint(frontPrismJointDef);
//m_pB2World->DestroyJoint(joint);
}
LHJoint * rearPrismJoint = rearDamper->jointWithUniqueName("RearDamperChasis");
type2 = rearPrismJoint->getType();
if(type2 == LH_PRISMATIC_JOINT)
{
//m_pRearPrismJoint = (b2PrismaticJoint *)rearPrismJoint;
rearPrismJointDef = new b2PrismaticJointDef();
rearPrismJointDef->Initialize(m_pChasis, m_pRearDamper, m_pRearDamper->GetWorldCenter(), b2Vec2(0, 1));
rearPrismJointDef->enableLimit = REAR_PRISM_JOINT_ENABLE_LIMIT;
rearPrismJointDef->enableMotor = REAR_PRISM_JOINT_ENABLE_MOTOR;
rearPrismJointDef->lowerTranslation = REAR_PRISM_JOINT_LOWERTRANSLATION;
rearPrismJointDef->upperTranslation = REAR_PRISM_JOINT_UPPERTRANSLATION;
rearPrismJointDef->maxMotorForce = REAR_PRISM_JOINT_MAXMOTORFORCE;
m_pRearSpring = (b2PrismaticJoint *)m_pB2World->CreateJoint(rearPrismJointDef);
}
LHJoint * frontRevoluteJoint = frontWheel->jointWithUniqueName("FrontDamperWheel");
//frontWheel->jo
LH_JOINT_TYPE type = frontRevoluteJoint->getType();
if(type == LH_REVOLUTE_JOINT)
{
m_pFrontMotor = (b2RevoluteJoint *)frontRevoluteJoint;
frontRevoluteJointDef = new b2RevoluteJointDef();
frontRevoluteJointDef->Initialize(m_pFrontDamper, m_pFrontWheel, m_pFrontWheel->GetWorldCenter());
frontRevoluteJointDef->motorSpeed = FRONT_REVOLUTE_JOINT_MOTORSPEED;
frontRevoluteJointDef->maxMotorTorque = FRONT_REVOLUTE_JOINT_MAXMOTORTORQUE;
frontRevoluteJointDef->enableMotor = FRONT_REVOLUTE_JOINT_ENABLE_MOTOR;
m_fFrontMotorSpeed = FRONT_REVOLUTE_JOINT_MOTORSPEED;
m_fFrontMaxMotorTorque = FRONT_REVOLUTE_JOINT_MAXMOTORTORQUE;
m_pFrontMotor = (b2RevoluteJoint *)m_pB2World->CreateJoint(frontRevoluteJointDef);
}
LHJoint * rearRevoluteJoint = rearWheel->jointWithUniqueName("RearDamperWheel");
type = rearRevoluteJoint->getType();
if(type == LH_REVOLUTE_JOINT)
{
//m_pRearRevoluteJoint = (b2RevoluteJoint *)rearRevoluteJoint;
rearRevoluteJointDef = new b2RevoluteJointDef();
rearRevoluteJointDef->Initialize(m_pRearDamper, m_pRearWheel, m_pRearWheel->GetWorldCenter());// - b2Vec2(0.1f,0.0f));
rearRevoluteJointDef->motorSpeed = REAR_REVOLUTE_JOINT_MOTORSPEED;
rearRevoluteJointDef->maxMotorTorque = REAR_REVOLUTE_JOINT_MAXMOTORTORQUE;
rearRevoluteJointDef->enableMotor = REAR_REVOLUTE_JOINT_ENABLE_MOTOR;
m_fRearMotorSpeed = REAR_REVOLUTE_JOINT_MOTORSPEED;
m_fRearMaxMotorTorque = REAR_REVOLUTE_JOINT_MAXMOTORTORQUE;
//revoluteJointDef.
m_pRearMotor = (b2RevoluteJoint *)m_pB2World->CreateJoint(rearRevoluteJointDef);
//m_fMotorSpeed = m_pRearMotor->GetMotorSpeed();
}
}
void GamePlayScene::ccTouchesBegan(cocos2d::CCSet *pTouches, CCEvent *pEvent)
{
CCTouch * touch = (CCTouch *)(pTouches->anyObject());
CCPoint location = touch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
if(location.x > mSize.width/2)
{
bIsAcceleration = true;
bIsBrake = false;
m_pFrontMotor->EnableMotor(true);
m_pRearMotor->EnableMotor(true);
//m_pFrontWheel->SetLinearVelocity(b2Vec2(-20.0f, 0.0f));
direction = 1;
oppositeDirection = -1;
}
else
{
m_pFrontMotor->EnableMotor(true);
m_pRearMotor->EnableMotor(true);
bIsAcceleration = false;
bIsBrake = true;
direction = -1;
oppositeDirection = 1;
//m_pRearWheel->SetLinearVelocity(b2Vec2(25.0f, 0.0f));
}
}
void GamePlayScene::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
//bIsAcceleration=true;
}
void GamePlayScene::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
bIsAcceleration=false;
bIsBrake = false;
//direction *= -1;
//m_pFrontMotor->EnableMotor(false);
}
void GamePlayScene::clickOnBackwardButn(CCObject * pSender)
{
bIsAcceleration = false;
m_pFrontMotor->EnableMotor(false);
m_pRearMotor->EnableMotor(false);
//m_fMotorSpeed = 0.0;
m_pRearMotor->SetMotorSpeed(-50.0f);
m_pFrontMotor->SetMotorSpeed(-50.0f);
m_pRearMotor->SetMaxMotorTorque(50.0f);
m_pFrontMotor->SetMaxMotorTorque(50.0f);
//m_pRearWheel->SetLinearVelocity(b2Vec2(-(10.0f),0.0f));
}
void GamePlayScene::clickOnForwardButn(CCObject * pSender)
{
m_pFrontMotor->EnableMotor(true);
m_pRearMotor->EnableMotor(true);
bIsAcceleration = true;
//m_pFrontWheel->SetLinearVelocity(b2Vec2(15.0f + m_fVelocityAcceleration,0.0f));
}
void GamePlayScene::draw()
{
CCLayer::draw();
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
kmGLPushMatrix();
m_pB2World->DrawDebugData();
kmGLPopMatrix();
}
void GamePlayScene::update(float dt)
{
b2Vec2 position=m_pFrontWheel->GetPosition();
int x=position.x*32.0f;
int y=position.y*32.0f;
this->setPositionX(480.0f*90/100 + -x);
if(bIsAcceleration)
{
m_pFrontMotor->SetMotorSpeed(30 * M_PI * direction);
m_pFrontMotor->SetMaxMotorTorque(34);
m_pRearMotor->SetMotorSpeed(30 * M_PI * direction);
m_pRearMotor->SetMaxMotorTorque(24);
//m_pChasis->ApplyTorque(20 * direction);
}
else if(bIsBrake)
{
m_pFrontMotor->SetMotorSpeed(30 * M_PI * direction);
m_pFrontMotor->SetMaxMotorTorque(24);
m_pRearMotor->SetMotorSpeed(30 * M_PI * direction);
m_pRearMotor->SetMaxMotorTorque(34);
//m_pCarBody->ApplyTorque(30 * direction);
}
else
{
m_pFrontMotor->SetMotorSpeed(0.0f);
m_pFrontMotor->SetMaxMotorTorque(0.5f);
m_pRearMotor->SetMotorSpeed(0.0f);
m_pRearMotor->SetMaxMotorTorque(0.5f);
}
/*if(m_fMotorSpeed >= 50.0f)
{
m_fMotorSpeed = 50.0f;
}*/
m_pFrontSpring->SetMaxMotorForce(10 + abs(100 * pow(m_pFrontSpring->GetJointTranslation(), 2)));
m_pFrontSpring->SetMotorSpeed(-4*pow(m_pFrontSpring->GetJointTranslation(), 1));
m_pRearSpring->SetMaxMotorForce(30 + abs(100 * pow(m_pRearSpring->GetJointTranslation(), 2)));
m_pRearSpring->SetMotorSpeed((m_pRearSpring->GetMotorSpeed() - 10*m_pRearSpring->GetJointTranslation())*0.4);
int32 velocityIterations = 8;
int32 positionIterations = 6;
m_pB2World->Step(dt, velocityIterations, positionIterations);
}
void GamePlayScene::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}

error C2146: syntax error : missing ';' before identifier 'ContextRecord'

i have a header file which contained all of the class' functions including code so the class didn't have a cpp file. everything worked. I added the cpp file and moved the function code over to that and now i get this error when compiling. the header that im getting the error in ((x86)\microsoft sdks\windows\v7.0a\include\winnt.h(6361)) isnt even included by the file that im changing. does anyone know what the reason for this might be? i can provide code i just don't know what would be helpful.
the cpp file:
#include "Fisherman.h"
void Fisherman::Initialise(){
memset((void*)&mListener, 0, sizeof(X3DAUDIO_LISTENER));
memset((void*)&mEmitter, 0, sizeof(X3DAUDIO_EMITTER));
memset((void*)&mDSPSettings, 0, sizeof(X3DAUDIO_DSP_SETTINGS));
XAUDIO2_VOICE_DETAILS details;
mCastSplash->GetSourceVoice()->GetVoiceDetails(&details);
mEmitter.ChannelCount = details.InputChannels;
mEmitter.CurveDistanceScaler = 1.0f;
X3DAUDIO_VECTOR emitterPos = { 0.0f, 0.0f, 0.0f};
mEmitter.Position = emitterPos;
X3DAUDIO_VECTOR emitterVel = { 0.0f, 0.0f, 0.0f };
mEmitter.Velocity = emitterVel;
mDSPSettings.SrcChannelCount = mEmitter.ChannelCount;
mDSPSettings.DstChannelCount = mXACore->GetChannelCount();
FLOAT32 * matrix = new FLOAT32[mDSPSettings.SrcChannelCount * mDSPSettings.DstChannelCount];
mDSPSettings.pMatrixCoefficients = matrix;
X3DAUDIO_VECTOR front = { 0.0f, 0.0f, 1.0f };
X3DAUDIO_VECTOR top = { 0.0f, 1.0f, 0.0f };
mListener.OrientFront = front;
mListener.OrientTop = top;
X3DAUDIO_VECTOR listenerVel = {0.0f, 0.0f, 0.0f};
mListener.Velocity = listenerVel;
X3DAUDIO_VECTOR listenerPos = { 0.0f, 0.0f, 0.0f };
mListener.Position = listenerPos;
}
void Fisherman::Rotate (int MouseDeltaX){
X3DAUDIO_VECTOR input = mListener.OrientFront;
X3DAUDIO_VECTOR result;
float theta = -(X3DAUDIO_PI/1000)*MouseDeltaX;
float cs = cos(theta);
float sn = sin(theta);
if(cs < 0.00001) cs = 0.0f;
result.x = input.x * cs - input.z * sn;
result.z = input.x * sn + input.z * cs;
result.y = 0.0f;
mListener.OrientFront = result;
}
bool Fisherman::Cast(Fish* aFish){
mCast->Play(0);
mCastOut = true;
mFish = aFish;
X3DAUDIO_VECTOR seg_v = Multiply(mListener.OrientFront, 30);
X3DAUDIO_VECTOR pt_v = {mFish->GetX(), 0.0f, mFish->GetZ()};
float proj_v_length = Dot(pt_v, mListener.OrientFront);
X3DAUDIO_VECTOR proj_v = Multiply(mListener.OrientFront, proj_v_length);
X3DAUDIO_VECTOR dist_v = Subtract(pt_v, proj_v);
if(VectorLength(dist_v) < mFish->GetRadius()){
mEmitter.Position = mFish->GetEmitter().Position;
return true;
}else{
mEmitter.Position = Multiply(mListener.OrientFront, 15);
return false;
}
}
void Fisherman::ReelIn(Fish* aFish){
mFish = aFish;
mFish->MoveCloser(mReelSpeed);
mReelingIn = true;
}
void Fisherman::ReelOut(Fish* aFish){
mFish = aFish;
mFish->MoveFurther();
mReelingIn = false;
}
void Fisherman::SetReelSpeed(float deltaTime){
float reelSpeed = 1.0f - deltaTime;
if(reelSpeed < 0.0f){
reelSpeed = 0.0f;
}
if(reelSpeed > 10){
mReelSpeedList.push_back(reelSpeed);
if(mReelSpeedList.size() > 3){
mReelSpeedList.pop_front();
}
reelSpeed = 0.0f;
std::list<float>::const_iterator iterator;
for (iterator = mReelSpeedList.begin(); iterator != mReelSpeedList.end(); ++iterator){
reelSpeed += *iterator;
}
mReelSpeed = reelSpeed/mReelSpeedList.size();
}else
mReelSpeed = reelSpeed;
mReelClickTimer = 0.1 / mReelSpeed;
}
void Fisherman::PlayReelClick(){
if(!mReelClick[0]->IsPlaying()){
mReelClick[0]->Play(0);
}else if(!mReelClick[1]->IsPlaying()){
mReelClick[1]->Play(0);
}else if(!mReelClick[2]->IsPlaying()){
mReelClick[2]->Play(0);
}else if(!mReelClick[3]->IsPlaying()){
mReelClick[3]->Play(0);
}else if(!mReelClick[4]->IsPlaying()){
mReelClick[4]->Play(0);
}else {
return;
}
}
bool Fisherman::NothingPlaying(){ // check to see if any sounds are playing
if(mCast->IsPlaying())return false;
if(mCastSplash->IsPlaying())return false;
for(int i =0; i < REEL_CLICK_OBJECTS; i++){
if(mReelClick[i]->IsPlaying()){
return false;
}
}
return true;
}
void Fisherman::SetReelingIn(bool isReelingIn){
mReelingIn = isReelingIn;
}
void Fisherman::SetCastOut(bool hasCastOut){
mCastOut = hasCastOut;
}
float Fisherman::GetReelClickTime(){
return mReelClickTimer/CLICK_TIMER_MULTIPLIER;
}
bool Fisherman::IsReelingIn(){
return mReelingIn;
}
float Fisherman::GetReelSpeed(){
return mReelSpeed;
}
X3DAUDIO_LISTENER Fisherman::GetListener(){
return mListener;
}
X3DAUDIO_EMITTER Fisherman::GetEmitter(){
return mEmitter;
}
X3DAUDIO_DSP_SETTINGS Fisherman::GetSettings(){
return mDSPSettings;
}
XASound* Fisherman::GetCastSound(){
return mCast;
}
XASound* Fisherman::GetCastSplashSound(){
return mCastSplash;
}
bool Fisherman::HasCastSplashed(){
return mCastSplashBool;
}
void Fisherman::SetCastSplashed(bool splashed){
mCastSplashBool = splashed;
}
bool Fisherman::IsCastOut(){
return mCastOut;
}
the header:
#ifndef _FISHERMAN_H_
#define _FISHERMAN_H_
#include <X3DAudio.h>
#include <math.h>
#include <list>
#include "VectorCalculations.h"
#include "Fish.h"
#include "XASound.hpp"
using AllanMilne::Audio::XASound;
const float CLICK_TIMER_MULTIPLIER = 2.5f;
const int REEL_CLICK_OBJECTS = 5;
class Fisherman{
public:
Fisherman(XACore* aXACore, XASound *cast, XASound *castSplash, std::list<XASound*> reelClickList)
: // a Fish Object pointer for the fisherman to interact with.
mFish (NULL), mXACore (aXACore),
//XASound objects with sounds for the fisherman
mCast (cast), mCastSplash (castSplash)
{
mReelSpeedList.clear();
for(int i = 0; i < REEL_CLICK_OBJECTS; i++){
mReelClick[i] = reelClickList.front();
mReelClick[i]->SetVolume(2.0f);
reelClickList.pop_front();
mReelClickList.push_back(mReelClick[i]);
}
mCastSplash->SetVolume(7.0f);
mXACore = aXACore;
mCastSplashBool = false;
mCastOut = false;
mReelingIn = false;
mCastDistance = 0.0f;
Initialise();
}
~Fisherman(){}
void Initialise();
void Rotate(int MouseDeltaX);
bool Cast(Fish* aFish);
void ReelIn(Fish* aFish);
void ReelOut(Fish* aFish);
void SetReelSpeed(float deltaTime);
void PlayReelClick();
bool NothingPlaying(); // check to see if any sounds are playing
void SetFront(X3DAUDIO_VECTOR front);
void SetReelingIn(bool isReelingIn);
void SetCastOut(bool hasCastOut);
float GetReelClickTime();
bool IsReelingIn();
float GetReelSpeed();
X3DAUDIO_LISTENER GetListener();
X3DAUDIO_EMITTER GetEmitter();
X3DAUDIO_DSP_SETTINGS GetSettings();
XASound* GetCastSound();
XASound* GetCastSplashSound();
bool HasCastSplashed();
void SetCastSplashed(bool splashed);
bool IsCastOut();
private:
XACore *mXACore;
XASound *mCast;
XASound *mCastSplash;
XASound *mReelClick[REEL_CLICK_OBJECTS];
float mCastDistance;
float mReelClickTimer;
float mReelSpeed;
std::list<float> mReelSpeedList;
std::list<XASound*> mReelClickList;
X3DAUDIO_LISTENER mListener;
X3DAUDIO_EMITTER mEmitter;
X3DAUDIO_DSP_SETTINGS mDSPSettings;
Fish *mFish;
bool mCastOut;
bool mCastSplashBool;
bool mReelingIn;
};
#endif
including windows.h at the top of the header file solved this issue.
Does the error also say PCONTEXT undefined?
Try adding #include <Windows.h> before #include <X3DAudio.h>.
In reply to comment about unresolved external symbol:
That's because you didn't define Fisherman::SetFront in the cpp file.
Whole code would be helpfull. Did you remember obvious to give namespace to names of functions etc?
class A { void foo() { } };
=>
class A { void foo(); }
void A::foo() { ... }
etc?
Fastest way would be reverting back to state when it worked, and not moving whole at once, but only one function at a time. Then when problem would occure you could give us that specific code or try to fix it yourself.
It is very possible that there is just a missed ';', somewhere before this line.
Double-check your code, remember that everything you define in a cpp file has to already be declared in your class definition.
I'd prefer to post a comment, but apparently I'm not allowed.
Anyway, as a simpler way to give us a feel of what you're trying to do, definatley post your code - it does not have to be exactly as you use, but should be a concise piece of generic code, highlighting how you are doing something. (SSCCE) If it really is a bug in how you typed it, it would be good to have your exact code, but put it on pastebin and provide a link so that this page doesn't end up with reams of code.
EDIT:
winnt.h apparently causes all sorts of problems: Weird compile error dealing with Winnt.h
it is used to provide definitions for winAPI specific headers, as far as I know. One of the solutions proposed on that thread is to get the definitions from a different windows header before including whichever header requires those definitions.