• What do you need help with? Version 5
    5,752 replies, posted
Hi, im having trouble with some collision detection code This works very good, but the problem is that is in the loop that updates all entities: [code] for(std::list<CEntity*>::iterator i = Entities.begin(); i != Entities.end();) { for (std::list<CEntity*>::iterator q = i; q != Entities.end(); q++) { if((*i)->name == (*q)->name) { } else { sf::FloatRect rect1 = (*i)->sprite.getGlobalBounds(); sf::FloatRect rect2 = (*q)->sprite.getGlobalBounds(); if(rect1.intersects(rect2)) { Collisions.push_back(CEntityCol((*i),(*q))); } } } if(!(*i)->IsAlive()) { delete (*i); i = Entities.erase(i); } else { (*i)->Update(dt); ++i; } } [/code] But if i put them in another loop, it just freezes: [CODE] for(std::list<CEntity*>::iterator i = Entities.begin(); i != Entities.end();) { for (std::list<CEntity*>::iterator q = i; q != Entities.end(); q++) { if((*i)->name == (*q)->name) { } else { sf::FloatRect rect1 = (*i)->sprite.getGlobalBounds(); sf::FloatRect rect2 = (*q)->sprite.getGlobalBounds(); if(rect1.intersects(rect2)) { Collisions.push_back(CEntityCol((*i),(*q))); } } } } for(std::list<CEntity*>::iterator i = Entities.begin(); i != Entities.end();) { if(!(*i)->IsAlive()) { delete (*i); i = Entities.erase(i); } else { (*i)->Update(dt); ++i; } }[/CODE] I cant see where is the problem :S Thanks you.
I thought to try a different means of settings up a love2D game but ran into a problem early on... it just gives me a blank screen 0.0 I tried using this code... [code] main.lua ---------- require "entities" function love.draw() end function love.draw() end function love.update(dt) end entities.lua -------- player = { x=15, y=25 } function player:draw() love.graphics.setColor(0, 255, 0) love.graphics.rectangle("fill", self.x, self.y, 80, 80) end villager = { x=35, y=42 } function villager:draw() love.graphics.setColor(255, 0, 0) love.graphics.rectangle("fill", self.x, self.y, 80, 80) end entities = { player, villager } function love.draw() for _, ents in ipairs(entities) do ents:draw() love.graphics.print(v.. "JIMMIES") end end [/code]
[QUOTE=Naelstrom;37527583]Probably have some memory errors (ex: bool Foo; if(Foo) {DoStuff();}), if you're on Linux you can easily spot them using Valgrind. Not sure of any alternatives for Windows though.[/QUOTE] Disappeared with DrMemory. Fucking hell, this is annoying. In other news, Window's default DLLs have a shit-ton of uninitialized read errors according to DrMemory. [editline]3rd September 2012[/editline] And unaddressable access errors.
I'm looking for a Regex string which will be able to detect "xxx in Chat" from the Steam Community webpage for groups (i.e: [url]http://steamcommunity.com/groups/TF2Mappers[/url] ) I've got the page contents (page source) as a String in C#, with the relevent page souce: [html] <span class="hiliteTextBlue"><a class="steamLink" href="steam://friends/joinchat/103582791429594873">13 in Chat</a></span> [/html] The numbers after joinchat/ will change per group, and I don't want to catch anything from the rest of the source like "Join us in chat" or "Come chat with us". I'm looking to extract just the string of how many, eg; "13". I'd like to do this in C#, can anyone get me a regex expression for this?
Events in C# are such a pain in this case. The WebClient class allows you to call DownloadData(), and the only way to check what the progress is is to hook an event handler to DownloadProgressChanged. Suppose I have a WebClient declared in the class that I use for grabbing multiple pages for multiple reasons. What if I just want to update a ProgressBar while staying within the method that needs the downloaded data? I just want to be able to do [code]while (true) { if (client.IsBusy) //update progress bar with something called "client.Progress" Thread.Sleep(10); } //do stuff with the data[/code] But I can't because then it'd be reliant on repeatedly checking if the progress changed, and won't continue executing (//do stuff with the data) until somewhere between 0 and 10 milliseconds. But! [code]DownloadProgressChangedEventHandler one = new DownloadProgressChangedEventHandler(oneSpecificPurpose); DownloadProgressChangedEventHandler two = new DownloadProgressChangedEventHandler(aSecondSpecificPurpose); client.DownloadProgressChanged += one; client.DownloadData("http://www.google.com"); client.DownloadProgressChanged -= one; //do stuff with google.com client.DownloadProgressChanged += two; client.DownloadData("http://store.steampowered.com/"); client.DownloadProgressChanged -= two; //do stuff with store.steampowered.com[/code] Am I able to subscribe and unsubscribe an event at will like that? Should I make use of await and async keywords? Sorry for wall of text.
[QUOTE=JakeAM;37530300]I thought to try a different means of settings up a love2D game but ran into a problem early on... it just gives me a blank screen 0.0 I tried using this code... [code] main.lua ---------- require "entities" function love.draw() end function love.draw() end function love.update(dt) end entities.lua -------- player = { x=15, y=25 } function player:draw() love.graphics.setColor(0, 255, 0) love.graphics.rectangle("fill", self.x, self.y, 80, 80) end villager = { x=35, y=42 } function villager:draw() love.graphics.setColor(255, 0, 0) love.graphics.rectangle("fill", self.x, self.y, 80, 80) end entities = { player, villager } function love.draw() for _, ents in ipairs(entities) do ents:draw() love.graphics.print(v.. "JIMMIES") end end [/code][/QUOTE] You are overwriting love.draw with a function that does nothing after you require entities.lua
[QUOTE=ncls_hv;37528941]Hi, im having trouble with some collision detection code This works very good, but the problem is that is in the loop that updates all entities: [code]for(std::list<CEntity*>::iterator i = Entities.begin(); i != Entities.end();) { for (std::list<CEntity*>::iterator q = i; q != Entities.end(); q++) { if((*i)->name == (*q)->name) { } else { sf::FloatRect rect1 = (*i)->sprite.getGlobalBounds(); sf::FloatRect rect2 = (*q)->sprite.getGlobalBounds(); if(rect1.intersects(rect2)) { Collisions.push_back(CEntityCol((*i),(*q))); } } } if(!(*i)->IsAlive()) { delete (*i); i = Entities.erase(i); } else { (*i)->Update(dt); ++i; } }[/code] But if i put them in another loop, it just freezes: [CODE]for(std::list<CEntity*>::iterator i = Entities.begin(); i != Entities.end();) { for (std::list<CEntity*>::iterator q = i; q != Entities.end(); q++) { if((*i)->name == (*q)->name) { } else { sf::FloatRect rect1 = (*i)->sprite.getGlobalBounds(); sf::FloatRect rect2 = (*q)->sprite.getGlobalBounds(); if(rect1.intersects(rect2)) { Collisions.push_back(CEntityCol((*i),(*q))); } } } } for(std::list<CEntity*>::iterator i = Entities.begin(); i != Entities.end();) { if(!(*i)->IsAlive()) { delete (*i); i = Entities.erase(i); } else { (*i)->Update(dt); ++i; } }[/CODE] I cant see where is the problem :S Thanks you.[/QUOTE] You never increase i in the first loop? Try to debug. [editline]4th September 2012[/editline] [QUOTE=Hypershadsy;37533593]Events in C# are such a pain in this case.[/QUOTE] Thats why I do not like them. Can you not just have several threads? And even if you made up your own event you still have put them if a loop and then fire the event when you want it to as far as I know. But I have never worked with events in C# really so I have no idea.
This code is supposed to spawn 5 enemies and wait one second between each spawn. However, it spawns all five enemies at once without waiting. [code] double[] curWave = waves[id]; double enemyType = curWave[0]; double enemyCount = curWave[1]; double nextEnemyTimer = 0; double maxEnemyTimer = curWave[2]; Console.WriteLine("Enemy type: " + enemyType + ", Enemy Count: " + enemyCount + ", Enemy Interval: " + maxEnemyTimer); for (int i = 0; i < enemyCount; i++) { while (nextEnemyTimer < maxEnemyTimer) { nextEnemyTimer += gTime.ElapsedGameTime.Milliseconds; //Console.WriteLine(nextEnemyTimer); } Console.WriteLine("Spawning enemy #" + (i + 1)); EnemyManager.SpawnEnemy(EnemyManager.GetEnemyTypeByID(Convert.ToInt16(enemyType))); nextEnemyTimer = 0; }[/code] I have done testing, with which i discovered that the nextEnemyTimer is being increased every time but very rapidly. Is this a problem with the for loop being ultimately fast?
[QUOTE=Funley;37537018]This code is supposed to spawn 5 enemies and wait one second between each spawn. However, it spawns all five enemies at once without waiting. [code]-code-[/code] I have done testing, with which i discovered that the nextEnemyTimer is being increased every time but very rapidly. Is this a problem with the for loop being ultimately fast?[/QUOTE] I could be wrong, but my best guess is that the while loop is not, like you seem to be assuming, run once for every iteration of the game loop; it's just run again and again until it exits. So instead of nextEnemyTimer being bumped up each frame, it's being bumped up as fast as possible until it exceeds maxEnemyTimer.
[QUOTE=AyeGill;37537190]I could be wrong, but my best guess is that the while loop is not, like you seem to be assuming, run once for every iteration of the game loop; it's just run again and again until it exits. So instead of nextEnemyTimer being bumped up each frame, it's being bumped up as fast as possible until it exceeds maxEnemyTimer.[/QUOTE] I managed to change the wave spawning into the Update(). Its horrid but it works.
[QUOTE=Hypershadsy;37533593]Events in C# are such a pain in this case. The WebClient class allows you to call DownloadData(), and the only way to check what the progress is is to hook an event handler to DownloadProgressChanged. Suppose I have a WebClient declared in the class that I use for grabbing multiple pages for multiple reasons. What if I just want to update a ProgressBar while staying within the method that needs the downloaded data? I just want to be able to do [code]while (true) { if (client.IsBusy) //update progress bar with something called "client.Progress" Thread.Sleep(10); } //do stuff with the data[/code] But I can't because then it'd be reliant on repeatedly checking if the progress changed, and won't continue executing (//do stuff with the data) until somewhere between 0 and 10 milliseconds.[/QUOTE] The problem with doing it that way is that as long as your UI thread is sitting in that loop, it's not responding to [i]other[/i] events, like the user clicking on things. Your program will seem to lock up until the download is finished; Windows may even bring up the "program is not responding" dialog. If this download is in response to a user action like a button or menu click, this code is running [i]in[/i] an event handler, and it needs to return quickly so that the program can resume its main event loop. Note that your "do stuff with the data" part doesn't belong there, because this method needs to return as soon as it [i]starts[/i] the download. Use the DownloadDataCompleted event to do stuff when the download finishes. And you need to use DownloadDataAsync(), not DownloadData() or you won't get these events at all. (DownloadData() will wait for the whole download to finish before continuting to the next line of your code, which means the event loop doesn't run so your program will lock up while it downloads.) [QUOTE=Hypershadsy;37533593]But! [code]DownloadProgressChangedEventHandler one = new DownloadProgressChangedEventHandler(oneSpecificPurpose); DownloadProgressChangedEventHandler two = new DownloadProgressChangedEventHandler(aSecondSpecificPurpose); client.DownloadProgressChanged += one; client.DownloadData("http://www.google.com"); client.DownloadProgressChanged -= one; //do stuff with google.com client.DownloadProgressChanged += two; client.DownloadData("http://store.steampowered.com/"); client.DownloadProgressChanged -= two; //do stuff with store.steampowered.com[/code] Am I able to subscribe and unsubscribe an event at will like that? Should I make use of await and async keywords?[/QUOTE] You can do that with events, but you're mixing several different paradigms here. There are three basic ways to use WebClient: [b]On the UI thread with explicit events[/b] This is what I was describing above. From a method running on the UI thread (e.g. a button click handler), you start your download in the background using DownloadDataAsync(), and set up events to fire when interesting things happen. You handle download progress (and completion) in event handlers, the same way you handle button clicks. [b]On the UI thread with tasks[/b] This is where the "await" and "async" keywords come in. Instead of setting up an event handler for the DownloadDataCompleted event comes in, you can declare your method "async", call "await client.DownloadDataTaskAsync(uri)", and then just write the "do stuff" code on the next line. The "await" is basically a "return" that makes an event handler out of the remainder of the method, so it'll start the download, then leave your method and go back to the main event loop, and resume running your method when the download finishes. But you still have to use an event handler for DownloadProgressChanged, because "await" is for calls that can suspend and later resume [i]once[/i], not things that can occur many times. [b]On a dedicated thread[/b] You create a subclass of WebClient that overrides methods like OnDownloadProgressChanged() and OnDownloadDataCompleted(), and call DownloadData() on a dedicated thread ([i]not[/i] the UI thread). It calls your overridden methods at the appropriate times on the same thread. The dedicated thread approach may seem simpler, but it's actually more complex. Since you probably want your download to start in response to an event on the UI thread (such as the user clicking a button), you'd have to use something like BeginInvoke() to ask some other thread to start the download. And since that other thread can't safely change things in the UI, it has to use BeginInvoke() to ask the UI thread to do so. BTW, if you're using Visual Studio, you can probably add your WebClient as a component in the designer and hook up the event handlers through the designer, the same way you do for button click handlers. [b]Disclaimer:[/b] I'm not a .NET developer and I've never used WebClient, only read its documentation just now.
if (playerPos.X + player.Width < wallPos.X || playerPos.X > wallPos.X + wall.Width || playerPos.Y + player.Height < wallPos.Y || playerPos.Y > wallPos.Y + wall.Height) Anyone know how to get bounding box collision to work like a platformer? What I mean is instead of having your character move through the box it should collide with it and not be able to move through it. I can't seem to figure of what I need to add to make that happen. Also the code is in C#.
To answer your question: [QUOTE=Naelstrom;37495357]Use aabb collision tests and solving. How I do it is like this: [cpp]for (unsigned int i=0;i<Players.size();i++) { //If your tiles are on a fixed grid, you should only loop through a chunk of the tiles that are near the current player. for (unsigned int o=0;o<NearbyTiles.size();o++) { Player* CP = Players[i]; Tile* CT = NearbyTiles[o]; //The functions assume the boxes' positions represent the center of the box. glm::vec4 PBox = glm::vec4(CP->GetPos().x,CP->GetPos().y,CP->GetScale().x,CP->GetScale().y); glm::vec4 TBox = glm::vec4(CT->GetPos().x,CT->GetPos().y,CT->GetScale().x,CT->GetScale().y); if (Intersects(PBox,TBox)) { glm::vec2 Move = MinimumTranslation(PBox,TBox); //If we can only move the player we use this; otherwise you should halve Move and add/subtract it to both boxes' positions. CP->SetPos(CP->GetPos()+Move); } } }[/cpp][/QUOTE] EDIT: Links: [url]https://github.com/naelstrof/Astrostruct/blob/master/src/Physics.hpp[/url] [url]https://github.com/naelstrof/Astrostruct/blob/master/src/Physics.cpp[/url]
[QUOTE=Lord Ned;37526821]This gets dicey when you start adding/deleting entities, or changing anything's unique ID.[/QUOTE] Since the entities are connected via pointers in editor/game I just update all the IDs before I save the map to file. Tnx.
So I added mob simulation to my server and realized my time steps were really fucked up. The logic all uses delta time, but I think I have implemented it wrong because things tend to not work correctly when the FPS (ticks per second) is a few hundred thousand on the server. My current implementation is like this. I calculate the actual tick per second at the beginning of the frame, and calculate a delta_time (speed factor) using the following: deltaTime = targetFramerate/FPS. And then I call world.update(deltaTime); However when the framerate is well over 300,000, the logic seems to perform too slow, and doesn't perform at it's intended 60 FPS. So basically I'm looking for information on how to create a perfect time step that will run all my logic the same way regardless of actual framerate. I think I am calculating the deltaTime wrong, but logically if the FPS is 120, and the target FPS is 60, then 60/120 is 0.5, so everything should move at half the speed.. however when the actual FPS is too high, things move too slow.. Any information would be helpful.
Whic data types are deltaTime, targetFramerate and FPS?
They are all doubles
Could try implementing a small delay into the loop to prevent the FPS from getting that high? Kind of a band-aid, unless the problem is rounding.
I think your right because when the FPS is in the millions I think there isn't enough precision in a double to handle it. I put a small delay in to put it at around 1000 FPS and it seems to work better.
That's weird... doubles should be able to store down to ~5*10^-324 (0.000...(323 zeroes)...5)
How can I find the highest API level call that my program makes?
[QUOTE=Electroholic;37552222]I think your right because when the FPS is in the millions I think there isn't enough precision in a double to handle it. I put a small delay in to put it at around 1000 FPS and it seems to work better.[/QUOTE] Have you looked at [url]http://gafferongames.com/game-physics/fix-your-timestep/[/url] ? It really fixed up my timestep issues.
[QUOTE=Naelstrom;37554966]Have you looked at [url]http://gafferongames.com/game-physics/fix-your-timestep/[/url] ? It really fixed up my timestep issues.[/QUOTE] Where abouts in your code have you implemented this? I would like to see how you have done it
Heh, I actually haven't implemented it in my engine yet because I have no need of strict time stepping. (yet) You can't use it as reference in this case, sorry. :x [editline]5th September 2012[/editline] [QUOTE=AlienCat;37548356]To answer your question: -quote with dead links-[/QUOTE] Sorry those links are dead, use these: [url]https://github.com/naelstrof/Astrostruct/blob/master/src/Physics.hpp[/url] [url]https://github.com/naelstrof/Astrostruct/blob/master/src/Physics.cpp[/url] Blame Jookia for the inconvenience.
Okay okay, so I'm working pretty much my first LÖVE project, a simple N-Body gravity thingomajig, and everything works fine except for collision/merging. I've got a table 'universe' which holds all the objects, and each object has a position, velocity and force vector as well as mass. They are created with zero velocity at the mouse position with a random mass and gravitate towards each other. This works exactly as intended, and is achieved with: [CODE]function love.update(dt) for k, obj1 in ipairs(universe) do for i = k + 1, #universe do obj2 = universe[i] local diff = (obj1.pos - obj2.pos) local force = ((mul*obj1.mass*obj2.mass)/(diff:len2() + 1))*diff:normalized() obj1.force = obj1.force - force obj2.force = obj2.force + force end integrate(obj1, dt) end end -- and to update velocities and positions -- function integrate(object, dt) object.vel = object.vel + (object.force/object.mass)*dt object.pos = object.pos + object.vel*dt object.force = vector() end -- (Yes it is very very basic integration, once I get my problem sorted I'll experiment with timesteps and RK4 etc) -- [/CODE] Now I want to have colliding bodies merge. I acheived this in the nested loop in the update function by checking if the pair of objects were within the sum of their radii and simply [URL="http://upload.wikimedia.org/math/4/7/9/47903de82eff8d167f68ba055f6dbacf.png"]setting the first object's velocity to the weighted average of the two object's velocities[/URL], then removing the second object. I ignored position changes as for now, a little visual hop isn't that big of a deal. The problem is when the objects collided, the final velocity was infinitesimally tiny regardless of the velocities and mass at collision. Here's the update function with collision/merging stuff: [CODE]function love.update(dt) for k, obj1 in ipairs(universe) do for i = k + 1, #universe do obj2 = universe[i] if obj2 ~= nil then local diff = (obj1.pos - obj2.pos) if diff:len() <= (obj1.mass^0.5)+(obj2.mass^0.5) + 10 then obj1.vel = ((obj1.vel*obj1.mass) + (obj2.vel*obj2.mass))/(obj1.mass + obj2.mass) obj1.mass = obj1.mass + obj2.mass table.remove(universe, i) else local force = ((mul*obj1.mass*obj2.mass)/(diff:len2() + 1))*diff:normalized() obj1.force = obj1.force - force obj2.force = obj2.force + force end end end integrate(obj1, dt) end end[/CODE] Can anyone see anything wrong with how I'm handling this? I've broken down the final velocity equation to see what is causing it become so tiny and it happens after dividing by the sum of the masses. I added some debug print messages which trigger when a collision occurs that describe the two bodies' velocities and masses at the time of collision and the final body's velocity and mass afterwards so you guys can see the problem: [CODE]Collision (105.61888028595,81.159771167097) and (-12.109966179358,-9.3055529588752) Of masses 97 and 846 Collision result (-1.9289389221059e-015,-9.6446946105295e-016) 943[/CODE] I'm very very new to Lua and LÖVE so here's hoping its something I'm just not seeing that someone can point out.
[QUOTE=Em See;37561379]Okay okay, so I'm working pretty much my first LÖVE project, a simple N-Body gravity thingomajig, and everything works fine except for collision/merging. I've got a table 'universe' which holds all the objects, and each object has a position, velocity and force vector as well as mass. They are created with zero velocity at the mouse position with a random mass and gravitate towards each other. This works exactly as intended, and is achieved with: [CODE]function love.update(dt) for k, obj1 in ipairs(universe) do for i = k + 1, #universe do obj2 = universe[i] local diff = (obj1.pos - obj2.pos) local force = ((mul*obj1.mass*obj2.mass)/(diff:len2() + 1))*diff:normalized() obj1.force = obj1.force - force obj2.force = obj2.force + force end integrate(obj1, dt) end end -- and to update velocities and positions -- function integrate(object, dt) object.vel = object.vel + (object.force/object.mass)*dt object.pos = object.pos + object.vel*dt object.force = vector() end -- (Yes it is very very basic integration, once I get my problem sorted I'll experiment with timesteps and RK4 etc) -- [/CODE] Now I want to have colliding bodies merge. I acheived this in the nested loop in the update function by checking if the pair of objects were within the sum of their radii and simply [URL="http://upload.wikimedia.org/math/4/7/9/47903de82eff8d167f68ba055f6dbacf.png"]setting the first object's velocity to the weighted average of the two object's velocities[/URL], then removing the second object. I ignored position changes as for now, a little visual hop isn't that big of a deal. The problem is when the objects collided, the final velocity was infinitesimally tiny regardless of the velocities and mass at collision. Here's the update function with collision/merging stuff: [CODE]function love.update(dt) for k, obj1 in ipairs(universe) do for i = k + 1, #universe do obj2 = universe[i] if obj2 ~= nil then local diff = (obj1.pos - obj2.pos) if diff:len() <= (obj1.mass^0.5)+(obj2.mass^0.5) + 10 then obj1.vel = ((obj1.vel*obj1.mass) + (obj2.vel*obj2.mass))/(obj1.mass + obj2.mass) obj1.mass = obj1.mass + obj2.mass table.remove(universe, i) else local force = ((mul*obj1.mass*obj2.mass)/(diff:len2() + 1))*diff:normalized() obj1.force = obj1.force - force obj2.force = obj2.force + force end end end integrate(obj1, dt) end end[/CODE] Can anyone see anything wrong with how I'm handling this? I've broken down the final velocity equation to see what is causing it become so tiny and it happens after dividing by the sum of the masses. I added some debug print messages which trigger when a collision occurs that describe the two bodies' velocities and masses at the time of collision and the final body's velocity and mass afterwards so you guys can see the problem: [CODE]Collision (105.61888028595,81.159771167097) and (-12.109966179358,-9.3055529588752) Of masses 97 and 846 Collision result (-1.9289389221059e-015,-9.6446946105295e-016) 943[/CODE] I'm very very new to Lua and LÖVE so here's hoping its something I'm just not seeing that someone can point out.[/QUOTE] Yup, I think I figured it out. The thing is, your two objects have equal but opposite momenta. So your simulation is doing the right thing. Another thing, you're modifying the table while iterating it. This will probably cause you to skip the element that was at index (i+1) if you remove an element.
Yes, table.remove shifts elements down to fill the gap. Reversing your iteration will prevent that. [editline]6th September 2012[/editline] Reverse the for loop, not the for in loop.
Yep, turned out to be just that - starting from rest, two objects would pretty much cancel one another out. I added a random velocity to each spawned object and now I see the behavior I was hoping for, many thanks Puska. [QUOTE=MakeR;37561503]Yes, table.remove shifts elements down to fill the gap. Reversing your iteration will prevent that. [editline]6th September 2012[/editline] Reverse the for loop, not the for in loop.[/QUOTE] As in iterate backwards from the count of items down to the outer loop's item + 1 ?
[QUOTE=Em See;37561633]As in iterate backwards from the count of items down to the outer loop's item + 1 ?[/QUOTE] Yes, that way the values in the table that are shifted after table.remove are the ones that you have already iterated over.
Quick question about shadow mapping, after you have drawn from the lights PoV theres bound to be an area from the users PoV that is out of bounds from the lights PoV. What do you do with that area? IE: [IMG]http://takinginitiative.files.wordpress.com/2011/05/shadowmappingfinal.jpg?w=750[/IMG] Do you make it all light or all shadows?
Sorry, you need to Log In to post a reply to this thread.