I'm trying to calculate the distance between two coordinates, but I've run into a little problem. It's broken. It finds the station that is the closest to the client, but it outputs that there's 1737km between the two coordinates, even though I put in the same ones...
[php]$shortest_distance = 99999999999;
$closest_station_id = -1;
$i = 0;
foreach($array['resource']['stations']['station'] as $station){
$Radius = 6371;
$s_lat = $station['@attributes']['lat'];
$s_lon = $station['@attributes']['lon'];
$dLat = deg2rad(($c_lat - $s_lat));
$dLon = deg2rad($c_lon - $s_lon);
$lat1 = deg2rad($s_lat);
$lat2 = deg2rad($c_lat);
$a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$d = $Radius * $c;
if($d < $shortest_distance){
$closest_station_id = $i;
$shortest_distance = $d;
}
$i++;
}[/php]
Distance is sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2).
[QUOTE=SiPlus;39655846]Distance is sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2).[/QUOTE]
Seeing how he's using longitudes and latitudes for the coordinates, I'd assume he wants to compare great-circle distances rather than Euclidean distances.
Can someone help me implement a FPS limiter in java?
I currenty have:
[cpp]
public static final int FPS = 60;
public static double DT = 1.0 / FPS;
public static final int DELAY = (int)(DT * 1000.0);
while (playing()) {
Constants.DT = (System.currentTimeMillis() - dtStart) * 0.001;
dtStart = System.currentTimeMillis();
//update and paint the game
Update();
Draw(g);
try {
Thread.sleep(Math.max(0, Math.round(Constants.DELAY - (Constants.DT*1000) )));
}
catch (Exception e) {}
}
[/cpp]
Constants.DT is now 0.016, however if I add anohter call inside update to make the thread sleep for 5 seconds(simulate some computations that take 5ms) then the DT goes up to 21-23, ie it doesnt adjust, it just keeps calling sleep with 16
Don't use Thread.sleep, check if the time since the last frame is over 1/60th of a second, and if it is, then you call Update() and Draw().
[QUOTE=Gulen;39657225]Don't use Thread.sleep, check if the time since the last frame is over 1/60th of a second, and if it is, then you call Update() and Draw().[/QUOTE]
Something like this?
[cpp]
double delta = 0;
double dtStart = System.currentTimeMillis();
while (playing()) {
delta += (System.currentTimeMillis() - dtStart);
dtStart = System.currentTimeMillis();
if(delta >= 16){
//update and paint the game
Update();
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
Draw(g);
// finally, we've completed drawing so clear up the graphics
// and flip the buffer over
g.dispose();
strategy.show();
System.out.println(delta);
delta = 0;
}
}
[/cpp]
I would've used a last_frame variable instead, and just do
[code](System.currentTimeMillis() - last_frame) >= (1/60) [/code]
I think your way would work too, though.
[QUOTE=Gulen;39657456]I would've used a last_frame variable instead, and just do
[code](System.currentTimeMillis() - last_frame) >= (1/60) [/code]
I think your way would work too, though.[/QUOTE]
But by doing it your way, how would you calculate last_frame? as without updating/drawing the frame will never be more than 16ms
last_frame is just the system time when the last frame was drawn. If the frame takes longer time to finish, it simply will take as long as it needs. If it doesn't need the full 1/60th of a second, it'll skip drawing/updating for a couple of ticks or cycles, and then draw/update once its been longer than 1/60th of a second.
Did it al your way,
[cpp]
double last_frame = System.currentTimeMillis();
double dtStart = System.currentTimeMillis();
while (playing()) {
if((System.currentTimeMillis() - last_frame) >= 1000/Constants.FPS){
Constants.DT = (System.currentTimeMillis() - dtStart) * 0.001;
dtStart = System.currentTimeMillis();
System.out.println(Constants.DT);
//update and paint the game
Update();
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
Draw(g);
// finally, we've completed drawing so clear up the graphics
// and flip the buffer over
g.dispose();
strategy.show();
last_frame = System.currentTimeMillis();
}
}[/cpp]
but it outputs:
[quote]0.037
0.019
0.017
0.019
0.021
0.017
0.02
0.026000000000000002
0.022
0.016
0.032
0.032
0.019
0.017
0.016
0.016
0.019
0.017
0.016
0.017
0.017
0.016
0.019
0.026000000000000002
0.017
0.016
0.018000000000000002[/quote]
im getting some random spikes?
[editline]20th February 2013[/editline]
The update and draw methods are empty
[QUOTE=Richy19;39657927]Did it al your way,
[cpp]
double last_frame = System.currentTimeMillis();
double dtStart = System.currentTimeMillis();
while (playing()) {
if((System.currentTimeMillis() - last_frame) >= 1000/Constants.FPS){
Constants.DT = (System.currentTimeMillis() - dtStart) * 0.001;
dtStart = System.currentTimeMillis();
System.out.println(Constants.DT);
//update and paint the game
Update();
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
Draw(g);
// finally, we've completed drawing so clear up the graphics
// and flip the buffer over
g.dispose();
strategy.show();
last_frame = System.currentTimeMillis();
}
}[/cpp]
but it outputs:
im getting some random spikes?
[editline]20th February 2013[/editline]
The update and draw methods are empty[/QUOTE]
What's wrong with that? I see nothing out of the ordinary.
Well, at some points he gets only half the FPS he's expecting. I guess that's why he's wondering about it.
Ah yeah, didn't see that.
Is it perhaps due to the fact that currentTimeMillis doesn't necessarily have the granularity you'd expect? I know some operating systems will only measure time in 10s of milliseconds.
[QUOTE=Chris220;39659091]Ah yeah, didn't see that.
Is it perhaps due to the fact that currentTimeMillis doesn't necessarily have the granularity you'd expect? I know some operating systems will only measure time in 10s of milliseconds.[/QUOTE]
That was the problem, I changed it to:
[cpp]
double last_frame = System.nanoTime() * 0.000001;
double dtStart = System.nanoTime() * 0.000001;
while (playing()) {
final double currMillis = System.nanoTime() * 0.000001;
if ((currMillis - last_frame) >= 1000 / Constants.FPS) {
Constants.DT = (currMillis - dtStart) * 0.001;
dtStart = currMillis;
System.out.println(Constants.DT);
//update and paint the game
update();
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
draw(g);
// finally, we've completed drawing so clear up the graphics
// and flip the buffer over
g.dispose();
strategy.show();
last_frame = currMillis;
} else {
Thread.yield();
}
}
[/cpp]
and added the thread.sleep() call to not run an empty loop and waste too much cpu
Changed to .yield(), seems more appropriet
i think Thread.yield can be used for that as well, i read somewhere that is better for using in game loops, dont remember why tho
I am making an Android application for the first time. I implemented saving an adapter for a ListView and it seems to work except when I press the home button on my phone the system locks up for ~5 seconds and then I get a popup saying the app has stopped working. This is not a problem when I press the back button to exit the app. What could be causing this? I can post any code you might think is relevant.
Edit:
Nevermind, I fixed the issue. I was saving an ArrayList of custom objects in a Bundle as a Serializable. Something was wrong there so I implemented Parcelable in my class and that worked out much better. No more crashing.
I was wondering, in relation to c++, if you have some code, for example murmurhash or dlmalloc, that's not yours, but also not really set up like a library you can install, what is the right way to depend on that? Do you chuck it right in with your code compiling it as part of your main code, or put it in a thirdparty folder for example, and link to it? Would you commit that folder in to your git repo? Because it feels wrong to do so. And should I start using cmake? I've been using premake and compiling third party code like this separately, being really lazy about my project organization so basically my code doesn't actually compile straight out the box without setting up those dependencies in a specific way.
I'd put it in a separate folder with its license file, and commit it. Usually I'd set it up to be compiled into a static library to be linked with my program, but for low-level stuff like dlmalloc I'd just include the source files.
God, really noob question. I've been messing with FLTK but all of a sudden it's complaining about "config.h" being missing. I have the header, but no matter where I put it (includes, root directory, work directory) neither g++ -lfltk or fltk-config works. Any advice?
Edit- Using GCC on *nix and MinGW on Windows, both suffer the same end issue. Using FLTK 1.3.2. Did the usual ./configure, make, make install.
I had an assembly assignment to initialize and sum an array of elements stored in the heap.
Now there's a paragraph in the brief that says,
[QUOTE=""]
Be sure to spill any "preserved" registers (see the lecture notes, or figure 2.11 of the Patterson and Hennessy text) you plan to use. But, it is possible to implement this procedure without spilling any registers to the stack.[/QUOTE]
I've managed to implement my solution without spilling any registers, but is it something I should know how to do? What does spilling involve? Would I get extra marks?
Do you know of any good guide/tutorial/article on how to implement a entity component system in C++?
For Java Im using NetBeans 6.9.1 and Im not to sure how I can add an image as a background. Is there an option in the Palette or something?
Any way of simplifying verbose code like this?
[cpp]
if (((lumpName[0] == 'E') && (isdigit(lumpName[1])) && (lumpName[2] == 'M') && (isdigit(lumpName[3])) &&
(lumpName[4] == '\0') && (lumpName[5] == '\0') && (lumpName[6] == '\0') && (lumpName[7] == '\0'))
[/cpp]
Regular expressions
[QUOTE=Richy19;39657927]Did it al your way,
but it outputs:
im getting some random spikes?
[editline]20th February 2013[/editline]
The update and draw methods are empty[/QUOTE]
You must set last_frame before drawing, now after drawing, because you want to make the game run every 16 milliseconds, not 16 milliseconds after the previous frame was drawn.
[code]
while (playing()) {
if ((System.currentTimeMillis() - last_frame) < (1000/Constants.FPS))
continue;
last_frame = System.currentTimeMillis();
Update();
Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
Draw(g);
g.dispose();
strategy.show();
}
[/code]
By the way, why doesn't Draw() itself call getDrawGraphics?
[QUOTE=SiPlus;39675954]You must set last_frame before drawing, now after drawing, because you want to make the game run every 16 milliseconds, not 16 milliseconds after the previous frame was drawn.
[code]
while (playing()) {
if ((System.currentTimeMillis() - last_frame) < (1000/Constants.FPS))
continue;
last_frame = System.currentTimeMillis();
Update();
Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
Draw(g);
g.dispose();
strategy.show();
}
[/code]
By the way, why doesn't Draw() itself call getDrawGraphics?[/QUOTE]
Beause that way in the Draw() I just worry about drawing the game objects, not clearing or displaying the screen
My process that I am trying to do is basically select a node out of an XML document and delete the entire node parent node that the user has selected.
[code]
int index = index = list_serverlist.SelectedIndex;
string selectedItem = list_serverlist.Items[index].ToString();
XmlNode selectedNode = doc.SelectSingleNode("/ServerList/Server/ServerName[text()='" + selectedItem + "']");
selectedNode.ParentNode.RemoveAll();
doc.Save(filePath);
[/code]
The XML file that I am trying to pull/delete from.
[code]
<?xml version="1.0"?>
<ServerList>
<Server>
<ServerName>FAB13-HST01</ServerName>
<ServerIP>wasd</ServerIP>
<ServerUsername>..\Administrator</ServerUsername>
<ServerPassword>wasd</ServerPassword>
</Server>
<Server>
<ServerName>FAB13-HST02</ServerName>
<ServerIP>wasd</ServerIP>
<ServerUsername>..\Administrator</ServerUsername>
<ServerPassword>wasd</ServerPassword>
</Server>
<Server>
<ServerName>FAB13-HST03</ServerName>
<ServerIP>wasd</ServerIP>
<ServerUsername>..\Administrator</ServerUsername>
<ServerPassword>wasd</ServerPassword>
</Server>
</ServerList>
[/code]
say I use the command to delete it and delete FAB13-HST01
[code]
<?xml version="1.0"?>
<ServerList>
[b]<Server>
</Server>[/b]
<Server>
<ServerName>FAB13-HST02</ServerName>
<ServerIP>wasd</ServerIP>
<ServerUsername>..\Administrator</ServerUsername>
<ServerPassword>wasd</ServerPassword>
</Server>
<Server>
<ServerName>FAB13-HST03</ServerName>
<ServerIP>wasd</ServerIP>
<ServerUsername>..\Administrator</ServerUsername>
<ServerPassword>wasd</ServerPassword>
</Server>
</ServerList>
[/code]
The content of the <Server> is deleted however the <Server><Server> remains there and I do not want that. When using
.ParentNode it returns null no matter what I have tried.
Havn't used XML much so not sure if I am missing something right in front of me but tried most things I can think of.
SFML beginner here. I'm having weird issues with rendering text. I'm writing a program that reads nodes, any edges and then draws them to the screen. I want each node to have a number. The issue is that adding some stuff to the program(like setting the outline color for the nodes) breaks the text, showing it as squares. Relevant piece of code:
[code]
sf::CircleShape node[11];
sf::Font font; font.loadFromFile("arial.ttf");
sf::Text nodeNumber("",font,15.f);
nodeNumber.setStyle(sf::Text::Bold);
nodeNumber.setColor(sf::Color::White);
for(int i=1; i<=n; i++)
{
node[i].setRadius(15.f);
node[i].setFillColor(sf::Color(78,190,218));
//node[i].setOutlineThickness(1.f);
//node[i].setOutlineColor(sf::Color(0,120,120));
node[i].setPosition(pos[i][0],pos[i][1]);
}
//...some code
for(int i=1; i<=n; i++)
{
window.draw(node[i]);
nodeNumber.setString(char('0'+i));
nodeNumber.setPosition(pos[i][0]+10,pos[i][1]+6);
window.draw(nodeNumber);
}
[/code]
[img]http://s11.postimage.org/4eupxzlqr/22_23_2013_1_29_41_PM.png[/img]
As you can see, it works fine. However, adding outline to the nodes(commented out) causes this:
[img]http://s11.postimage.org/edfokgv6b/22_23_2013_1_30_18_PM.png[/img]
Thanks.
Do you have an AMD card by any chance?
[QUOTE=MakeR;39687653]Do you have an AMD card by any chance?[/QUOTE]
Yep. Any way of fixing the problem?
edit: Thanks, it works now.
Sorry, you need to Log In to post a reply to this thread.