|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On February 15 2014 09:47 darkness wrote:Show nested quote +On February 15 2014 09:28 WarSame wrote: I was using the term C earlier, but it was causing some confusion when I was asking questions. I guess I'll just stick with it, then.
I'll give that a shot, thank you. C - a plain, procedural language C++ - superset of C, it supports C *and* offers more stuff (not only object-oriented programming) Objective-C - Apple's language, it supports C *and* offers object-oriented programming A bit informal, but you should distinguish all three. You can program C stuff in both C++ and Objective-C, but it's considered low level in general.  Edit: The C Programming Language book
The proper term should be ANSI C if you don't want to cause confusion (since C is quite a large family of programming languages).
And as far as pointers go, you should simply try to write a simple program that makes extensive use of arrays. Here's an example of a dice-throwing program I wrote a while back:
#include <stdio.h> #include <stdlib.h> #include <time.h>
// global variables struct DiceConfiguration { int DiceNumber; int DiceType; };
struct DiceRollResult { int DiceNumber; int Sum; int RollResults[]; };
// prototypes struct DiceConfiguration* GetConfiguration(); struct DiceRollResult* RollTheDice(struct DiceConfiguration *config); void GenerateRandomSeed(void); int RollDice(struct DiceConfiguration *config); void PrintDiceRollResult(struct DiceRollResult *result); void ReleaseMemory(struct DiceConfiguration *config, struct DiceRollResult *result);
int main(void) { struct DiceConfiguration *config; struct DiceRollResult *result; GenerateRandomSeed(); config = GetConfiguration(); while(config != NULL) { result = RollTheDice(config); PrintDiceRollResult(result); ReleaseMemory(config, result); config = GetConfiguration(); } return 0; }
struct DiceConfiguration* GetConfiguration() { int diceNumber = 0; int diceType = 0; printf("Enter the number of dice to roll (0 to quit): "); scanf("%d", &diceNumber); if(diceNumber == 0) { return NULL; } else { printf("Enter dice type (sides): "); scanf("%d", &diceType); if(diceType < 2) { printf("Dice can't have less than 2 sides!\n");
return NULL; } else { struct DiceConfiguration *config; config = malloc(sizeof(struct DiceConfiguration)); config->DiceNumber = diceNumber; config->DiceType = diceType; return config; } } }
struct DiceRollResult* RollTheDice(struct DiceConfiguration *config) { struct DiceRollResult *result; result = malloc(sizeof(struct DiceRollResult) + config->DiceNumber * sizeof(int)); result->DiceNumber = config->DiceNumber; for (int i = 0; i < config->DiceNumber; i += 1) { result->RollResults[i] = RollDice(config); result->Sum += result->RollResults[i]; } return result; }
void GenerateRandomSeed(void) { srand((unsigned int) time(0)); }
int RollDice(struct DiceConfiguration *config) { return rand() % config->DiceType + 1; }
void PrintDiceRollResult(struct DiceRollResult *result) { for (int i = 0; i < result->DiceNumber; i += 1) { printf("%d\n", result->RollResults[i]); } printf("Sum: %d\n", result->Sum); }
void ReleaseMemory(struct DiceConfiguration *config, struct DiceRollResult *result) { if (config != NULL) { free(config); config = NULL; } if (result != NULL) { free(result); result = NULL; } }
This may give you some tips if you're able to break it down and understand it.
|
On February 15 2014 14:32 Crazyeyes wrote:Show nested quote +On February 15 2014 14:17 Nesserev wrote:On February 15 2014 12:46 Crazyeyes wrote:Great! Hopefully you guys can help me out cause this I'm really quite stumped here. Here's the Stack Overflow thread: http://stackoverflow.com/questions/21792708/corona-lua-unable-to-access-table-value-after-collisionAnd I'll copy the text over here in a spoiler for those of you who don't feel like leaving TL + Show Spoiler +I'm trying to get some collision detection working but I can't seem to figure out what the problem is. The error I'm getting is ...\main.lua:178:attempt to concatenate field 'name' (a nil value) What I have is this: I have a box (the "ship") that stays at a fixed X coordinate, but moves up and down. It's meant to go through a "tunnel" made up of two rectangles with a gap between them, and I'm trying to detect a collision between the ship and the walls of the tunnel (ie the rectangles). I get this error when the collision happens. A lot of my code is just modified versions of the official Corona documentation and I'm just not able to figure out what the problem is. Here are the relevant pieces of code: function playGame() -- Display the ship ship = display.newRect(ship); shipLayer:insert(ship); -- Add physics to ship physics.addBody(ship, {density = 3, bounce = 0.3});
...
beginRun(); end
function beginRun() ... spawnTunnel(1100); -- this just calls the createTunnel function at a specific location gameListeners("add"); ... end
function gameListeners(event) if event == "add" then ship.collision = onCollision; ship:addEventListener("collision", ship); -- repeat above two lines for top -- and again for bottom end end
-- Collision handler function onCollision(self,event) if ( event.phase == "began" ) then -- line 178 is right below this line ---------------------------------- print( self.name .. ": collision began with " .. event.other.name ) end
-- Create a "tunnel" using 2 rectangles function createTunnel(center, xLoc) -- Create top and bottom rectangles, both named "box" top = display.newRect(stuff); top.name = "wall"; bottom = display.newRect(stuff); bottom.name = "wall";
-- Add them to the middleLayer group middleLayer:insert(top); middleLayer:insert(bottom);
-- Add physics to the rectangles physics.addBody(top, "static", {bounce = 0.3}); physics.addBody(bottom, "static", {bounce = 0.3}); end
I only get the error message once the two objects should collide, so it seems like the collision is happening, and it is being detected. But for some reason self.name and event.other.name are nil. It's my first time using LUA so I'm probably doing some things wrong, and it is a bit sloppy but as a learning exercise I'm not too concerned about that. Although the main issue here is of course solving the problem, any small criticisms or tips are always welcome. Well, I have no personal experience with LUA, but luckily it's a straightforward and easy-to-read scripting lanugage, and well, this is a problem that can occur in every language. (It also looks a bit like python) Check these things: - To what class does the function 'function onCollision(self,event)' belong to; and is self.name initialized in its constructor? (Check if you initialized a local variable called name instead of self.name) - Does the object that collides even have a member called self.name? Check what the types are of the objects that collide. Also, a nice way to debug, is to output information in every function. There are no classes. All of my code is in main.lua, which includes onCollision. I got the collision code from here, which also explains how it works in pretty simple terms. self should either be the ship or one of the walls (top/bottom), which have the .name property added right after creation. Physics are only enabled on the ship and the walls, so those are the only things that can collide. Furthermore, the physics engine can only have collisions between "dynamic" objects + other objects (so dynamic + dynamic or dynamic + static). Since the ship is my only dynamic physics body, and since I'm pretty confident that there is a collision taking place, we know that the ship is at least one of the bodies colliding and that the ship definitely has its .name property set. ... ... So while writing this post, or more specifically that last line, I thought "...wait, does it?" Mother fucker. I've spent like SIX HOURS trying to debug this shit AND I FUCKING FORGOT TO SET THE .name PROPERTY? Fuuuuuuck that. Thank you so much for responding. I had honestly given up at this point and was relying on someone fixing it for me. Jesus Christ I still can't believe what a stupid mistake that was. It works. After creating the ship, I give it a name now and that fixes it. Goddammit.
I am so very tempted to reference this reaction as your answer to the StackOverflow question .
|
It's always the stupid, simplest of mistakes that get you and make you pull out your hair...
Like, not 2 days ago I was reviewing my code, everything looks good but parts of it don't work and I have no idea why. Several hours later I discovered that I have mistakenly capitalized one word in one place. What it did was make pointer to the master file instead of object in the memory...
|
Is JVM responsible for deallocating memory in this scenario:
Timer theTimer = new Timer(); timer.schedule(....); theTimer = new Timer();
Not exactly copy/paste from my code, but it's very similar. Also, the Timer class is so badly designed not to allow you to stop a task and schedule a new one. There is a work-around, but still...
|
On February 16 2014 07:04 darkness wrote:Is JVM responsible for deallocating memory in this scenario: Timer theTimer = new Timer(); timer.schedule(....); theTimer = new Timer();
Not exactly copy/paste from my code, but it's very similar.  Also, the Timer class is so badly designed not to allow you to stop a task and schedule a new one. There is a work-around, but still...
Responsible is a strong word, it's your responsability to "clean up your mess", but the GC will do it for you eventually, yes.
|
On February 16 2014 07:09 misirlou wrote:Show nested quote +On February 16 2014 07:04 darkness wrote:Is JVM responsible for deallocating memory in this scenario: Timer theTimer = new Timer(); timer.schedule(....); theTimer = new Timer();
Not exactly copy/paste from my code, but it's very similar.  Also, the Timer class is so badly designed not to allow you to stop a task and schedule a new one. There is a work-around, but still... Responsible is a strong word, it's your responsability to "clean up your mess", but the GC will do it for you eventually, yes.
So how do you clean it in Java then? There's no free (C), release (Objective-C) or delete (C++) in Java.
|
You don't. Java's garbage collector will do it for you whenever Java feels like it.
|
This reminds me...
|
|
|
On February 16 2014 09:48 Blisse wrote: You don't. Java's garbage collector will do it for you whenever Java feels like it. You still have to make sure you get rid of all your references to things you don't want any more ^^ doesn't show up much but you do have to be cautious for those times when you need to do a bit of extra work.
|
Anyone know LINQ well?
Say i have a struct like so
public struct Computer { public string RemotePath; public string Database; public string BinaryDir; public string TemplateDir; public string ImageDir; }
And I have a collection of those structs.
Whats the LINQ syntax for displaying it like so
Remote Path: Path1 Path2 Path3
Database: Database1 Database2 Database3
BinaryDir; BinDir1 BinDir2 Bindir3
ect
I'm sure its something extremely simple but I haven't come up with anything remotely close.
|
|
|
On February 16 2014 12:35 Amnesty wrote:Anyone know LINQ well? Say i have a struct like so public struct Computer { public string RemotePath; public string Database; public string BinaryDir; public string TemplateDir; public string ImageDir; }
And I have a collection of those structs. Whats the LINQ syntax for displaying it like so Remote Path: Path1 Path2 Path3 Database: Database1 Database2 Database3 BinaryDir; BinDir1 BinDir2 Bindir3 ect I'm sure its something extremely simple but I haven't come up with anything remotely close.
I am not sure if this is what you want, but this method will print the fields of any collection of objects in this way:
public static void PrintCollection<T>(IEnumerable<T> data) { var fields = typeof(T).GetFields(); var pretty = String.Join("\n\n", fields.Select(f => f.Name + ":\n" + String.Join("\n", data.Select(d => f.GetValue(d).ToString())))); Console.WriteLine(pretty); }
|
On February 16 2014 07:04 darkness wrote:Is JVM responsible for deallocating memory in this scenario: Timer theTimer = new Timer(); timer.schedule(....); theTimer = new Timer();
Not exactly copy/paste from my code, but it's very similar.  Also, the Timer class is so badly designed not to allow you to stop a task and schedule a new one. There is a work-around, but still... Consider com.google.common.base.Stopwatch instead of Timer.
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Stopwatch.html
Also I really think you need to stop stressing about a few K of memory here and there between GC cycles. Don't dive down that hole until you
1) Run into actual performance issues and, 2) Are relatively certain they're due to memory (e.g. you start getting really high gc or full gc frequency, or really high ms per s gc time)
|
something something premature optimization something something
on the subject of timers... does anyone know how you're supposed to use gluPerspective/gluLookAt in a custom OpenGL/SDL app? I'm trying to make something that resembles a game from the ground up in OGL (for fun), and now I'm stuck on the part where you actually move the camera around (something about moving the world opposite to your camera motion vs. moving the camera).
I'm just not sure how you would proceed to change the camera like this in an event loop with like, models and stuff. Do I make a "Camera" class with variables for the current... camera? that listens to input from the keyboard? So my FOV/POV is the camera? @_@
I can figure out the math myself, I'm just not sure of how it'd be implemented in a simple
while (1) HandleEvents(e) GameLoop() GameRender()
I'm also trying to figure out how collision detection is going to work.. Right now my objects listen to the keyboard individually and then modify the model... I guess they should have functions exposing their members, they should all themselves be member variables of another "room" or "world" object that in the "GameLoop()" method goes and makes sure everything is good (and then changes things if they're not)? I feel like this makes sense but I can't find a good/up-to-date tutorial so I seem to be making this up as I go along. Would prefer some standardized way of approaching this.
Looking far into the future too.... I'm pretty sure my mind is going to hurt when I try to figure out how lighting works O_O
|
The most common way to turn the camera (that I've seen) is to apply a rotation matrix in the inverse direction to the entire 'world' (all objects in the scene). If you're using the built-in opengl methods for setting matrices, then you would use the function 'glMultMatrixf(mat4x4)' I believe. This multiplies your current model-view matrix by mat4x4. A slightly simpler way might be glRotatef, which just takes an angle (rotation in degrees) and 3 vector components (the normal vector that you're rotating around).
The basic code might look something like this: (stolen from http://www.opengl.org/archives/resources/faq/technical/viewing.htm) glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50.0, 1.0, 3.0, 7.0); // 50 degrees field-of-view in the y direction, 1.0 x/y aspect ratio, znear (minimum view distance) is 3, zfar is 7. // Don't set znear too close to your camera, your scene will react poorly. glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // eye is @ 0,0,5, global center is 0,0,0, up vector is y-axis glRotatef(x, 0,1,0); // Look right/left x degrees.
Note that glRotatef will compound each time you call it, so calling it 5x in a row (say, if you have the 'd' key pressed for 5 consecutive loop iterations) will rotate right/left by 5x. glLoadIdentity is pretty much a 'reset' function, it sets the modelview matrix to the identity matrix. If you call it, you should reload your gluLookAt matrix.
If you're unsure about rotation Matrices, the wiki page http://en.wikipedia.org/wiki/Rotation_matrix has the 3 simple ones under 'Basic Rotations'.
Also, I've loved these tutorials if you haven't seen them yet. http://www.arcsynthesis.org/gltut/ He teaches opengl with shaders, though, so you'll be directly manipulating perspective and modelview matrices (as opposed to letting opengl handle it.)
|
|
|
|
|
Man, working with Swing is a pain in the ass. How do i remove this magical margin below the title? + Show Spoiler +
|
Isn't that an empty menubar?
|
|
|
|
|
|