i am using cocos2d and box2d, with contact listener, and lets say i have a body that can hit a number of other bodies, BUT each one of them is turn on the contact listener.
so how can i know who hit who ?
i have this in my tick :
for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos)
{
MyContact contact = *pos;
b2Body *bodyA=contact.fixtureA->GetBody();
b2Body *bodyB=contact.fixtureB->GetBody();
//check if collision between to bodies
if( bodyA->GetUserData() !=NULL && bodyB->GetUserData() !=NULL) //if ((contact.fixtureA == _bottomFixture && contact.fixtureB == _ballFixture) ||(contact.fixtureA == _ballFixture && contact.fixtureB == _bottomFixture))
{
NSLog(#"Ball hit bottom!");
}
thanks a lot .
while creating the body set userdata like this
CCSprite *red=[CCSprite spriteWithFile:#"red.png"];
red.tag=3;
[self addChild:red];
b2BodyDef bd;
bd.type=b2_dynamicBody;
bd.position.Set(w/PTM_RATIO,h/PTM_RATIO);
bd.userData=red;
for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos)
{
MyContact contact = *pos;
b2Body *bodyA=contact.fixtureA->GetBody();
b2Body *bodyB=contact.fixtureB->GetBody();
//check if collision between to bodies
if( bodyA->GetUserData() !=NULL && bodyB->GetUserData() !=NULL) //if ((contact.fixtureA == _bottomFixture && contact.fixtureB == _ballFixture) ||(contact.fixtureA == _ballFixture && contact.fixtureB == _bottomFixture))
{
so here
CCSprite *actor = (CCSprite*)bodyA->GetUserData();
if ([actor tag] == 3) {
//red box
}
}
Put some identifier into user data. For example:
struct MyUserData
{
int myUniqueId;
};
When creating bodies attach some unique number to each and then you will be able to understand which body was colliding.
Related
private void Update()
{
Score.text = puanlama.ToString();
if (puanlama < firstbarvalue)//used for first bar value to open button activeness
{
activeness.interactable = false;
}
if (puanlama >= firstbarvalue && counterbars == 1)//counterbar for button activeness
{
textforvalues.text = firstbarvalue.ToString();// button value on it
activeness.interactable = true;
}
if (counterbars == 2)
{
activeness.interactable = false;
}
if (puanlama >= secondbarvalue && counterbars == 2)
{
textforvalues.text = secondbarvalue.ToString();
activeness.interactable = true;
}
if (counterbars == 3)
{
activeness.interactable = false;
}
}
public void Buttonactiveness()// Button activeness that I assign to my button[Here button and bars that I used][1]
{
if (puanlama >= firstbarvalue && counterbars == 1)
{
Fuelbars[0].SetActive(true);
for (int i = 1; i < Fuelbars.Length; i++)
{
Fuelbars[i].SetActive(false);
}
puanlama = puanlama - firstbarvalue;
counterbars++;
}
else if (puanlama >= secondbarvalue && counterbars == 2)//Bars which is second one
{
Fuelbars[0].SetActive(true);
Fuelbars[1].SetActive(true);
for (int i = 2; i < Fuelbars.Length; i++)
{
Fuelbars[i].SetActive(false);
}
puanlama = puanlama - secondbarvalue;
counterbars++;
}
I'm trying to put some code to turn this code to player pref and save this data.
playerpref counter bar is not working any suggest
Button activeness that I assign to my buttonHere button and bars that I used
Fuelbars which I'm trying to add is important part of work
counterbar for button activeness
In update section I tried to control button activeness and I changed button text
Hi I'm new to MQL5 and I wanted to add a trailing stop loss to my expert advisor but some reason it does not add. Here is the code:
if(PositionSelect(_Symbol) && UseTrailingStop == true)
{
double TrailingStop = (atr[1] * 3) + close[1];
Trail.TrailingStop(_Symbol,TrailingStop,0,0);
}
Please note that close[1] is for the close price of the previous bar and atr[1] is for the value of the average true range. What am i doing wrong?????
There you go: hope this is helpful.
//--- trailing position
for(i=0;i<PositionsTotal();i++)
{
if(Symbol()==PositionGetSymbol(i))
{
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
{
sl=MathMax(PositionGetDouble(POSITION_PRICE_OPEN)+Spread*_Point,Bid-SL*_Point);
if(sl>PositionGetDouble(POSITION_SL) && (Bid-StopLevel*_Point-Spread*_Point)>PositionGetDouble(POSITION_PRICE_OPEN))
{
request.action = TRADE_ACTION_SLTP;
request.symbol = _Symbol;
request.sl = NormalizeDouble(sl,_Digits);
request.tp = PositionGetDouble(POSITION_TP);
OrderSend(request,result);
if(result.retcode==10009 || result.retcode==10008) // request executed
Print("Moving Stop Loss of Buy position #",request.order);
else
{
Print(ResultRetcodeDescription(result.retcode));
return;
}
return;
}
}
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
sl=MathMin(PositionGetDouble(POSITION_PRICE_OPEN)-Spread*_Point,Ask+SL*_Point);
if(sl<PositionGetDouble(POSITION_SL) && (PositionGetDouble(POSITION_PRICE_OPEN)-StopLevel*_Point-Spread*_Point)>Ask)
{
request.action = TRADE_ACTION_SLTP;
request.symbol = _Symbol;
request.sl = NormalizeDouble(sl,_Digits);
request.tp = PositionGetDouble(POSITION_TP);
OrderSend(request,result);
if(result.retcode==10009 || result.retcode==10008) // request executed
Print("Moving Stop Loss of Sell position #",request.order);
else
{
Print(ResultRetcodeDescription(result.retcode));
return;
}
return;
}
}
}
}
I want to get the distance of the obstacles in the back and front of the Mavic Air.
I'm using the VissionDetectionStateChanged and it returns values in the 4 sectors, but all of them change only with obstacles in the back. If I put my hand in the front, nothing happens.
The VisionSensorPosition is returning TAIL always and when I put my hand very close to the tail of the aircraft it changes for NOSE.
Shouldn't be the opposite?
Right now I just display the information, but I'd like to be able to detect obstacles in the back and front of the aircraft to try to keep it in the middle of two objects and avoid collisions.
This is my code in the event:
private async void FlightAssistant_VissionDetectionStateChanged(object sender, VissionDetectionState? value)
{
if (value.HasValue)
{
if (txtPosition.Dispatcher.HasThreadAccess)
{
txtPosition.Text = value.Value.position.ToString();
for (int i = 0, count = value.Value.detectionSectors.Count; i < count; i++)
{
ObstacleDetectionSector sector = value.Value.detectionSectors[i];
TextBox txtWarning = this.FindControl<TextBox>("txtWarning" + i.ToString());
if (txtWarning != null)
txtWarning.Text = sector.warningLevel.ToString();
TextBox txtObstacleDistance = this.FindControl<TextBox>("txtObstacleDistance" + i.ToString());
if (txtObstacleDistance != null)
txtObstacleDistance.Text = sector.obstacleDistanceInMeters.ToString();
}
}
else
{
await txtPosition.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
txtIsSensorBeingUsed.Text = value.Value.isSensorBeingUsed.ToString();
txtPosition.Text = value.Value.position.ToString();
for (int i = 0, count = value.Value.detectionSectors.Count; i < count; i++)
{
ObstacleDetectionSector sector = value.Value.detectionSectors[i];
TextBox txtWarning = this.FindControl<TextBox>("txtWarning" + i.ToString());
if (txtWarning != null)
txtWarning.Text = sector.warningLevel.ToString();
TextBox txtObstacleDistance = this.FindControl<TextBox>("txtObstacleDistance" + i.ToString());
if (txtObstacleDistance != null)
txtObstacleDistance.Text = sector.obstacleDistanceInMeters.ToString();
}
});
}
}
}
I would like to decrease the height of the edit box associated with a CMFCToolbarEditBoxButton on a CMFCToolBar.
I have been able to change successfully the height of a combo box associated with CMFCToolbarComboBoxButton by changing the font size after the ReplaceButton step in OnToolbarReset.
This approach doesn't work for CMFCToolbarEditBoxButton. (see CGuiEditBox section in the following code.)
The first code is from OnToolbarSet. I assign a font to the edit box. I even tried making the size of the font very small. It had no effect.
Next I tried to change the rectangle associated with the edit box in the toolbar's AdjustLocations method. That approach also was unsuccessful.
LRESULT CMainFrame::OnToolbarReset(WPARAM wp, LPARAM)
{
UINT uiToolBarId = (UINT)wp;
switch (uiToolBarId)
{
case IDR_TOPTOOLBAR_REG:
{
CZoomCombo ZoomCombo;
ZoomCombo.EnableWindow(TRUE);
ZoomCombo.SetDropDownHeight(300);
ZoomCombo.SetCenterVert(TRUE);
ZoomCombo.AddItem(_T(".5x"));
ZoomCombo.AddItem(_T("1x"));
ZoomCombo.AddItem(_T("2x"));
ZoomCombo.AddItem(_T("3x"));
ZoomCombo.AddItem(_T("4x"));
ZoomCombo.AddItem(_T("5x"));
ZoomCombo.AddItem(_T("6x"));
ZoomCombo.AddItem(_T("7x"));
ZoomCombo.AddItem(_T("8x"));
ZoomCombo.AddItem(_T("9x"));
ZoomCombo.AddItem(_T("10x"));
m_wndTopToolBar.ReplaceButton(IDC_ZOOMCOMBO_DUMMY, ZoomCombo);
do
{
CMFCToolBarButton* pButton = NULL;
int nZoomIndex = m_wndTopToolBar.CommandToIndex(IDC_ZOOMCOMBO);
if (nZoomIndex == -1)
break;
pButton = m_wndTopToolBar.GetButton(nZoomIndex);
if (pButton == NULL)
break;
ASSERT(pButton->IsKindOf(RUNTIME_CLASS(CMFCToolBarComboBoxButton)));
CMFCToolBarComboBoxButton* pComboButton = (CMFCToolBarComboBoxButton*)pButton;
CComboBox* pCbo = pComboButton->GetComboBox();
CEdit* pEdit = pComboButton->GetEditCtrl();
if (pCbo == NULL || pEdit == NULL)
break;
pCbo->SetFont(&m_ToolBarBtnFont);
pEdit->SetFont(&m_ToolBarBtnFont);
m_wndTopToolBar.InvalidateButton(nZoomIndex);
int nSel = GetZoomComboIndex(m_Zoom);
if (nSel >= 0)
{
pCbo->SetCurSel(nSel);
CString str;
pCbo->GetWindowText(str);
pComboButton->SetText(str);
}
} while (false);
//
CGuiEditBox GuiEditBox;
GuiEditBox.EnableWindow(TRUE);
m_wndTopToolBar.ReplaceButton(IDC_GUIEDITBOX_DUMMY, GuiEditBox);
do
{
CMFCToolBarButton* pButton = NULL;
int nGuiIndex = m_wndTopToolBar.CommandToIndex(IDC_GUIEDITBOX);
if (nGuiIndex == -1)
break;
pButton = m_wndTopToolBar.GetButton(nGuiIndex);
if (pButton == NULL)
break;
ASSERT(pButton->IsKindOf(RUNTIME_CLASS(CMFCToolBarEditBoxButton)));
CMFCToolBarEditBoxButton* pEditBoxButton = (CMFCToolBarEditBoxButton*)pButton;
CEdit* pEdit = pEditBoxButton->GetEditBox();
if (pEdit == NULL)
break;
pEdit->SetFont(&m_ToolBarBtnFont); // height of this font is much less than default
//CFont* pFont = pEdit->GetFont();
//LOGFONT lf;
//pFont->GetLogFont(&lf); // confirmed that font has been changed
m_wndTopToolBar.InvalidateButton(nGuiIndex);
} while (false);
}
break;
}
return 0;
}
//
void CMFCToolBarEx::AdjustLocations()
{
CMFCToolBar::AdjustLocations();
if (GetSafeHwnd())
{
CMFCToolBarButton* pButton = NULL;
int nGuiIndex = CommandToIndex(IDC_GUIEDITBOX);
if (nGuiIndex != -1)
{
pButton = GetButton(nGuiIndex);
if (pButton)
{
ASSERT(pButton->IsKindOf(RUNTIME_CLASS(CMFCToolBarEditBoxButton)));
CMFCToolBarEditBoxButton* pEditBoxButton = (CMFCToolBarEditBoxButton*)pButton;
CEdit* pEdit = pEditBoxButton->GetEditBox();
if (pEdit != NULL)
{
CRect rPos;
pEdit->GetRect(&rPos);
rPos.DeflateRect(0, 4);
pEdit->SetRect(rPos);
}
}
}
}
}
I investigated the source code for afxtoolbareditboxbutton.cpp and saw that the height of the edit box is being set in the OnMove method by the following line
int cy = GetGlobalData()->GetTextHeight();
I changed that line in a subclass of CMFCToolBarEditBoxButton to resolve my problem.
I have defined a variable called fighter in my program. I have an if keyPressed function which selects a specific image based on key. The problem I am having is that that images are swapping but overlapping the original image. I also attempted to add an else statement with the fighter in the original position but still same response.
var fighter;
var stance;
var kick;
var jab;
var cross;
var mx; //Use to constrain fighter to center of circle
var my;
function preload(){
stance = loadImage("img/stance.svg");
kick = loadImage("img/kick.svg");
jab = loadImage("img/jab.svg");
cross = loadImage("img/cross.svg");
};
function setup(){
createCanvas(1280,720);
};
function draw(){
background(0, 246, 255);
fill("red");
ellipse(width/2,height/2,500,500);
mx = constrain(mouseX,width/2-250,width/2+250);
my = constrain(mouseY,height/2-250,height/2+250);
fighter = image(stance,mx,my);
if(keyIsPressed){
if((key == "a" || key == "A")){
fighter = "";
fighter = image(jab,mx,my);
}
else if ((key == "w" || key == "W")) {
fighter = image(cross,mx,my);
}
else if ((key == "s" || key == "S")) {
fighter = image(kick,mx,my);
}
};
};
You're setting fighter equal to the value returned by the image() function for some reason. This doesn't make a ton of sense.
Instead, I think you want to set fighter equal to one of the images, and then pass fighter into the image() function. Something like this:
fighter = stance;
if(keyIsPressed){
if((key == "a" || key == "A")){
fighter = jab;
}
else if ((key == "w" || key == "W")) {
fighter = cross
}
else if ((key == "s" || key == "S")) {
fighter = kick;
}
}
image(fighter, mx,my);