• What Do You Need Help With? V6
    7,544 replies, posted
[QUOTE=NixNax123;46629563]This is what I'm trying: [code] @Override public void show() { ... // reset removal list entsToBeRemoved.clear(); // update entities and check for removal for (Entity e : entities) { e.update(dt); if (e.toBeRemoved()) { entsToBeRemoved.add(e); } e.draw(); } // draw player last so bullets appear to come out of player (not from center) player.update(dt); player.draw(); // clear grid grid.clear(); // re-fill grid for (Entity e : entities) { grid.addEntity(e); } // check for collisions for (Entity e : entities) { e.detectCollisions(dt); } // check if player has no health if (player.toBeRemoved()) { player.handleRemoval(); } for (Entity e : entsToBeRemoved) { if (e instanceof EnemyShipEntity) { numEnemies--; } e.handleRemoval(); entities.remove(e); } ... }[/code] Collision is not working, and bullets are just disappearing soon after they leave my player sprite.[/QUOTE] Whats your collision algorithm?
[QUOTE=proboardslol;46630621]Whats your collision algorithm?[/QUOTE] This is how I check for collisions for an Asteroid: [code] @Override public void detectCollisions(float dt) { // get nearest entity nearestEntity = GameScreen.getGrid().getNearest(this); if (intersectsWithPlayer()) { handlePlayerIntersection(); } if (nearestEntity instanceof BulletEntity) { System.out.println("intersection with bullet!"); if (intersectsWithBullet()) { handleBulletIntersection(); collidingBulletVelocity = Vector2f.mul(collidingBulletVelocity, dt); } } if (nearestEntity instanceof AsteroidEntity) { if (intersectsWithAsteroid()) { handleAsteroidIntersection(); } } }[/code] Other entities, the code is pretty similar. My collision detection method itself works fine, I am 100% sure on that (it worked before I implemented the grid) [editline]3rd December 2014[/editline] And this is getNearest: [code] public Entity getNearest(Entity e) { // For comparisons Entity nearest = null; float maxDist = Float.MAX_VALUE; // Retrieve the entities List<Entity> collidables = retrieve(e); // Iterate and find the nearest for (Entity toCheck : collidables) { Vector2f diff = Vector2f.sub(toCheck.getPos(), e.getPos()); float dist = CVector.length(diff); if (dist < maxDist) { nearest = toCheck; maxDist = dist; } } return nearest; } private List<Entity> retrieve(Entity e) { // Retrieve the list of collide-able entities retrieveList.clear(); // Calculate the positions again float topLeftX = Math.max(0, e.getPos().x / gridCellSize); float topLeftY = Math.max(0, e.getPos().y / gridCellSize); float edgeLength = e.getBounds().getRadius().x * 2; float bottomRightX = Math.min(cols - 1, (e.getPos().x + edgeLength - 1) / gridCellSize); float bottomRightY = Math.min(rows - 1, (e.getPos().y + edgeLength - 1) / gridCellSize); for (int x = (int)topLeftX; x <= bottomRightX; x++) { for (int y = (int)topLeftY; y <= bottomRightY; y++) { List<Entity> cell = grid[x][y]; // Add every entity in the cell to the list for (Entity ent : cell) { if (!retrieveList.contains(ent)) { retrieveList.add(ent); } } } } return Collections.unmodifiableList(retrieveList); }[/code] Bullets sometimes disappear after coming out of the player, and collisions don't happen. [editline]3rd December 2014[/editline] Alright, I fixed the bullet disappearing problem by adding this line in retrieve: [code] // entity can't collide with itself retrieveList.remove(e);[/code] But collisions still don't work (bullets still pass through asteroids). [editline]3rd December 2014[/editline] However, sometimes bullets DO collide with asteroids, which is odd.
Will the new version of C# be able to check if a certain index of an array exists? I know that the newer version has the "?" operator which checks if it isn't null & exists altogether, so will it be possible to do this : [CODE]bool[] someArray = new bool[5]; int totallyOutOfRangeIndex = 89; if (someArray[totallyOutOfRangeIndex]?) { // It exists & is actually declared (In the range) } else { // It either doesn't exist or isn't declared (Not in the range) }[/CODE] I hate checking the length of the array everytime, I feel like it's kinda "eh" for readability (If that's even a word).
That doesn't even have sense, because getting an out of bounds index gives you just an overflow (or underflow) exception.
Can you not just catch an IndexOutOfBounds exception?
One possible solution would be [code] public static bool HasIdx<T>(this T[] Arr, int Idx) => Idx >= 0 && Idx < Arr.Length; [/code] As an extension method using a new C# feature. [editline]4th December 2014[/editline] [QUOTE=NixNax123;46631950]Can you not just catch an IndexOutOfBounds exception?[/QUOTE] That is a VERY shitty idea. Exceptions are not just slow, but implementing that would make it unreadable too! [editline].[/editline] Another solution would be [code] public static T Idx<T>(this T[] Arr, int Idx) => Idx >= 0 && Idx < Arr.Length ? Arr[Idx] : default(T); [/code] which would return null in case index doesn't exist. [editline].[/editline] Or it would return the default value for type of array in case the type is not nullable, like an array of structs for example.
[QUOTE=NixNax123;46629563]This is what I'm trying: [code] @Override public void show() { ... // reset removal list entsToBeRemoved.clear(); // update entities and check for removal for (Entity e : entities) { e.update(dt); if (e.toBeRemoved()) { entsToBeRemoved.add(e); } e.draw(); } // draw player last so bullets appear to come out of player (not from center) player.update(dt); player.draw(); // clear grid grid.clear(); // re-fill grid for (Entity e : entities) { grid.addEntity(e); } // check for collisions for (Entity e : entities) { e.detectCollisions(dt); } // check if player has no health if (player.toBeRemoved()) { player.handleRemoval(); } for (Entity e : entsToBeRemoved) { if (e instanceof EnemyShipEntity) { numEnemies--; } e.handleRemoval(); entities.remove(e); } ... }[/code] Collision is not working, and bullets are just disappearing soon after they leave my player sprite.[/QUOTE] Firstly, you clear the grid at the start of the game loop. You are updating many things before clearing. This is wrong. Secondly, you are updating the playing and drawing them, and updating the asteroids then drawing them, sequentially. This means that the actual shown frame isn't the accurate game state. In order, here's what you want to do: [code] clear grid remove entities update player, asteroids, bullets re-fill grid perform collision detection draw player, asteroids, bullets [/code] [editline]3rd December 2014[/editline] [QUOTE=NixNax123;46630716]However, sometimes bullets DO collide with asteroids, which is odd.[/QUOTE] You need to ensure your grid squares are big enough - specifically, at least the size of your biggest entity (a large asteroid) Also, whenever you add a point, you need to add it to the grid square it is in, but also the surrounding 8 squares! Otherwise you can get issues. That's the simpler way - the more complex way consists of checking each corner of an entity separately and adding only to the relevant squares (a max of 4, and more efficient with smaller things).
[QUOTE=Tommyx50;46631966]Firstly, you clear the grid at the start of the game loop. You are updating many things before clearing. This is wrong. Secondly, you are updating the playing and drawing them, and updating the asteroids then drawing them, sequentially. This means that the actual shown frame isn't the accurate game state. In order, here's what you want to do: [code] clear grid remove entities update player, asteroids, bullets re-fill grid perform collision detection draw player, asteroids, bullets [/code] [editline]3rd December 2014[/editline] You need to ensure your grid squares are big enough - specifically, at least the size of your biggest entity (a large asteroid) Also, whenever you add a point, you need to add it to the grid square it is in, but also the surrounding 8 squares! Otherwise you can get issues. That's the simpler way - the more complex way consists of checking each corner of an entity separately and adding only to the relevant squares (a max of 4, and more efficient with smaller things).[/QUOTE] Alright, I've made the code cleaner and more modular: [code] // clear grid grid.clear(); checkAndRemoveEntities(); updateEntitiesAndFillGrid(dt); detectEntityCollisions(dt); drawEntities(); ... } private static void checkAndRemoveEntities() { // reset removal list entsToBeRemoved.clear(); for (Entity e : entities) { if (e.toBeRemoved()) { entsToBeRemoved.add(e); } } for (Entity e : entsToBeRemoved) { e.handleRemoval(); entities.remove(e); } } private static void updateEntitiesAndFillGrid(float dt) { for (Entity e : entities) { e.update(dt); grid.addEntity(e); } } private static void detectEntityCollisions(float dt) { for (Entity e : entities) { e.detectCollisions(dt); } } private static void drawEntities() { for (Entity e : entities) { e.draw(); } }[/code] [code] public void addEntity(Entity e) { // get all the points the entity takes up // do not accept negative values, or values outside of the grid float topLeftX = Math.max(0, e.getPos().x / gridCellSize); float topLeftY = Math.max(0, e.getPos().y / gridCellSize); float edgeLength = e.getBounds().getRadius().x * 2; float bottomRightX = Math.min(cols - 1, (e.getPos().x + edgeLength - 1) / gridCellSize); float bottomRightY = Math.min(rows - 1, (e.getPos().y + edgeLength - 1) / gridCellSize); for (int x = (int) topLeftX; x <= bottomRightX; x++) { for (int y = (int) topLeftY; y <= bottomRightY; y++) { grid[x][y].add(e); } } }[/code] with gridCellSize being the grid edge length over 32. I figured out my problem. When I add an entity to the grid, the algorithm I use can not take negative values. However, entities can negative coordinates and still be displayed by the screen. This is causing them to not be shown by the grid. [editline]3rd December 2014[/editline] How would I fix this? Since my grid is represented as an array, my row and column can not be negative. So I can't assign an entity's position to a cell if the position has a negative coordinate.
Hey guys, I have a Raspberry Pi question. I'm trying to get the PiUi to work and so far I got the Pi to turn into an access point and my phone can connect to it, but when I tried getting PiUi to run on the Pi by running the Python piui_demo.py command, I get "IOError('Port 9999 not free on '0.0.0.0' ',). Any advice?
[QUOTE=NixNax123;46632200]How would I fix this? Since my grid is represented as an array, my row and column can not be negative. So I can't assign an entity's position to a cell if the position has a negative coordinate.[/QUOTE] I think you're very close now. do a coordinate transformation on all your entity positions so that they're all positive (in terms of (x,y) coords, the new coordinates should be in the range (0,0) to (width, height), so I assume if your old coordinates have (0,0) in the centre of the screen, you should just add width/2 to x and height/2 to y), then use these coordinates for the grid square assignment. not totally sure but I think it should then work?
In OpenGL if I'm going to have a lot of objects that share a mesh but have different properties (position, rotation, scale, texture, color) would it be advisable to create a single Vertex Buffer for the mesh and call a draw for every object that uses it and pass the properties to the shader via uniforms?
So, in my Operating Systems class, we're building a file-system simulator in straight C. In my "delete file" method, I am getting a "CRT detected that the application wrote to memory after end of heap buffer" (Visual Studio 2013). Pressing "Retry" immediately follows to a break in the following segment. [code] if (file->inode->doubleIndirect != NULL) { for (int j = 0; j < INODE_DOUBLE_INDIRECT_BLOCKS; j++) { if (file->inode->doubleIndirect->indirectBlocks[j] != NULL) { for (int i = 0; i < INODE_DIRECT_BLOCKS; i++) { lastBlock = addOrConsolidateFreeBlock(lastBlock, file->inode->doubleIndirect->indirectBlocks[j]->dataBlocks[i]); file->inode->doubleIndirect->indirectBlocks[j]->dataBlocks[i] = -1; } free(file->inode->doubleIndirect->indirectBlocks[j]); file->inode->doubleIndirect->indirectBlocks[j] = NULL; } } free(file->inode->doubleIndirect); // This is where the break is file->inode->doubleIndirect = NULL; }[/code] And I have no idea why that is raising the exception. Especially since I do a similar thing for file->inode->indirect, without cause. Does anyone have any ideas as to what may be triggering this exception? For completeness' sake, here are the relevant structures. [code]// Defines #define INODE_DIRECT_BLOCKS 13 #define INODE_INDIRECT_BLOCKS 16 #define INODE_DOUBLE_INDIRECT_BLOCKS 16 #define INODE_TRIPLE_INDIRECT_BLOCKS 16 // Prototypes struct SIM_INODE_INDIRECT_STRUCT; struct SIM_INODE_DOUBLE_INDIRECT_STRUCT; struct SIM_INODE_TRIPLE_INDIRECT_STRUCT; // Structs typedef struct SIM_INODE_STRUCT { int dataBlocks[INODE_DIRECT_BLOCKS]; struct SIM_INODE_INDIRECT_STRUCT* indirect; struct SIM_INODE_DOUBLE_INDIRECT_STRUCT* doubleIndirect; struct SIM_INODE_TRIPLE_INDIRECT_STRUCT* tripleIndirect; } SIM_INODE; typedef struct SIM_INODE_INDIRECT_STRUCT { int dataBlocks[INODE_INDIRECT_BLOCKS]; } SIM_INODE_INDIRECT; typedef struct SIM_INODE_DOUBLE_INDIRECT_STRUCT { SIM_INODE_INDIRECT* indirectBlocks[INODE_DOUBLE_INDIRECT_BLOCKS]; } SIM_INODE_DOUBLE_INDIRECT; typedef struct SIM_INODE_TRIPLE_INDIRECT_STRUCT { SIM_INODE_DOUBLE_INDIRECT* doubleIndirectBlocks[INODE_TRIPLE_INDIRECT_BLOCKS]; } SIM_INODE_TRIPLE_INDIRECT; typedef struct SIM_FILE_STRUCT { char* fileName; int fileSize; SIM_INODE* inode; } SIM_FILE; [/code] And "file" in the first snippet is of type SIM_FILE*. Also, I am entirely aware of my bad C habit of putting the pointer asterisk with the type, and not the variable. I know why it's bad practice, but I do it anyways. Please don't bring it up. :v:
I know this isn't exactly a programming question, but does anyone know of an online game where you program an AI for ships, and two AI's fight it out (yours vs another opponents). The ships were triangles and they broke apart to create smaller ships. It had a prebuilt API to make creating an AI for your ships easier. I tried finding it with no luck and I am hoping someone here knows about it.
So I made an 8x8 BMP file that looks like this and octal dumped it. After I cut out the addresses, headers, etc, I was left with 128 bytes, which I dumped into decimal. I paired each byte together with the byte it's next to and put them on an 8x8 setup. The image was this: [img]http://i.imgur.com/PmzXcm9.png[/img] and the output was this: [code] (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 65280) (255, 0) (255, 65280) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) [/code] I know the bitmap files are on an x,y axis starting like a normal cartesian plane (left-right, bottom-top), so I see why the first pixel is in the last line of data. evidently, These numbers are a added together. a 65535 means (255,255) (8bit max + 8bit max = 16bit max), and the reds (65280) are also added together ( (1111)2, or (255)10 + 0 ). Since the RGB for black is (0,0,0), but the output for blacks is (255,0), I'm guessing it also adds in T(ransparency) too, hence for black: Where "+" denotes bitwise concatenation, not addition: ((255t + 0r), (0g + 0b)) == (255,0) and for red: (255t + 0?), (255r + 0?) = (255, 65280) Where ? is either green or blue. I know that transparency is the first part of the first set of data from extrapolating what I saw before, and I know that red is the 1st part of the second set of data because the RGB for red is (255,0,0), and the only way that 65280 could = red is if it is: (1111)2 + (0000)2 where the first data point is red. Now to figure out which point of data is blue and green, I switch out red with blue: [img]http://i.imgur.com/bicVVk7.png[/img] and get the OD, [code] (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (65535, 0) (255, 0) (65535, 0) (255, 0) (65535, 65535) (255, 0) (65535, 65535) (255, 0)[/code] Since Blue is then (65535, 0), that means that the combination is: TBGR, or RGBT backwards. Now, what I want to know is how I can take these two numbers and figure out 4 independent RGBT values for them. Since I'm going to assume that T is 255 So instead of dealing with totally useless octal, decimal, or hexadecimal numbers, I'm going to just get the xxd of it and get it in raw binary, which reveals that the binary of the image with orange [img]http://i.imgur.com/FGGbPbi.png[/img] is: [code] 11111111 00000000 10000011 11111111 255t 0b 131g 255r [/code] Thus, we can get 4 bytes per pixel to read its RGB data of course, I could've looked all this up, It's more fun to figure it out myself :D
I made a bitmap decoder / encoder in Java, shit is pretty fun! Never try jpegs though. [I]Never.[/I] [editline]4th December 2014[/editline] [QUOTE=Turnips5;46633092]I think you're very close now. do a coordinate transformation on all your entity positions so that they're all positive (in terms of (x,y) coords, the new coordinates should be in the range (0,0) to (width, height), so I assume if your old coordinates have (0,0) in the centre of the screen, you should just add width/2 to x and height/2 to y), then use these coordinates for the grid square assignment. not totally sure but I think it should then work?[/QUOTE] Odd, that's still not working. Anyone else? Also, I must figure out a way to stop the game timer while the game is paused. A small bug, but a noticeable one.
So If I opened a bmp file for input as binary in C, and got down to the aforementioned image data, could i just do something like,, 1.) get the width and height from the BMP header 2.) make a struct with 4 members, TBGR 3.) make a 2d array of these structs, with the same width and height as the bmp file 4.) use some iteration to group together 5x5 elements of the array, and average their TBGR values, putting the result into a new array, 1/5th of the size of the original array, with averaged pixel values?
[QUOTE=proboardslol;46644580]So If I opened a bmp file for input as binary in C, and got down to the aforementioned image data, could i just do something like,, 1.) get the width and height from the BMP header 2.) make a struct with 4 members, TBGR 3.) make a 2d array of these structs, with the same width and height as the bmp file 4.) use some iteration to group together 5x5 elements of the array, and average their TBGR values, putting the result into a new array, 1/5th of the size of the original array, with averaged pixel values?[/QUOTE] sure, yes
[QUOTE=NixNax123;46641141]I made a bitmap decoder / encoder in Java, shit is pretty fun! Never try jpegs though. [I]Never.[/I] [editline]4th December 2014[/editline] Odd, that's still not working. Anyone else? Also, I must figure out a way to stop the game timer while the game is paused. A small bug, but a noticeable one.[/QUOTE] You could perform the transformation afterwards. If you maximum "grid" negative position is -8, then just add 8 to every transformed grid position before you actually place it into the 2d array. Game timers - most games will have a dt value (for time consistency across framerates), and a timeMultiplier value. Then, for anything which is consistent regardless of game speed (like ui), you use the dt directly, and for anything which is affected by pauses or slow motion, you use (dt * timeMultiplier). Then, if you set timeMultiplier to 0, you can get a pause, 0.5 for slowmotion, 1 for no change, and so on. If's usually better if you have a real_dt and a game_dt (instead of using the multiplier directly) so you can get away with performing that multiplication only once at the start of a frame.
I have a project going on, but it's going very slow without a programmer. If somebody is interested in making a 2D game, pm me. Here's some sprites I've made: [url]http://imgur.com/a/93ckV[/url] [url]http://imgur.com/a/Jz44e[/url] [url]http://imgur.com/a/EitZC[/url] I am also very much willing to learn programming, right now I've had a go at Game Maker, but I'm not really that good at it, if someone wants to collaborate to make something, please pm me. They're not all for the same project, obviously.
[QUOTE=Gogi;46645088]I have a project going on, but it's going very slow without a programmer. If somebody is interested in making a 2D game, pm me. Here's some sprites I've made: [url]http://imgur.com/a/93ckV[/url] [url]http://imgur.com/a/Jz44e[/url] [url]http://imgur.com/a/EitZC[/url] I am also very much willing to learn programming, right now I've had a go at Game Maker, but I'm not really that good at it, if someone wants to collaborate to make something, please pm me. They're not all for the same project, obviously.[/QUOTE] I sure hope I could be [img]http://i.imgur.com/1sF40MV.png[/img] putin these in my game
lol never mind. Scanner next vs scanner nextln
Does anyone know why this part of my code causes an infinite loop? I am trying to have it so if the user enters a letter instead of a number, it is going to prompt them that their entry is invalid and have them enter it again, but instead it just infinite loops. I tried it with the last line being the cout error message and also tried it as a throw with a catch later on. The infinite loop only happens if they input an invalid entry (letters), but still works ok if the input is fine. [code] while (cout << "Please enter the student ID for student #" << x + 1 << ": " && !(cin >> people[x].studentid)) { cin.ignore(1000, '\n'); cin.clear(); cout << "Please enter a number, not a letter." << endl; } [/code] This code is also nested inside a for loop. EDIT: Shit, just got it figured out with some different code. Here it is in case anyone happens to have a similar issue. My apologies for not trying more things before posting. [code] bool valid = false; while (!valid) { valid = true; cout << "Please enter the student ID for student # " << x + 1 << ": "; cin >> people[x].studentid; if (cin.fail()) { cin.clear(); cin.ignore(); cout << "Please enter only numbers for the student ID." << endl; valid = false; } } [/code] EDIT2: Okay it worked for the first thing I was trying, but now using it on another part of the code it doesn't want to work. It will error check fine, but it wont let my loop continue. [t]http://i.imgur.com/J8wMaqX.png[/t] Anyone have any ideas about this?
[QUOTE=Gmod4ever;46640145]So, in my Operating Systems class, we're building a file-system simulator in straight C. ... [/QUOTE] Resolved this issue. It wasn't related to that line - the issue was there was a case where previous elements (EG elements from before the error) were being removed from the hash-map improperly. Resolved that issue and these errors disappeared.
Does anyone know how I might implement trig functions which work in degrees in C++, WITHOUT using Pi? I'm trying to write a simulation of [url=http://en.wikipedia.org/wiki/Buffon%27s_needle]Buffon's Needle experiment[/url] (which calculates Pi using probability), and one variable needs to be a random number between 0 and pi/2 (or 0 and 90 if it's in degrees), but I'm working on the assumption I don't have a value for Pi, hence I'm trying to calculate it. So the condition I have in my code is: [CODE] x = fmod(rand(),(t/2)); th = fmod(rand(),(M_PI/2)); if(x<(l/2)*sin(th)) { //do stuff } [/CODE] I tried generating th between 0 and 1 and getting rid of the sin term entirely, but it messes with the distribution and doesn't give me the values I expect.
That's really odd, I just read about that yesterday when I was curious how pi was calculated, for no real reason except curiosity. [quote]random number between 0 and 2pi (or 0 and 90 if it's in degrees), [/quote] 2pi is 360 degrees.
[QUOTE=CoolCorky;46647162]Does anyone know how I might implement trig functions which work in degrees in C++, WITHOUT using Pi? I'm trying to write a simulation of [url=http://en.wikipedia.org/wiki/Buffon%27s_needle]Buffon's Needle experiment[/url] (which calculates Pi using probability), and one variable needs to be a random number between 0 and 2pi (or 0 and 90 if it's in degrees), but I'm working on the assumption I don't have a value for Pi, hence I'm trying to calculate it. So the condition I have in my code is: [CODE] x = fmod(rand(),(t/2)); th = fmod(rand(),(M_PI/2)); if(x<(l/2)*sin(th)) { //do stuff } [/CODE] I tried generating th between 0 and 1 and getting rid of the sin term entirely, but it messes with the distribution and doesn't give me the values I expect.[/QUOTE] You could just approximate pi by summing some terms of the series 4-4/3+4/5-4/7+4/9-..., which converges to pi. But I guess that loses the point of the experiment. Then again I guess the point of the experiment is that you start from a geometric point of view, where sin is already defined.
Degrees = approximately (radians * 57.296), if that helps. Then use a function to approximate sine using the Taylor series.
[QUOTE=NixNax123;46647873]Degrees = approximately (radians * 57.296), if that helps. Then use a function to approximate sine using the Taylor series.[/QUOTE] You can still derive Pi from that. (1 / 57.296) * 180 = 3.141...
I'm pretty sure he wants to convert degrees to radians without using Pi, so he can use the sine function.
I'm trying to come up with a smooth, stable method for sliding down steep slopes. I'm working in Unity3d using a Rigidbody for my character. If you know how source engine handles sliding, that's basically what I'm trying to achieve (minus the air accel/surfing, this would require a bit more effort afaik). [url]https://dl.dropboxusercontent.com/u/96502721/S/sliding.mp4[/url] Simple methods for handling sliding in Unity result in unsmooth/unrealistic motions. The player instantly slides at full speed as soon as he lands on a steep slope, the player lands and stops dead in motion, or if the player tries to walk onto a steep slope it'll constantly push him backwards and the screen will shake back and forth rapidly. I've been trying to figure this out all day, the smoothest thing I've come up with is simply adding force against the player in the direction of the slant the player is standing on. It looks something like this: [code] float GroundAngle = Vector3.Angle (HitNormal, Vector3.up); if(GroundAngle >= _SlideAngle) { Vector3 SlopeForce = new Vector3(HitNormal.x, -0.75f, HitNormal.z); float SlideSpeed = (GroundAngle * _SlopeModifier) / 2; fSlideRate = Mathf.Lerp (fSlideRate, SlideSpeed, Time.time); SlopeForce *= fSlideRate; _RigidBody.AddForce (SlopeForce, ForceMode.VelocityChange); } [/code] My lerp isn't working properly and there's some flaws which I'm sure an experienced programmer could point out easily. If somebody here could help polish my code or maybe demonstrate a better way to handle sliding that'd be awesome. edit: Came back to it after clearing my head a little bit, cleaned up the code.. [code] bSliding = true; vSlideVelocity = new Vector3(HitNormal.x, -HitNormal.y, HitNormal.z); fSlideRate += _SlideSpeed * Time.deltaTime / 2; vSlideVelocity *= fSlideRate; fSlideRate += _SlideSpeed * Time.deltaTime / 2; _RigidBody.AddForce(vSlideVelocity, ForceMode.VelocityChange); [/code] This is simpler, cleaner, more effective I think. I need to clamp the slide velocity so it doesn't get too crazy. The only problem I have now is if a player holds W trying to climb up a slope the player's movement force works against the slide force. Meanwhile fSlideRate is gradually increasing until it finally has enough velocity to force the player to slide. But because fSlideRate has been building up, the player slides down really fast in an instant. I can't think of a way around this yet. Any ideas?
Sorry, you need to Log In to post a reply to this thread.