• What do you need help with? Version 5
    5,752 replies, posted
How would you go about and round a number to the nearest x? For example, rounding to nearest decimal, rounding to nearest 5, and so on.
[QUOTE=Mordi;37324998]How would you go about and round a number to the nearest x? For example, rounding to nearest decimal, rounding to nearest 5, and so on.[/QUOTE] [lua] rounded = math.Round(num/nearest)*nearest [/lua]
[QUOTE=ralle105;37325157][lua] rounded = math.Round(num/nearest)*nearest [/lua][/QUOTE] I guess it would be relevant in this case to note that I am working in C++, where there doesn't seem to be a round-function. However, it should be possible by flooring to an integer and multiplying/dividing from there, but I don't know exactly how. Nevermind, this seems to work. Thanks! [cpp]value = (GLint)((value / step) + 0.5f) * step;[/cpp]
Oh sorry I thought I was in the lua waywo:v:
I've begun relearning Java since I understand programming more, now, and I'm stuck on a certain concept: interfaces. I do not really understand the use of them. Can someone give me a brief explanation and maybe an example of where it's used? Thanks
[QUOTE=wakeboarderCWB;37325529]I've begun relearning Java since I understand programming more, now, and I'm stuck on a certain concept: interfaces. I do not really understand the use of them. Can someone give me a brief explanation and maybe an example of where it's used? Thanks[/QUOTE] I'd type you the classic Animal inheritance example but I can't remember it right now. Basically, interfaces define a set of methods (namely, the [I]interface of the objects[/I]) which the classes that implement it then... implement. After that, you can create an instance of any single implementation class and use them as if they were of the interface type. (For example, if you have class A, class B and interface I, and the two classes implement I, you could have a variable x of type I which could then contain instances of both A and B.)
[QUOTE=Mordi;37325248]I guess it would be relevant in this case to note that I am working in C++, where there doesn't seem to be a round-function. However, it should be possible by flooring to an integer and multiplying/dividing from there, but I don't know exactly how. Nevermind, this seems to work. Thanks! [cpp]value = (GLint)((value / step) + 0.5f) * step;[/cpp][/QUOTE] Casting to an int seems a little dirty, you should use floor() from cmath like so: [code]float round(float a) { return floor(a+0.5); } Value = round(Value/Nearest)*Nearest;[/code]
[QUOTE=esalaka;37326475]I'd type you the classic Animal inheritance example but I can't remember it right now. Basically, interfaces define a set of methods (namely, the [I]interface of the objects[/I]) which the classes that implement it then... implement. After that, you can create an instance of any single implementation class and use them as if they were of the interface type. (For example, if you have class A, class B and interface I, and the two classes implement I, you could have a variable x of type I which could then contain instances of both A and B.)[/QUOTE] Oh! I get it much better now. Thanks a bunch!
[QUOTE=wakeboarderCWB;37325529]I've begun relearning Java since I understand programming more, now, and I'm stuck on a certain concept: interfaces. I do not really understand the use of them. Can someone give me a brief explanation and maybe an example of where it's used? Thanks[/QUOTE] An interface is sort of a different type of polymorphism than you're probably used to. Instead of extending another object, you implement an interface to support the functionality, which allows you to essentially have multiple inheritances on a class while still being able to have the class hierarchy you want. Interfaces use abstract methods, so all the real code is defined in the implementing class instead of the interface itself, but the interface defines the ways it connects with other objects. They set standards for classes to interact with each other, so different programmers can have code that works well together by implementing the same interfaces. A lot of functions and classes in Java are set up to use interfaces to allow any type of class to be used, while still requiring them to meet a standard for usage. For example, the Comparable interface, which tells Java your class includes code for the compareTo method, and allows you to use the built in classes to sort a Collection of that class.
[QUOTE=Neo Kabuto;37326829]An interface is sort of a different type of polymorphism than you're probably used to. Instead of extending another object, you implement an interface to support the functionality, which allows you to essentially have multiple inheritances on a class while still being able to have the class hierarchy you want. Interfaces use abstract methods, so all the real code is defined in the implementing class instead of the interface itself, but the interface defines the ways it connects with other objects. They set standards for classes to interact with each other, so different programmers can have code that works well together by implementing the same interfaces. A lot of functions and classes in Java are set up to use interfaces to allow any type of class to be used, while still requiring them to meet a standard for usage. For example, the Comparable interface, which tells Java your class includes code for the compareTo method, and allows you to use the built in classes to sort a Collection of that class.[/QUOTE] Wow, great explanation! Thanks. So interfaces basically are a guidance to what the class will do? I don't essentially need them, correct?
[QUOTE=wakeboarderCWB;37326976]Wow, great explanation! Thanks. So interfaces basically are a guidance to what the class will do? I don't essentially need them, correct?[/QUOTE] You don't [I]need [/I]them, but it's a very common object-oriented programming design pattern, so expect to use them at least occasionally. APIs for Java also tend to use them a lot.
[QUOTE=Neo Kabuto;37327097]You don't [I]need [/I]them, but it's a very common object-oriented programming design pattern, so expect to use them at least occasionally. APIs for Java also tend to use them a lot.[/QUOTE] Yeah, thanks a lot! Both you and eslaka were a great help and I appreciate it very much!
[QUOTE=Naelstrom;37326506]Casting to an int seems a little dirty, you should use floor() from cmath like so: [code]float round(float a) { return floor(a+0.5); } Value = round(Value/Nearest)*Nearest;[/code][/QUOTE] Hmm, I understand what you mean but I don't see how it's dirty. Although I'm no expert in C++ by any means.
[QUOTE=Mordi;37328398]Hmm, I understand what you mean but I don't see how it's dirty. Although I'm no expert in C++ by any means.[/QUOTE] I'm not sure it applies in this case, but some type casts have undefined behavior, and might not be the same on all systems. Pretty sure ints in C++ aren't like that now, though.
I'm having trouble with basic trigonometry: Basically, I have a ball that's supposed to bounce off a line. [img]http://i.imgur.com/yU6ML.png[/img] Values that I know: the ball's coordinates, coordinates of 2 points on the line, coordinates of intersection point of the ball's traectory and the line, X and Y velocities of the ball. I need to calculate the new X and Y velocities after the ball bouncing off the line! No gravity or friction needed. I feel like this is something simple but unfortunately my math skills have failed me, and I don't even know what this is called to look it up. Please help if you can!
The three important variables here are N (normal of the collision surface), v (incoming velocity) and V (new velocity). The normal of the collision surface can be calculated by taking the cross product of the vector from A to B and the up vector: [img]http://i.imgur.com/z7ZBN.png[/img] This can be simplified by working out the cross product: [img]http://i.imgur.com/t28nr.png[/img] The incoming velocity v is already given, as you indicated in your post. Reflection in physics works like this: [img]http://upload.wikimedia.org/wikipedia/commons/1/10/Reflection_angles.svg[/img] That means that the angle between N and v must be the same as the angle between N and V. This problem would be easy to solve if the line was exactly horizontal. Then we could simply negate the y velocity and keep the x velocity. So, why not change the problem so it's exactly like that? Let's rotate the problem! We first need the angle to rotate the problem around so that the line is horizontal. This can be easily calculated with the inverse tangens. We'll call this angle theta. [img]http://i.imgur.com/cKBLt.png[/img] In your example case, it would be around 40 degrees. Now let's rotate the problem by -theta so that the line is flat. This requires you to rotate the velocity as well: [img]http://i.imgur.com/eVKrm.png[/img] v' is the incoming velocity after rotating the problem. The rotated outgoing velocity is now very easy to determine because this is an easy problem to solve: [img]http://i.imgur.com/DiCfI.png[/img] [img]http://i.imgur.com/CObaN.png[/img] The x velocity is unaffected, but the ball will now go upwards (or downwards dependent on incidence angle). Now the only thing that remains is rotating the problem back with +theta: [img]http://i.imgur.com/cLieR.png[/img] When substituting the intermediate steps, the solution becomes: [img]http://i.imgur.com/taE6b.png[/img] Unfortunately I don't know enough about math to be able to simplify this, but this will at least get you your post collision velocity.
Thank you SO much, Overv! It all makes sense now!
It seems I lied about N being of any relevance at all, but I hope I helped you out well :p
Well, any cos(-θ) can be flipped to plain cos(θ). This is because cosine is an even function and is therefore mirrored across the Y axis. In other words, positive [I]x[/I] and negative [I]x[/I] both give the same result. Also, any -sin(-θ) can be rewritten as plain sin(θ). The sine function is an odd function, and therefore -f(-x) = f(x). Note that θ MUST be negative for this one to work.
I am a bit confused about how to use the function "glVertexAttribPointer". The "pointer" object is a const void* and yet everyone uses it as an offset. I also saw an example were someone passed vertex data through the "pointer" argument. I just want to know if you are supposed to pass an array of vertex data or cast an int into a void* and use it as an offset, or can you do both? Can you pass an array of vertex data even if there is already a buffer bound?
[QUOTE=polkm;37335292]I am a bit confused about how to use the function "glVertexAttribPointer". The "pointer" object is a const void* and yet everyone uses it as an offset. I also saw an example were someone passed vertex data through the "pointer" argument. I just want to know if you are supposed to pass an array of vertex data or cast an int into a void* and use it as an offset, or can you do both? Can you pass an array of vertex data even if there is already a buffer bound?[/QUOTE] void* is a special C++ type that accepts any kind of pointer. This means you can put an array of floats, ints, doubles, etc into it without it complaining or having to cast anything. For the people who use an "offset" instead of a pointer (not everyone does it), what they're probably doing is passing a pointer to a class/struct full of a bunch of different sets of vertex data, then offsetting it to the actual array of data. You can do either the offset or bind directly to the vertex data, as they are the exact same thing really. And yes you can pass in vertex data more than once to override the previous vertex data, and I've been told this is the correct way to update vertex buffers that have changed. [editline]asdf[/editline] If you want a reference to mostly non-deprecated OpenGL that is also compatible with OpenGL 2.0, you can check out my game engine here: [url]https://github.com/naelstrof/Astrostruct[/url] It's pretty well documented and you can ask me questions about it on steam if you'd like.
Does anybody remember program made by other fper that lets you open windows w/o borders? I really want it now (for todo)
[QUOTE=Naelstrom;37315932]Use aabb penetration solving instead, it's much more exact and clean. You would still have to do some nice time stepping or some simple tracers to keep fast moving objects from flying through everything though.[/QUOTE] Do you have any links to articles on that? All I can find when I google it is that one facepuncher's help thread on his platforming issues with the spikes.
[QUOTE=Overv;37332548]The three important variables here are N (normal of the collision surface), v (incoming velocity) and V (new velocity). The normal of the collision surface can be calculated by taking the cross product of the vector from A to B and the up vector: [img]http://i.imgur.com/z7ZBN.png[/img] This can be simplified by working out the cross product: [img]http://i.imgur.com/t28nr.png[/img] The incoming velocity v is already given, as you indicated in your post. Reflection in physics works like this: [img]http://upload.wikimedia.org/wikipedia/commons/1/10/Reflection_angles.svg[/img] That means that the angle between N and v must be the same as the angle between N and V. This problem would be easy to solve if the line was exactly horizontal. Then we could simply negate the y velocity and keep the x velocity. So, why not change the problem so it's exactly like that? Let's rotate the problem! We first need the angle to rotate the problem around so that the line is horizontal. This can be easily calculated with the inverse tangens. We'll call this angle theta. [img]http://i.imgur.com/cKBLt.png[/img] In your example case, it would be around 40 degrees. Now let's rotate the problem by -theta so that the line is flat. This requires you to rotate the velocity as well: [img]http://i.imgur.com/eVKrm.png[/img] v' is the incoming velocity after rotating the problem. The rotated outgoing velocity is now very easy to determine because this is an easy problem to solve: [img]http://i.imgur.com/DiCfI.png[/img] [img]http://i.imgur.com/CObaN.png[/img] The x velocity is unaffected, but the ball will now go upwards (or downwards dependent on incidence angle). Now the only thing that remains is rotating the problem back with +theta: [img]http://i.imgur.com/cLieR.png[/img] When substituting the intermediate steps, the solution becomes: [img]http://i.imgur.com/taE6b.png[/img] Unfortunately I don't know enough about math to be able to simplify this, but this will at least get you your post collision velocity.[/QUOTE] 1. project the velocity vector onto the surface normal 2. subtract this projection from the velocity twice [img]http://latex.codecogs.com/gif.latex?V%20=v%20-%202%20\frac%20{n%20\cdot%20v}{||n||^2}n[/img] (V is final velocity, v is initial velocity, n is surface normal) In C code: [cpp]/* Input: * surface0, surface1 surface start and end points * velocity initial velocity * Output: * out_velocity mirrored velocity */ /* Surface normal: flip the axes and change one coordinate's sign. */ float nx = surface1.y - surface0.y; float ny = -surface1.x + surface0.x; float m = 2 * ((nx * velocity.x) + (ny * velocity.y)) / (nx * nx + ny * ny); out_velocity.x = velocity.x - m * n.x; out_velocity.y = velocity.y - m * n.y;[/cpp]
i don't actually. it was once implemented in my game engine though and the functions/documentation still exist. Intersects and minimum translation are of interest to you: [url]http://farmpolice.com/astrostruct/docs/NPhysics_8hpp.html#a67cbd2c3da7a70eb4a728e14cd27b282[/url] [url]https://github.com/naelstrof/Astrostruct/blob/master/src/NPhysics.cpp[/url] boxes are defined as a vec4 with origin at the given point: (x, y, width, height) you can ask me questions about it on steam in about 8 hours. im going to sleep.
[QUOTE=vombatus;37337321]Does anybody remember program made by other fper that lets you open windows w/o borders? I really want it now (for todo)[/QUOTE] There were two, but you're probably thinking of FrameAway. (the other was Window Manager) Here are links to both. [url]http://frameaway.org/[/url] [url]http://foohy.net/post/19141165866/new-release-window-manager[/url]
[QUOTE=BMCHa;37337752]There were two, but you're probably thinking of FrameAway. (the other was Window Manager) Here are links to both. [url]http://frameaway.org/[/url] [url]http://foohy.net/post/19141165866/new-release-window-manager[/url][/QUOTE] Thanks a lot - I was looking exactly for second one for source. Soon there will be the best ToDo app, mark my words.
I need help with getting the android emulator to work. It always shows the android loading screen but it never loads the program it compiled. It starts by using a good % of CPU and then after a while it starts idling until it disconnects with no notice. Here's the log: [code][2012-08-21 09:34:58 - AccelerometerPlay] ------------------------------ [2012-08-21 09:34:58 - AccelerometerPlay] Android Launch! [2012-08-21 09:34:58 - AccelerometerPlay] adb is running normally. [2012-08-21 09:34:59 - AccelerometerPlay] Performing com.example.android.accelerometerplay.AccelerometerPlayActivity activity launch [2012-08-21 09:34:59 - AccelerometerPlay] Automatic Target Mode: launching new emulator with compatible AVD 'droid' [2012-08-21 09:34:59 - AccelerometerPlay] Launching a new emulator with Virtual Device 'droid' [2012-08-21 09:35:31 - AccelerometerPlay] New emulator found: emulator-5554 [2012-08-21 09:35:31 - AccelerometerPlay] Waiting for HOME ('android.process.acore') to be launched... [2012-08-21 09:44:19 - AccelerometerPlay] emulator-5554 disconnected! Cancelling 'com.example.android.accelerometerplay.AccelerometerPlayActivity activity launch'! [/code] By the way I've never been able to get past this loading screen even after several reinstalls of everything.
[QUOTE=Naelstrom;37322266]I find linking dynamically much easier than static; while static doesn't cause as much clutter as dynamic. It's mainly preference as far as I know, do what you think is best.[/QUOTE] I guess it would also affect compile time? I mean, if you're not recompiling all your dynamic libraries each time. [QUOTE=ECrownofFire;37333329]Well, any cos(-θ) can be flipped to plain cos(θ). This is because cosine is an even function and is therefore mirrored across the Y axis. In other words, positive [I]x[/I] and negative [I]x[/I] both give the same result. Also, any -sin(-θ) can be rewritten as plain sin(θ). The sine function is an odd function, and therefore -f(-x) = f(x). Note that θ MUST be negative for this one to work.[/QUOTE] It should be noted that sin(-x) = -sin(x), too. You didn't make that ever so clear.
[QUOTE=OnDemand;37338051] By the way I've never been able to get past this loading screen even after several reinstalls of everything.[/QUOTE] Okay, I finally managed to start up the emulator for the first time but only separately and I still have the same problem as before when I try to start it from the eclipse IDE. [img]http://img819.imageshack.us/img819/9894/droip.png[/img]
Sorry, you need to Log In to post a reply to this thread.