Advertisement
Guest User

Dice.m

a guest
May 26th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  Dice.m
  3. //  DiceRoller
  4. //
  5. //  Created by Jacques Questiaux on 2012/04/17.
  6. //  Copyright (c) 2012 j.questiaux@gmail.com. All rights reserved.
  7. //
  8.  
  9. #import "Dice.h"
  10. #import "d4.h"
  11. #import "d6.h"
  12. #import "d8.h"
  13. #import "d10.h"
  14. #import "d12.h"
  15. #import "d20.h"
  16.  
  17. @interface Dice () {
  18.    
  19.     GLuint _vertexArray;
  20.     GLuint _vertexBuffer;
  21.     GLuint _indexBuffer;
  22.     GLuint _colorBuffer;
  23.     GLuint _normalBuffer;
  24.    
  25.     GLfloat *vertices;
  26.     GLubyte *indices;
  27.     GLfloat *normals;
  28.     GLfloat *colors;
  29.    
  30.     GLubyte numVertices;
  31.     GLubyte numIndices;
  32.     GLubyte numNormals;
  33.     GLubyte numColors;
  34.    
  35.     NSMutableArray *surfaceNormals;
  36.    
  37.     bool k;
  38. }
  39.  
  40.  
  41. - (void) rotateXInDegrees: (float) rot;
  42. - (void) rotateYInDegrees: (float) rot;
  43. - (void) makeSurfaceNormals;
  44.  
  45. @end
  46.  
  47.  
  48. @implementation Dice
  49.  
  50. @synthesize preD = _preD;
  51. @synthesize postD = _postD;
  52. @synthesize plusD = _plusD;
  53. @synthesize success = _success;
  54.  
  55. @synthesize up = _up;
  56. @synthesize front = _front;
  57. @synthesize right = _right;
  58. @synthesize position =_position;
  59.  
  60. @synthesize direction = _direction;
  61.  
  62. @synthesize modelViewMatrix = _modelViewMatrix;
  63.  
  64. - (id) initWithParams: (NSUInteger)preD: (NSUInteger)postD: (NSUInteger)plusD: (NSUInteger)success{
  65.    
  66.     if(self = [super init]){
  67.        
  68.         self.preD = preD;
  69.         self.postD = postD;
  70.         self.plusD = plusD;
  71.         self.success = success;
  72.        
  73.         size = 75;
  74.         friction = 0.95f;
  75.        
  76.         surfaceNormals = [[NSMutableArray alloc] init];
  77.        
  78.         return self;
  79.     }
  80.    
  81.     return nil;
  82. }
  83.  
  84. - (void) Context: (EAGLContext *) c Effect: (GLKBaseEffect *) e {
  85.    
  86.     NSLog(@"before context\n");
  87.    
  88.     self.right = GLKVector3Make(1, 0, 0);
  89.     self.up = GLKVector3Make(0, 1, 0);
  90.     self.front = GLKVector3Make(0, 0, 1);
  91.    
  92.     context = c;
  93.     effect = e;
  94.    
  95.     k = true;
  96.    
  97.     NSLog(@"after context\n");
  98.    
  99.     [self setup];
  100. }
  101.  
  102. - (void) setup{
  103.    
  104.     NSLog(@"before setup\n");
  105.    
  106.     //too lazy to swap to a switch statement
  107.     if(self.postD == 4){
  108.         vertices = d4vertices;
  109.         numVertices = sizeof(d4vertices);
  110.        
  111.         indices = d4indices;
  112.         numIndices = sizeof(d4indices);
  113.        
  114.         normals = d4normals;
  115.         numNormals = sizeof(d4normals);
  116.        
  117.         colors = d4colors;
  118.         numColors = sizeof(d4colors);
  119.        
  120.     }
  121.     else if(self.postD == 6){
  122.         vertices = d6vertices;
  123.         numVertices = sizeof(d6vertices);
  124.        
  125.         indices = d6indices;
  126.         numIndices = sizeof(d6indices);
  127.        
  128.         normals = d6normals;
  129.         numNormals = sizeof(d6normals);
  130.        
  131.         colors = d6colors;
  132.         numColors = sizeof(d6colors);
  133.        
  134.     }
  135.     else if(self.postD == 8){
  136.         vertices = d8vertices;
  137.         numVertices = sizeof(d8vertices);
  138.        
  139.         indices = d8indices;
  140.         numIndices = sizeof(d8indices);
  141.        
  142.         normals = d8normals;
  143.         numNormals = sizeof(d8normals);
  144.        
  145.         colors = d8colors;
  146.         numColors = sizeof(d8colors);
  147.        
  148.     }
  149.     else if(self.postD == 10){
  150.         vertices = d10vertices;
  151.         numVertices = sizeof(d10vertices);
  152.        
  153.         indices = d10indices;
  154.         numIndices = sizeof(d10indices);
  155.        
  156.         normals = d10normals;
  157.         numNormals = sizeof(d10normals);
  158.        
  159.         colors = d10colors;
  160.         numColors = sizeof(d10colors);
  161.        
  162.     }
  163.     else if(self.postD == 12){
  164.         vertices = d12vertices;
  165.         numVertices = sizeof(d12vertices);
  166.        
  167.         indices = d12indices;
  168.         numIndices = sizeof(d12indices);
  169.        
  170.         normals = d12normals;
  171.         numNormals = sizeof(d12normals);
  172.        
  173.         colors = d12colors;
  174.         numColors = sizeof(d12colors);
  175.        
  176.     }
  177.     else {
  178.         vertices = d20vertices;
  179.         numVertices = sizeof(d20vertices);
  180.        
  181.         indices = d20indices;
  182.         numIndices = sizeof(d20indices);
  183.        
  184.         normals = d20normals;
  185.         numNormals = sizeof(d20normals);
  186.        
  187.         colors = d20colors;
  188.         numColors = sizeof(d20colors);
  189.  
  190.     }
  191.    
  192.     NSLog(@"after postD\n");
  193.    
  194.     glGenVertexArraysOES(1, &_vertexArray);
  195.     glBindVertexArrayOES(_vertexArray);
  196.    
  197.     glGenBuffers(1, &_indexBuffer);
  198.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
  199.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices, indices, GL_STATIC_DRAW);
  200.    
  201.     glGenBuffers(1, &_vertexBuffer);
  202.     glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
  203.     glBufferData(GL_ARRAY_BUFFER, numVertices, vertices, GL_STATIC_DRAW);
  204.    
  205.     glEnableVertexAttribArray(GLKVertexAttribPosition);        
  206.     glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, 0);
  207.    
  208.     glGenBuffers(1, &_colorBuffer);
  209.     glBindBuffer(GL_ARRAY_BUFFER, _colorBuffer);
  210.     glBufferData(GL_ARRAY_BUFFER, numColors, colors, GL_STATIC_DRAW);
  211.    
  212.     glEnableVertexAttribArray(GLKVertexAttribColor);
  213.     glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, 0, 0);
  214.    
  215.     glGenBuffers(1, &_normalBuffer);
  216.     glBindBuffer(GL_ARRAY_BUFFER, _normalBuffer);
  217.     glBufferData(GL_ARRAY_BUFFER, numNormals, normals, GL_STATIC_DRAW);
  218.    
  219.     glEnableVertexAttribArray(GLKVertexAttribNormal);
  220.     glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 0, 0);
  221.    
  222.     glBindVertexArrayOES(0);
  223.    
  224.     NSLog(@"after setup\n");
  225. }
  226.  
  227. - (void) tearDown {
  228.    
  229.    
  230.     glDeleteBuffers(1, &_vertexBuffer);
  231.     glDeleteBuffers(1, &_indexBuffer);
  232.     glDeleteBuffers(1, &_colorBuffer);
  233.     glDeleteBuffers(1, &_normalBuffer);
  234. }
  235.  
  236. - (void) makeSurfaceNormals {
  237.    
  238.     int numElements = sizeof(normals)/sizeof(GLfloat);
  239.    
  240.     GLKVector3 down = GLKVector3Make(0, 0, -1);
  241.    
  242.     GLKVector3 currentClosestToDown;
  243.    
  244.     float closestDistance = 9999;
  245.    
  246.     float rollAmount = 1;
  247.    
  248.     for(int i = 0; i < numElements; i++){
  249.        
  250.         int index0 = indices[i*3];
  251.         int index1 = indices[i*3+1];
  252.         int index2 = indices[i*3+2];
  253.        
  254.         GLKVector3 vertNormal0 = GLKVector3Make(normals[index0], normals[index0+1], normals[index0+2]);
  255.         GLKVector3 vertNormal1 = GLKVector3Make(normals[index1], normals[index1+1], normals[index1+2]);
  256.         GLKVector3 vertNormal2 = GLKVector3Make(normals[index2], normals[index2+1], normals[index2+2]);
  257.        
  258.         GLKVector3 surfNormal = GLKMatrix4MultiplyVector3(mvm, GLKVector3Add(GLKVector3Add(vertNormal0, vertNormal1), vertNormal2));
  259.        
  260.         float distanceToDown = GLKVector3Length(GLKVector3Subtract(surfNormal, down));
  261.        
  262.         if(distanceToDown < closestDistance){
  263.            
  264.             closestDistance = distanceToDown;
  265.             currentClosestToDown = surfNormal;
  266.         }
  267.        
  268.     }
  269.    
  270.     GLKVector3 rollDirection;
  271.    
  272.     //rollAmount = (closestDistance*closestDistance);
  273.    
  274.     damp = logf(rollAmount+1);
  275.    
  276.    
  277.     rollDirection = GLKVector3Subtract(currentClosestToDown, down);
  278.     rollDirection = GLKVector3MultiplyScalar(GLKVector3Normalize(GLKVector3Make(rollDirection.x, rollDirection.y, 0)), 5/rollAmount);
  279.    
  280.     [self applyForceInDirection: GLKVector3Make(rollDirection.x*damp, rollDirection.y*damp, rollDirection.z*damp)];
  281.    
  282. }
  283.  
  284. - (void) update: (float) timeSinceLastUpdate{
  285.    
  286.     tslu = timeSinceLastUpdate;
  287.    
  288.     self.direction = GLKVector3MultiplyScalar(self.direction, friction);
  289.    
  290.     [self checkIfCollidedWithWalls];
  291.    
  292.     [self moveInDirection:self.direction];
  293.    
  294.     mvm = GLKMatrix4Make(self.right.x,      self.right.y,       self.right.z,       0,
  295.                          self.up.x,         self.up.y,          self.up.z,          0,
  296.                          self.front.x,      self.front.y,       self.front.z,       0,
  297.                          self.position.x,   self.position.y,    self.position.z,    1);
  298.    
  299.     [self makeSurfaceNormals];
  300. }
  301.  
  302. - (void) draw {
  303.    
  304.     effect.transform.modelviewMatrix = GLKMatrix4Multiply(self.modelViewMatrix, mvm);
  305.    
  306.    
  307.     if(k){
  308.         NSLog(@"before effect\n");
  309.        
  310.     }
  311.    
  312.     [effect prepareToDraw];
  313.    
  314.    
  315.     if(k){
  316.         NSLog(@"after effect\n");
  317.         k = false;
  318.     }
  319.    
  320.    
  321.     glBindVertexArrayOES(_vertexArray);  
  322.     glDrawElements(GL_TRIANGLES, numIndices/sizeof(GLubyte), GL_UNSIGNED_BYTE, 0);
  323.    
  324. }
  325.  
  326. - (void) rotateXInDegrees:(float)rot{
  327.    
  328.     GLKMatrix3 temp = GLKMatrix3MakeRotation(GLKMathDegreesToRadians(rot), 1, 0, 0);
  329.    
  330.     self.right = GLKMatrix3MultiplyVector3(temp, self.right);
  331.     self.up = GLKMatrix3MultiplyVector3(temp, self.up);
  332.     self.front = GLKMatrix3MultiplyVector3(temp, self.front);
  333.  
  334. }
  335.  
  336. - (void) rotateYInDegrees:(float)rot{
  337.    
  338.     GLKMatrix3 temp = GLKMatrix3MakeRotation(GLKMathDegreesToRadians(rot), 0, 1, 0);
  339.     self.right = GLKMatrix3MultiplyVector3(temp, self.right);
  340.     self.up = GLKMatrix3MultiplyVector3(temp, self.up);
  341.     self.front = GLKMatrix3MultiplyVector3(temp, self.front);
  342.  
  343. }
  344.  
  345. - (void) applyForceInDirection: (GLKVector3) dir {
  346.    
  347.     self.direction = GLKVector3Add(self.direction, dir);
  348. }
  349.  
  350. - (void) moveInDirection: (GLKVector3) dir {
  351.    
  352.     previousPosition = self.position;
  353.    
  354.     self.position = GLKVector3Make(self.position.x + dir.x, self.position.y + dir.y, self.position.z + dir.z);
  355.    
  356.     [self rotateXInDegrees:-dir.y/3.14f];
  357.     [self rotateYInDegrees:dir.x/3.14f];
  358.    
  359. }
  360.  
  361. - (BOOL) collidedWith: (Dice *) dice{
  362.    
  363.     float distance = GLKVector3Distance(self.position, dice.position);
  364.    
  365.     if(distance < size + dice->size){
  366.        
  367.         GLKVector3 newDirection = GLKVector3Normalize(GLKVector3Subtract(self.position, dice.position));
  368.        
  369.         float newSpeed = (GLKVector3Length(self.direction) + GLKVector3Length(dice.direction)) / 2;
  370.        
  371.         self.direction = GLKVector3MultiplyScalar(GLKVector3Make(newDirection.x, newDirection.y, newDirection.z), newSpeed * 0.75f);
  372.        
  373.         dice.direction = GLKVector3MultiplyScalar(GLKVector3Make(-newDirection.x, -newDirection.y, -newDirection.z), newSpeed * 0.75f);
  374.        
  375.         self.position = previousPosition;
  376.        
  377.         return true;
  378.     }
  379.  
  380.    
  381.     return false;
  382. }
  383.  
  384. - (BOOL) checkIfCollidedWithWalls{
  385.    
  386.     BOOL didCollide = false;
  387.    
  388.     if(self.position.x - size < -500){
  389.         self.position = GLKVector3Make(-500 + size, self.position.y, self.position.z);
  390.         self.direction = GLKVector3Make(-self.direction.x, self.direction.y, self.direction.z);
  391.         didCollide = true;
  392.     }
  393.    
  394.     if (self.position.x + size > 500){
  395.         self.position = GLKVector3Make(500 - size, self.position.y, self.position.z);
  396.         self.direction = GLKVector3Make(-self.direction.x, self.direction.y, self.direction.z);
  397.         didCollide = true;
  398.     }
  399.    
  400.     if(self.position.y - size < -625){
  401.         self.position = GLKVector3Make(self.position.x, -625 + size, self.position.z);
  402.         self.direction = GLKVector3Make(self.direction.x, -self.direction.y, self.direction.z);
  403.         didCollide = true;
  404.     }
  405.     if (self.position.y + size > 625){
  406.         self.position = GLKVector3Make(self.position.x, 625 - size, self.position.z);
  407.         self.direction = GLKVector3Make(self.direction.x, -self.direction.y, self.direction.z);
  408.         didCollide = true;
  409.     }
  410.    
  411.    
  412.     return didCollide;
  413. }
  414.  
  415. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement