If you just use ToString on it, it'll return the type as a string. Do you want something like this?
"0 1 1 2 12 2 678 4 52 2"
[QUOTE=NovembrDobby;35148632]If you just use ToString on it, it'll return the type as a string. Do you want something like this?
"0 1 1 2 12 2 678 4 52 2"[/QUOTE]
That's what I'm attempting to do, yes.
You could have something like:
[csharp]
static string ListToString(List<Object> list)
{
string result = "";
foreach(Object o in list)
result += o.ToString + " ";
return result;
}
public static void Main (string[] args)
{
List<Double> list = new List<Double>();
for(int i = 0; i < 10; i++)
list.Add(i + 0.523 * 1.232);
Console.WriteLine(list);
string listS = ListToString(list);
Console.WriteLine(listS);
}
[/csharp]
[QUOTE=Richy19;35148778]You could have something like:
[csharp]
static string ListToString(List<Object> list)
{
string result = "";
foreach(Object o in list)
result += o.ToString + " ";
return result;
}
public static void Main (string[] args)
{
List<Double> list = new List<Double>();
for(int i = 0; i < 10; i++)
list.Add(i + 0.523 * 1.232);
Console.WriteLine(list);
string listS = ListToString(list);
Console.WriteLine(listS);
}
[/csharp][/QUOTE]
Or you could go crazy with LINQ...
[csharp]List<int> ints = new List<int>();
ints.Add(1);
ints.Add(632);
ints.Add(20);
Console.WriteLine(ints.ConvertAll(Convert.ToString).Aggregate((s,
s1) => string.Concat(s + " ", s1))); // Outputs "1 632 20"[/csharp]
So I'm writing functions for timers in my Love2D game and I was wondering if there are any more efficient ways to do it.
[lua]function timer:Create( name, time, func )
local name, time, func = name, time, func;
timer.num = timer.num + 1;
timer[timer.num]= { name, time + os.time(), true, func };
end
function timer:Destroy( name )
for i = 1, #timer do
if timer[i][1] == name then
timer[i][2] = -1;
break;
end
end
end
function timer:Update( dt )
for i = 1, #timer do
if timer[i][3] then
if timer[i][2] == os.time() then timer[i][3] = false; timer[i][4](); end
end
end
end[/lua]
How I did it isn't much code in the first place, but chances are there's some less-taxing/more efficient way out there.
i need some help with oop in c++...
I'm an OK programmer, i did some games and other softwares, but i never used oop.
I can understand why should I use oop, btw I used something like this but not using classes...
I just want an examplel, lets say Age of Empires or any other RTS game, if I create a peon or any other unit, how do I do this in c++ and how I interact with this object? Just a short example is OK, then I will know what to look for in google.
[QUOTE=nick10510;35155586]So I'm writing functions for timers in my Love2D game and I was wondering if there are any more efficient ways to do it.
[lua]function timer:Create( name, time, func )
local name, time, func = name, time, func;
timer.num = timer.num + 1;
timer[timer.num]= { name, time + os.time(), true, func };
end
function timer:Destroy( name )
for i = 1, #timer do
if timer[i][1] == name then
timer[i][2] = -1;
break;
end
end
end
function timer:Update( dt )
for i = 1, #timer do
if timer[i][3] then
if timer[i][2] == os.time() then timer[i][3] = false; timer[i][4](); end
end
end
end[/lua]
How I did it isn't much code in the first place, but chances are there's some less-taxing/more efficient way out there.[/QUOTE]
First off I'd do timer[i][2] >= os.time()
[QUOTE=simie;35149335]Or you could go crazy with LINQ...
[csharp]List<int> ints = new List<int>();
ints.Add(1);
ints.Add(632);
ints.Add(20);
Console.WriteLine(ints.ConvertAll(Convert.ToString).Aggregate((s,
s1) => string.Concat(s + " ", s1))); // Outputs "1 632 20"[/csharp][/QUOTE]
why String.Concat
[editline]16th March 2012[/editline]
[QUOTE=Richy19;35148778][csharp]
static string ListToString(List<Object> list)
{
string result = "";
foreach(Object o in list)
result += o.ToString + " ";
return result;
}
[/csharp][/QUOTE]
this is the worst code ever
[csharp]
static string ListToString<T>(IEnumerable<T> enumerable)
{
StringBuilder sb = new StringBuilder();
foreach(var o in enumerable) {
sb.Append(o.ToString());
}
return sb.ToString();
}
[/csharp]
is much more flexible since you can use any IEnumerable<T> rather than restricting it to just a List<T> and faster for larger inputs since you're avoiding boxing value types into an object and you're not constantly reallocating the same way you would if you just naively concatenated strings
I figured out an algorithm for generating Worms-like 2D landscapes. It looks pretty damn close, the main problem is that I have no idea how to fill it properly.
[img]http://vinh.peniscorp.com/land1.png[/img]
The two most used polygon filling algorithms are apparently the even-odd and nonzero winding algorithms, but the naive approach requires you to test every individual pixel on the map, which is obviously horrible. Since I have a convex polygon filling function available, I also thought of decomposing my polygon into convex polygons and then filling each of them individually, but it looks like it would take a while. Especially with horrors like this:
[img]http://vinh.peniscorp.com/land2.png[/img]
And it's only going to get worse once I smooth the whole shape with a spline.
So how do programs like Flash or Illustrator fill polygons and curved shapes so quickly? There must be a super fast way of doing this but I just can't figure out how to do it. I need it to work with polygons with a fuckload of segments, since I'm going to use a spline to smooth the landscapes shown above before filling them.
Flood fill?
Or am I misunderstanding you?
[QUOTE=ROBO_DONUT;35159844]Flood fill?
Or am I misunderstanding you?[/QUOTE]
Flood fill could probably work but it's a pixel by pixel approach and I don't think that's what people use for filling splines and polygons. Plus you can't really know where to start filling. Well, in this case I do know, but I'd still like a more generalized approach.
Nevermind, used the ear clipping algorithm to turn everything into triangles. Then it's just a matter of filling those triangles. It's not the fastest thing ever but it works okay for what I need.
[img]http://vinh.peniscorp.com/land3.png[/img]
[QUOTE=swift and shift;35159680]why String.Concat
[editline]16th March 2012[/editline]
this is the worst code ever
[csharp]
static string ListToString<T>(IEnumerable<T> enumerable)
{
StringBuilder sb = new StringBuilder();
foreach(var o in enumerable) {
sb.Append(o.ToString());
}
return sb.ToString();
}
[/csharp]
is much more flexible since you can use any IEnumerable<T> rather than restricting it to just a List<T> and faster for larger inputs since you're avoiding boxing value types into an object and you're not constantly reallocating the same way you would if you just naively concatenated strings[/QUOTE]
Doing something the natural way is NOT "the worst code ever". I'd say your code is possible a candidate for that title, because you just completely overengineered a simple problem.
The question was, "how do I convert a List of strings into a single string that lists the contents?". Not "How do I convert any possible Enumerable to a string as fast as possible because I have 1 million items I happen to want to print to the console".
It's useful to someone learning to be shown the necessary steps that are needed to solve the problem, given the constraints.
Whats NOT very useful is to be shown a parameterized function that takes a interface argument to make a string using the StringBuilder class, with the only explanation given for [I]why[/I], being something only other experienced programmers would understand. Further, teaching this "program it perfectly the first time" idea just helps make more programmers who can't accomplish anything because they feel worthless if they write code that solves the problem at hand, and not every problem that could ever happen.
I'm trying to make a networked game, but I have ran into a problem. I want to send a packet to a server, however the server is not receiving said packets. On the client side, I am binding the socket using the address of the server, which seems to not work. Here is the client code:
[cpp]int main(){
WSADATA WsaData;
WSAStartup( MAKEWORD(2,2), &WsaData );
int sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
sockaddr_in client;
client.sin_family = AF_INET;
client.sin_port = htons(port);
client.sin_addr.s_addr = htonl(destination_address);
if(bind(sd,(const sockaddr*)&client, sizeof(sockaddr_in)) <0)
return 1;
char packet_data[256] = {"Test"};
int sent_bytes = sendto( sd, (const char*)packet_data, sizeof(packet_data), 0, (sockaddr*)&client, sizeof(sockaddr_in) );
if ( sent_bytes != sizeof(packet_data) )
return 2;
closesocket(sd);
WSACleanup;
return 0;
}[/cpp]
And here is the server code:
[cpp]int main(){
WSADATA WsaData;
WSAStartup( MAKEWORD(2,2), &WsaData );
int sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = ADDR_ANY;
if(bind(sd,(const sockaddr*)&server, sizeof(sockaddr_in)) <0)
return 1;
while(true){
char packet_data[256];
unsigned int maximum_packet_size = sizeof( packet_data );
sockaddr_in from;
int fromLength = sizeof( from );
int received_bytes = recvfrom( sd, (char*)packet_data, maximum_packet_size, 0, (sockaddr*)&from, &fromLength );
if(received_bytes <= 0)
return 2;
std::cout<<packet_data;
}
closesocket(sd);
WSACleanup;
return 0;
}[/cpp]
You're meant to bind to the local address, not the remote address. UDP is connectionless, so you don't specify a remote address until you're ready to send.
-snip I don't have to bind at all-
Any ideas why this would return an empty string with out triggering an error? (errors do work when I enter an invalid path and the file isn't empty)
[cpp]
string io::Read(string path) {
if(!io::Exists(path))
gLua->ErrorNoHalt(("Couldn't find path \"" + path + "\"!\n").c_str());
return "";
FileHandle_t fhndl = Interface::File->Open(getPath(path).c_str(), "r", "GAME");
if(fhndl) {
int size = Interface::File->Size(fhndl);
char* buffer = new char[size + 1];
Interface::File->Read(buffer, size, fhndl);
Interface::File->Close(fhndl);
return string(buffer);
}
gLua->ErrorNoHalt(("Couldn't open file \"" + path + "\"!\n").c_str());
return "";
}
[/cpp]
[QUOTE=ROBO_DONUT;35159844]Flood fill?
Or am I misunderstanding you?[/QUOTE]
Since you seem to disagree with my solution, I'm going to generate very large maps with this algorithm, something that will be around 4000x2000 pixels. Since I am using Lua, floodfill would be the worst idea ever because processing each pixel individually would take an enormous amount of time and memory.
Since I have a native function that fills triangles, it's actually faster to turn the entire map into triangles and fill them one after each other. The GPU does most of the work and no matter how big the map is, as long as the number of segments is the same, it won't take a lot more time. :v:
Now if there are any better solutions around, I'll take. This one is reasonably fast so far.
-snip-
Somehow managed to bind the same port twice on the same computer in winsock fixing my issue.
I'm making a very basic game in unity for android by following a tutorial (it's just to give my teacher an idea of how quickly something decent can be made) She's going to teach us the programming so all I did was I took the .js files from one of the unity tutorials and used them in my project
[code]var maxTilt : float = 50.0;
var sensitivity : float = 0.5;
var forwardForce : float = 5.0;
var guiSpeedElement : Transform;
var craft : GameObject;
private var normalizedSpeed : float = 0.2;
private var euler : Vector3 = Vector3.zero;
var horizontalOrientation : boolean = true;
function Awake () {
if (horizontalOrientation)
{
Screen.orientation =
Screen.orientation.LandscapeLeft;
}
else
{
Screen.orientation =
Screen.orientation.Portrait;
}
guiSpeedElement = GameObject.Find("speed").transform;
guiSpeedElement.position = new Vector3 (0, normalizedSpeed, 0);
}
function FixedUpdate () {
if(GameObject.FindWithTag("train"))
{
GameObject.FindWithTag("train").rigidbody.AddRelativeForce(0, 0, normalizedSpeed * (forwardForce*3));
var accelerator : Vector3 = Input.acceleration;
if (horizontalOrientation)
{
var t : float = accelerator.x;
accelerator.x = -accelerator.y;
accelerator.y = t;
}
// Rotate turn based on acceleration
euler.y += accelerator.x * turnSpeed;
// Since we set absolute lean position, do some extra smoothing on it
euler.z = Mathf.Lerp(euler.z, -accelerator.x * maxTurnLean, 0.2);
// Since we set absolute lean position, do some extra smoothing on it
euler.x = Mathf.Lerp(euler.x, accelerator.y * maxTilt, 0.2);
// Apply rotation and apply some smoothing
var rot : Quaternion = Quaternion.Euler(euler);
GameObject.FindWithTag("train").transform.rotation = Quaternion.Lerp (transform.rotation, rot, sensitivity);
}
}
function Update () {
for (var evt : Touch in Input.touches)
{
if (evt.phase == TouchPhase.Moved)
{
normalizedSpeed = evt.position.y / Screen.height;
guiSpeedElement.position = new Vector3 (0, normalizedSpeed, 0);
}
}
}
[/code]
I did have to change some things because it said the code was "obsolete" so I just deleted some things till the errors went away. it's not working though, can someone figure out why
I guess I ask this question here instead of making a new topic: I wanna make 3D stuff using C++ so I would like to have some kind of 3D engine. It should be able to do the basic stuff like render 3D models from various formats, that would include static models and animated one. Maybe I would like support for skeletal models as well to make rag-dolls and stuff. Maybe some support for water and particles as well. Some kind is optimization such as portals ore something would be nice as well.
I have tried Crystal Space but the community is dead so there are no one that could help you. I am going to try Ogre3D as well, but I am not sure if I like it.
[QUOTE=_Kilburn;35173629]Since you seem to disagree with my solution, I'm going to generate very large maps with this algorithm, something that will be around 4000x2000 pixels. Since I am using Lua, floodfill would be the worst idea ever because processing each pixel individually would take an enormous amount of time and memory.
Since I have a native function that fills triangles, it's actually faster to turn the entire map into triangles and fill them one after each other. The GPU does most of the work and no matter how big the map is, as long as the number of segments is the same, it won't take a lot more time. :v:
Now if there are any better solutions around, I'll take. This one is reasonably fast so far.[/QUOTE]
I don't disagree with your solution. It's a fine solution. I was disagreeing with your statements that "Flood fill could probably work but it's a pixel by pixel approach", when all methods are 'pixel-by-pixel' approaches, because, if nothing else, you have to set the color of each and every pixel within the bounds. Also, "Plus you can't really know where to start filling", which isn't true. You start outside the boundary, you move towards any point anywhere on a boundary and as soon as you cross a line, you're inside the shape (odd-even, etc., but you need to handle some special cases with acute angles)
I didn't know that you had access to hardware rasterization. Given that, your solution is, I think, better.
I have a base class for all map objects, from which all tiles inherit.
I then have this in my map class
[cpp]
std::vector<MapObject> mObjs;
[/cpp]
to which I add a tile like so:
[cpp]
mObjs.push_back(GrassComponents(eGame));
[/cpp]
but when I call draw:
[cpp]
mObjs[0].Draw();
[/cpp]
I think its calling the MapObjects drw method and not the grass components draw method.
All draw methods are virtual
If I have a vector of mapobjects pointers and add a new grassComponent it works fine, but due to what I want to do at a later stage I cant really do this unless there is a way to clone th new objects or something :S
[QUOTE=Richy19;35176243]I have a base class for all map objects, from which all tiles inherit.
I then have this in my map class
[cpp]
std::vector<MapObject> mObjs;
[/cpp]
to which I add a tile like so:
[cpp]
mObjs.push_back(GrassComponents(eGame));
[/cpp]
but when I call draw:
[cpp]
mObjs[0].Draw();
[/cpp]
I think its calling the MapObjects drw method and not the grass components draw method.
All draw methods are virtual
If I have a vector of mapobjects pointers and add a new grassComponent it works fine, but due to what I want to do at a later stage I cant really do this unless there is a way to clone th new objects or something :S[/QUOTE]
Try to make the draw function in GrassComponent non-virtual.
[QUOTE=Richy19;35176243]If I have a vector of mapobjects pointers and add a new grassComponent it works fine, but due to what I want to do at a later stage I cant really do this unless there is a way to clone th new objects or something :S[/QUOTE]
You're going to have to [url=http://cboard.cprogramming.com/cplusplus-programming/123284-polymorphism-via-vector-objects.html]make[/url] [url=http://stackoverflow.com/questions/8790210/polymorphism-in-c-loss-of-type-in-vector-of-parent-class]them[/url] [url=http://www.cplusplus.com/forum/general/17754/]pointers[/url].
[QUOTE=NovembrDobby;35176951]You're going to have to [url=http://cboard.cprogramming.com/cplusplus-programming/123284-polymorphism-via-vector-objects.html]make[/url] [url=http://stackoverflow.com/questions/8790210/polymorphism-in-c-loss-of-type-in-vector-of-parent-class]them[/url] [url=http://www.cplusplus.com/forum/general/17754/]pointers[/url].[/QUOTE]
Yea I thought so,
The problem is that right now I have the map editor class, this has a vector of mapObjects* that holds all the different tiles, I then have a mapObject* that points to the current map tile in use, I then want to make it so if I push enter have it copy the data to the levels vector of mapObjects* problem is if I do that then the position wont stick.
Dont know if that explains it very well
I have run through ever line of my code and can't seem to find the problem with it. My character refuses to aim at the mouse. The function I made for him to aim at it is right so that means it's something most likely to do with my camera code yet I still don't see a problem in there. I don't want to post up code because there is a LOT where the problem could lie.
So if anyone is open to help me. It's a C# XNA project [url]http://www.mediafire.com/?k9dglre07h6l87l[/url]
I want to know how to do 2D overhead collision detection in java. This is basically just making it so when the character touches a border or something, they can't go any further. I have been struggling with this for almost 2 weeks, for I am not a very good programmer.
Check if the X and Y co-ordinates are below 0, then set them to zero if they are, then check if X +width is above the map width and if so set the X co-ord to mapwidth - width. Then check Y + height > map height, and set Y = mapheight-height.
[QUOTE=WTF Nuke;35182690]Check if the X and Y co-ordinates are below 0, then set them to zero if they are, then check if X +width is above the map width and if so set the X co-ord to mapwidth - width. Then check Y + height > map height, and set Y = mapheight-height.[/QUOTE]
Although I was more so hoping for a way to make a 2d engine with it, that would work for the borders. Thanks.
[editline]17th March 2012[/editline]
I added the code, it works a lot better than expected. Thanks so much!
Sorry, you need to Log In to post a reply to this thread.