📄 ballinthehoop.cpp.in
字号:
/**************************************************************************\ * * This file is part of a set of example programs for the Coin library. * Copyright (C) 2000-2003 by Systems in Motion. All rights reserved. * * <URL:http://www.coin3d.org> * * This sourcecode can be redistributed and/or modified under the * terms of the GNU General Public License version 2 as published by * the Free Software Foundation. See the file COPYING at the root * directory of the distribution for more details. * * As a special exception, all sourcecode of the demo examples can be * used for any purpose for licensees of the Coin Professional * Edition License, without the restrictions of the GNU GPL. See our * web pages for information about how to acquire a Professional Edition * License. * * Systems in Motion, <URL:http://www.sim.no>, <mailto:support@sim.no> *\**************************************************************************/#define HAVE_SOCOMPONENT_SETFULLSCREEN#include <Inventor/@Gui@/So@Gui@RenderArea.h>#include <Inventor/nodes/SoSeparator.h>#include <Inventor/nodes/SoSwitch.h>#include <Inventor/nodes/SoMaterial.h>#include <Inventor/nodes/SoDirectionalLight.h>#include <Inventor/nodes/SoPointLight.h>#include <Inventor/nodes/SoSpotLight.h>#include <Inventor/nodes/SoSphere.h>#include <Inventor/nodes/SoCube.h>#include <Inventor/nodes/SoPerspectiveCamera.h>#include <Inventor/nodes/SoTranslation.h>#include <Inventor/nodes/SoRotationXYZ.h>#include <Inventor/nodes/SoVertexProperty.h>#include <Inventor/nodes/SoMaterialBinding.h>#include <Inventor/nodes/SoIndexedFaceSet.h>#include <Inventor/nodes/SoFont.h>#include <Inventor/nodes/SoText3.h>#include <Inventor/nodes/SoText2.h>#include <Inventor/nodes/SoEventCallback.h>#include <Inventor/fields/SoMFColor.h>#include <Inventor/fields/SoSFString.h>#include <Inventor/events/SoKeyboardEvent.h>#include <Inventor/events/SoMouseButtonEvent.h>#include <Inventor/sensors/SoTimerSensor.h>#include <Inventor/sensors/SoAlarmSensor.h>#include <Inventor/@Gui@/So@Gui@.h>#include <math.h>SoSwitch * sw;SoMaterial * ballmaterial;SoMFColor * fargefelt1, * fargefelt2;SoTranslation * balltrans;So@Gui@RenderArea * viewer;SbVec3f startspeed;SbVec2s pos;int elasticityvalue = 1;int colorindex = 0;int score = 0;int live = 3;int scorefactor = 0;SbString tmpstring;SoSFString * scorestring;SoSFString * livesstring;float powervalue = 0.5;SoTimerSensor * powersensor;SoTimerSensor * shootsensor;SoAlarmSensor * goalsensor;SoMaterial * elastics[30];SoMaterial * strength[30];SoMaterial * walls[5];SoIndexedFaceSet * fs;int indices[25] = { 0, 1, 2, 3, SO_END_FACE_INDEX, 0, 3, 4, 5, SO_END_FACE_INDEX, 3, 2, 6, 4, SO_END_FACE_INDEX, 1, 2, 6, 7, SO_END_FACE_INDEX, 5, 7, 6, 4, SO_END_FACE_INDEX};voidstart_ball(){ scorefactor = 0; balltrans->translation.setValue(10,10,0); elasticityvalue = rand() % 29 + 1; int i; for(i = 0; i < 30; i++){ if(i < elasticityvalue) elastics[i]->transparency.setValue(0.0); else elastics[i]->transparency.setValue(0.8); } powervalue = 0.5; for(i = 0; i < 30; i++) strength[i]->transparency.setValue(0.8); fargefelt1 = &elastics[elasticityvalue-1]->diffuseColor; ballmaterial->diffuseColor.connectFrom(fargefelt1); fargefelt2 = &elastics[elasticityvalue-1]->specularColor; ballmaterial->specularColor.connectFrom(fargefelt2); if (shootsensor->isScheduled()) shootsensor->unschedule();}voidshoot_ball(){ SbVec2s winsize = viewer->getViewportRegion().getWindowSize(); float velhor; float velver; float velfor; int halfwinsize = winsize[0]/2; if(pos[0] > halfwinsize) velhor = ((float)(pos[0]-halfwinsize))/((float)halfwinsize)*powervalue*2; else velhor = -((float)(halfwinsize-pos[0]))/((float)halfwinsize)*powervalue*2; halfwinsize = winsize[1]/2; if(pos[1] > halfwinsize) velver = ((float)(pos[1]-halfwinsize))/((float)halfwinsize)*powervalue*2; else velver = -((float)(halfwinsize-pos[1]))/((float)halfwinsize)*powervalue*2; velfor = -(sqrt(powervalue*2*powervalue*2-velhor*velhor)+ sqrt(powervalue*2*powervalue*2-velver*velver))/2; startspeed[0] = velhor*4/3; startspeed[1] = velver; startspeed[2] = velfor; shootsensor->schedule();}voidstart_game(){ score = 0; tmpstring = SbString(score); scorestring->setValue(tmpstring.getString()); live = 3; tmpstring = SbString(live); livesstring->setValue(tmpstring.getString()); start_ball(); sw->whichChild = 1;}voidend_ball(){ shootsensor->unschedule(); start_ball();}voidevent_cb(void *userdata, SoEventCallback *node){ const SoEvent *event = node->getEvent(); if(SO_MOUSE_PRESS_EVENT(event, BUTTON1)){ pos = event->getPosition(); powersensor->schedule(); } else if(SO_MOUSE_RELEASE_EVENT(event, BUTTON1)){ powersensor->unschedule(); shoot_ball(); } else if (SO_KEY_PRESS_EVENT(event, S)) { start_game(); } else if (SO_KEY_PRESS_EVENT(event, X)) { exit(1); }}voidpower_cb(void *userdata, SoSensor *){ powervalue += 0.2; if(powervalue > 30) powervalue = 30; for(int i = 0; i < 30; i++){ if(i < powervalue) strength[i]->transparency.setValue(0.0); else strength[i]->transparency.setValue(0.8); }}voidgoal(){ shootsensor->unschedule(); sw->whichChild = 2; goalsensor->setTimeFromNow(3); goalsensor->schedule();}voidshoot_cb(void *userdata, SoSensor *){ SbVec3f tmp= balltrans->translation.getValue(); tmp[0]+=startspeed[0]*0.04; tmp[1]+=startspeed[1]*0.04; tmp[2]+=startspeed[2]*0.04; if(tmp[0] < 1){ tmp[0] = 1; startspeed[0] = -startspeed[0]*elasticityvalue/31; startspeed[1] = startspeed[1]*25/31; startspeed[2] = startspeed[2]*25/31; colorindex = (colorindex+1)%5; fs->materialIndex.set1Value(1, colorindex); scorefactor++; } if(tmp[0] > 19){ tmp[0] = 19; startspeed[0] = -startspeed[0]*elasticityvalue/31; startspeed[1] = startspeed[1]*25/31; startspeed[2] = startspeed[2]*25/31; colorindex = (colorindex+1)%5; fs->materialIndex.set1Value(3, colorindex); scorefactor++; } if(tmp[1] < 1){ tmp[1] = 1; startspeed[1] = -startspeed[1]*elasticityvalue/31; startspeed[0] = startspeed[0]*25/31; startspeed[2] = startspeed[2]*25/31; colorindex = (colorindex+1)%5; fs->materialIndex.set1Value(0, colorindex); scorefactor++; } if(tmp[1] > 19){ tmp[1] = 19; startspeed[1] = -startspeed[1]*elasticityvalue/31; startspeed[0] = startspeed[0]*25/31; startspeed[2] = startspeed[2]*25/31; colorindex = (colorindex+1)%5; fs->materialIndex.set1Value(4, colorindex); scorefactor++; } if(tmp[2] > -1){ tmp[2] = -1; startspeed[2] = -startspeed[2]*elasticityvalue/31; startspeed[1] = startspeed[1]*25/31; startspeed[0] = startspeed[0]*25/31; scorefactor++; } if(tmp[2] < -19){ if(tmp[1] < 16.5 && tmp[1] > 13.5 && tmp[0] > 3.5 && tmp[0] < 6.5){ score += (int) (scorefactor*(elasticityvalue*powervalue)/20*3); tmpstring = SbString(score); scorestring->setValue(tmpstring.getString()); goal(); return; } if(tmp[1] < 11.5 && tmp[1] > 8.5 && tmp[0] > 8.5 && tmp[0] < 11.5){ score += (int) (scorefactor*(elasticityvalue*powervalue)/20*2); tmpstring = SbString(score); scorestring->setValue(tmpstring.getString()); goal(); return; } if(tmp[1] < 6.5 && tmp[1] > 3.5 && tmp[0] > 13.5 && tmp[0] < 16.5){ score += (int) (scorefactor*(elasticityvalue*powervalue)/20); tmpstring = SbString(score); scorestring->setValue(tmpstring.getString()); goal(); return; } scorefactor++; tmp[2] = -19; startspeed[2] = -startspeed[2]*elasticityvalue/31; startspeed[1] = startspeed[1]*25/31; startspeed[0] = startspeed[0]*25/31; } balltrans->translation.setValue(tmp[0],tmp[1],tmp[2]); if(startspeed.length() < 0.15 || sqrt(startspeed[0]*startspeed[0] + startspeed[2]*startspeed[2]) < 0.1){ live--; tmpstring = SbString(live); livesstring->setValue(tmpstring.getString()); if(live < 0) start_game(); else end_ball(); } startspeed[1]-=9.1*0.04;}voidgoal_cb(void *userdata, SoSensor *){ sw->whichChild = 1; start_ball();}intmain(int argc, char **argv){ @WIDGET@ window = So@Gui@::init(argv[0]); if (window == NULL) exit(1); SoSeparator * root = new SoSeparator; SoTranslation * tr = new SoTranslation; tr->translation.setValue(2,18,-18); root->addChild(tr); SoPointLight * l = new SoPointLight; l->intensity.setValue(0.5); root->addChild(l); tr = new SoTranslation; tr->translation.setValue(16,0,0); root->addChild(tr); l = new SoPointLight; root->addChild(l); tr = new SoTranslation; tr->translation.setValue(-1,-18,18); root->addChild(tr); sw = new SoSwitch; root->ref();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -