Beispiel #1
0
bool ElasticItem::checkCollision(MoveableItem *item)
{
	if (opacity() == 0)
		return false;

	QPoint ballCenter = item->centerPoint();
	QPoint mappedCenter = mapFromItem(item->parentItem(), ballCenter).toPoint();
	ballCenter = mappedCenter;

	if (topSensor(item->width()/2).contains(ballCenter, true)) {
		item->setSpeedY(-fabs(item->speedY()));
		return true;
	}
	if (bottomSensor(item->width()/2).contains(ballCenter, true)) {
		item->setSpeedY(fabs(item->speedY()));
		return true;
	}
	if (leftSensor(item->width()/2).contains(ballCenter, true)) {
		item->setSpeedX(-fabs(item->speedX()));
		return true;
	}
	if (rightSensor(item->width()/2).contains(ballCenter, true)) {
		item->setSpeedX(fabs(item->speedX()));
		return true;
	}

	if (topLeftSensor(item->width()/2).contains(ballCenter, true)) {
		item->setSpeedX(-fabs(item->speedX()));
		item->setSpeedY(-fabs(item->speedY()));
		return true;
	}
	if (topRightSensor(item->width()/2).contains(ballCenter, true)) {
		item->setSpeedX(fabs(item->speedX()));
		item->setSpeedY(-fabs(item->speedY()));
		return true;
	}

	if (bottomLeftSensor(item->width()/2).contains(ballCenter, true)) {
		item->setSpeedX(-fabs(item->speedX()));
		item->setSpeedY(fabs(item->speedY()));
		return true;
	}
	if (bottomRightSensor(item->width()/2).contains(ballCenter, true)) {
		item->setSpeedX(fabs(item->speedX()));
		item->setSpeedY(fabs(item->speedY()));
		return true;
	}

	return false;
}
int main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                			 // Stop WDT

  ADC10CTL0 = ADC10SHT_3 + ADC10ON + ADC10IE;		 // ADC10ON, interrupt enabled
  ADC10CTL1 = ADC10DIV_7;
  ADC10CTL1 |= ADC10SSEL1|ADC10SSEL0;  				// Select SMCLK

  P1DIR |= BIT0; 									// Set P1.0 to output direction
  P1DIR |= BIT6;

  for (;;){

    leftSensor();
    if (ADC10MEM < 0x180)
      P1OUT &= BIT0;   						// Clear P1.0 LED off
    else
      P1OUT |= BIT0;  						// Set P1.0 LED on

    _delay_cycles(1000);
    rightSensor();
    if (ADC10MEM < 0x180)
    	P1OUT &= BIT6;
    else
    	P1OUT |= BIT6;

    _delay_cycles(1000);
    centerSensor();
    if (ADC10MEM < 0x150)
       	P1OUT &= BIT0;
    else
       	P1OUT |= BIT0;
  }

    return 0;
}