What would be the best way to determine if something is travelling in an approximate line? in 3D
Based on what? A bunch of positions at different times?
Yea, essentially I have 8 objects and X points in 3d for each object and I want to check if any of these arent travelling in a straight line
Record the last n positions. Calculate the dot-product between the vectors 'n-1', 'n', and 'n+1' and see if the line is still straight enough for you. You don't have do do any additional calculation, you can just compare the dot-product directly.
For example if(dot(pos[n] - pos[n-1], pos[n+1] - pos[n]) < 0.9)
That will detect sharp changes in direction. But it will not detect slow but steady changes.
To detect those skip intermediate vectors and (for example) substitute n-1 with n-10 and n+1 with n+10
If you need a clearer explanation or sample I'll see what I can do
[QUOTE=Richy19;43025766]Yea, essentially I have 8 objects and X points in 3d for each object and I want to check if any of these arent travelling in a straight line[/QUOTE]
The delta between dimensions should scale proportionally.
if DeltaX1 is 50 and DeltaY1 is 100 and if DeltaX2 is 25 then DeltaY2 should be 50.
[editline]30th November 2013[/editline]
This is for exact line, if you want something approximate then just ignore small offset, for example DeltaY2 could be 49 or 51.
EDIT:
You could also do
[code]
float A1 = DeltaX1 / DeltaX2;
float A2 = DeltaY1 / DeltaY2;
float APPROX = 0.001f;
if (Abs(A1 - A2) < APPROX) then
STRAIGHT LINE
else
NOT STRAIGHT LINE
[/code]
I'm trying to create a simple UI in a console window and I'm having trouble with characters.
This is the output I get:
[img]http://i.imgur.com/BSMEBfq.png[/img]
The first and second row are created using std::cout, but the third one is created using FillConsoleOutputCharacterA. As you can see, the horizontal and vertical double line characters produce 'a', but the corner character works fine. I can't use std::cout, because that and other lines will be changing often and clearing the whole screen is too slow.
How can I fix this?
[QUOTE=Nevec;43025876]I'm trying to create a simple UI in a console window and I'm having trouble with characters.
This is the output I get:
*image*
The first and second row are created using std::cout, but the third one is created using FillConsoleOutputCharacterA. As you can see, the horizontal and vertical double line characters produce 'a', but the corner character works fine. I can't use std::cout, because that and other lines will be changing often and clearing the whole screen is too slow.
How can I fix this?[/QUOTE]
[url=https://en.wikipedia.org/wiki/Curses_(programming_library)]curses[/url]?
Use curses or conio.h
[QUOTE=Felheart;43025844]Record the last n positions. Calculate the dot-product between the vectors 'n-1', 'n', and 'n+1' and see if the line is still straight enough for you. You don't have do do any additional calculation, you can just compare the dot-product directly.
For example if(dot(pos[n] - pos[n-1], pos[n+1] - pos[n]) < 0.9)
That will detect sharp changes in direction. But it will not detect slow but steady changes.
To detect those skip intermediate vectors and (for example) substitute n-1 with n-10 and n+1 with n+10
If you need a clearer explanation or sample I'll see what I can do[/QUOTE]
No, this is actually perfect for what I need. Thanks :D
[QUOTE=Z_guy;43026010][url=https://en.wikipedia.org/wiki/Curses_(programming_library)]curses[/url]?[/QUOTE][QUOTE=esalaka;43026084]Use curses or conio.h[/QUOTE]
Thanks, but I'd rather not use an external library and conio.h doesn't seem to support the characters I need.
[b]Edit:[/b]
I think I fixed the problem by using the unicode variant of the function and the appropriate character code.
[QUOTE=Felheart;43025844]For example if(dot(pos[n] - pos[n-1], pos[n+1] - pos[n]) < 0.9)[/QUOTE]
Wait, how would this work? Surelly thats just comparing the dot of 2 points?
[QUOTE=Nevec;43026188]Thanks, but I'd rather not use an external library and conio.h doesn't seem to support the characters I need.
[b]Edit:[/b]
I think I fixed the problem by using the unicode variant of the function and the appropriate character code.[/QUOTE]
Congratulations on fixing it, but I cannot stress enough how useful the curses library is if you're doing any kind of extensive console I/O work. I really urge you to reconsider your choice against using it, it can be an invaluable tool to wield.
[editline]30th November 2013[/editline]
[QUOTE=Richy19;43026265]Wait, how would this work? Surelly thats just comparing the dot of 2 points?[/QUOTE]
The dot product is proportional to the angle between the two vectors, if they are close to being the same vector then the dot product will be large. The dot product of two vectors at 90 degrees to each other is 0, and the dot product of two vectors parallel to each other is a large number (x0 * x1 + y0 * y1 + z0 * z1).
-ninja'd-
[QUOTE=Chris220;43026391]Congratulations on fixing it, but I cannot stress enough how useful the curses library is if you're doing any kind of extensive console I/O work. I really urge you to reconsider your choice against using it, it can be an invaluable tool to wield.[/QUOTE]
Thanks. This project won't have any complex user interactions, though. It's a simple maze game. There will be an animation when the player completes or resets the maze and I needed a way to change individual characters. Input is working like it should. At the moment the only problem I had was the one I described and now it's fixed.
[QUOTE=Richy19;43026265]Wait, how would this work? Surelly thats just comparing the dot of 2 points?[/QUOTE]
I'll make an easy example.
Lets assume you have recorded 3 vectors/points/positions of your object.
Lets call them p1,p2 and p3
Now you want to calculate the direction from p1 to p2 and the result will be stored in d1
d1 = normalize(p2-p1)
Also you want to know the direction from p2 to p3 and store it in d2
d2 = normalize(p3-p2)
now you build the dotproduct of d1 and d2 and store the result in 'angle'
angle=dot(d1,d2)
Now you have the change of direction of your object (from -1 to 1, where 1 is 'identical' direction and -1 'the object turned by 180°' and 0 is 'the object turned by 90°')
You could say your object's direction changed by 'angle' radians over these 3 points.
As a nice trick is that you don't have to use the last 3 points to get 2 "delta" vectors.
Instead you can only take the last position (as p1) and the current position (as p2) and then calculate d2 like this
d2 = normalize(object.Velocity);
[editline]30th November 2013[/editline]
Oh and 'angle' is not really an angle, its a number between -1 and 1
To know how much your object changed in degrees you have to convert the intervall -1;1 to 0;180
I'd do it like this:
degrees = ((angle + 1) * 0.5) * 180;
[editline]30th November 2013[/editline]
[url]http://en.wikipedia.org/wiki/Dot_product[/url]
Btw are you working with 2 or 3 dimensions??
[editline]30th November 2013[/editline]
And to answer your question: No, the dotproduct of two points does not yield a meaningful result.
You have to give 'direction-vectors' to the dotproduct not 'position-vectors'.
Also the dotproduct will only return numbers in the interval -1;1 if you give normalized vectors to it.
[QUOTE=Felheart;43026579]I'll make an easy example.
Lets assume you have recorded 3 vectors/points/positions of your object.
Lets call them p1,p2 and p3
Now you want to calculate the direction from p1 to p2 and the result will be stored in d1
d1 = normalize(p2-p1)
Also you want to know the direction from p2 to p3 and store it in d2
d2 = normalize(p3-p2)
now you build the dotproduct of d1 and d2 and store the result in 'angle'
angle=dot(d1,d2)
Now you have the change of direction of your object (from -1 to 1, where 1 is 'identical' direction and -1 'the object turned by 180°' and 0 is 'the object turned by 90°')
You could say your object's direction changed by 'angle' radians over these 3 points.
As a nice trick is that you don't have to use the last 3 points to get 2 "delta" vectors.
Instead you can only take the last position (as p1) and the current position (as p2) and then calculate d2 like this
d2 = normalize(object.Velocity);
[editline]30th November 2013[/editline]
Oh and 'angle' is not really an angle, its a number between -1 and 1
To know how much your object changed in degrees you have to convert the intervall -1;1 to 0;180
I'd do it like this:
degrees = ((angle + 1) * 0.5) * 180;
[editline]30th November 2013[/editline]
[url]http://en.wikipedia.org/wiki/Dot_product[/url]
Btw are you working with 2 or 3 dimensions??
[editline]30th November 2013[/editline]
And to answer your question: No, the dotproduct of two points does not yield a meaningful result.
You have to give 'direction-vectors' to the dotproduct not 'position-vectors'.
Also the dotproduct will only return numbers in the interval -1;1 if you give normalized vectors to it.[/QUOTE]
In 3d, its not that I need to find out the amount, its simply if an object isnt moving in a straifght line.
I have an assignment where we are given a number of images from the left eye perspective and the right. and we have to tell which one out of 8 objects is swirving
[QUOTE=Richy19;43026898]In 3d, its not that I need to find out the amount, its simply if an object isnt moving in a straifght line.
I have an assignment where we are given a number of images from the left eye perspective and the right. and we have to tell which one out of 8 objects is swirving[/QUOTE]
If two normalized vectors are in the same direction, their dot product is 1. As their directions change, the dot product gets closer to 0.
Similar to what Felheart said, take the 2 previous points and the current point as p1, p2, p3.
If the result of dot(normalize(p2 - p1), normalize(p3 - p2)) is very close to 1, then points p1, p2, and p3 are collinear. That is, the object moved in a straight line from p1 to p3.
[QUOTE=robmaister12;43027190]If two normalized vectors are in the same direction, their dot product is 1. As their directions change, the dot product gets closer to 0.
Similar to what Felheart said, take the 2 previous points and the current point as p1, p2, p3.
If the result of dot(normalize(p2 - p1), normalize(p3 - p2)) is very close to 1, then points p1, p2, and p3 are collinear. That is, the object moved in a straight line from p1 to p3.[/QUOTE]
Adding to that, the result is the cosine of the angle, so you reach 0 at 90° and -1 if they point in opposite directions.
Hey guys, I know for sure I'm going about this wrong, someone point me in the right direction please!..
Rolling my own Keyboard input thing, wanted a 'virtual' keyboard that I could map real keys to (e.g. map the letters a,b,c and d on the real keyboard all to the letter a on the virtual one (the one that is exposed to the game)). Also see how long each key has been down.
ATM I'm filtering the messages the application receives - looking for WM_KEYDOWN and WM_KEYUP. When one is detected, the respective entry in a dictionary is updated to show that key's state. This dictionary is polled by the game. The game runs in a 'While-there-are-no-messages-in-the-message-pump' loop, for want of a better term.
My problem is, If you hold down say the 'a' key, then the 's' key if you release the 'a' key it's state in the dictionary is not updated until the 's' key is released.
My thoughts was, instead of messages, using raw keyboard input...i.e. For each key in the dictionary, every game loop iteration, simply scan the keyboard and update it's entry.
CODE:
[code]
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Red_Brick_2D
{
public class IODaemon : IMessageFilter
{
private Dictionary<Keys, KeyBinding> keyTable = null;
#region Constructors
public IODaemon()
{
keyTable = new Dictionary<Keys, KeyBinding>();
Application.AddMessageFilter(this);
}
public IODaemon(IEnumerable<KeyValuePair<Keys, KeyBinding>> bindings)
:this()
{
foreach (KeyValuePair<Keys, KeyBinding> binding in bindings)
{
keyTable.Add(binding.Key, binding.Value);
}
}
#endregion
#region Methods
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 256 /*0x0100*/)
{
KeyBinding keyBinding;
if (keyTable.TryGetValue((Keys)m.WParam, out keyBinding))
{
keyBinding.Pressed = true;
}
}
else if (m.Msg == 257 /*0x0101*/)
{
KeyBinding keyBinding;
if (keyTable.TryGetValue((Keys)m.WParam, out keyBinding))
{
keyBinding.Pressed = false;
keyBinding.Time = TimeSpan.Zero;
}
}
return false;
}
public void Update(TimeSpan dt)
{
foreach (KeyBinding keyBinding in keyTable.Values)
{
if (keyBinding)
{
keyBinding.Time += dt;
}
}
}
public void BindKey(Keys key)
{
KeyBinding existingBinding;
KeyBinding newBinding = new KeyBinding(key);
if (keyTable.TryGetValue(key, out existingBinding))
{
existingBinding = newBinding;
return;
}
keyTable.Add(key, newBinding);
}
public void BindKey(Keys key, KeyBinding keyBinding)
{
BindKey(key);
keyTable[key] = keyBinding;
}
public void UnbindKey(Keys key)
{
KeyBinding existingBinding;
if (keyTable.TryGetValue(key, out existingBinding))
{
keyTable.Remove(key);
}
}
#endregion
#region Properties
public KeyBinding this[Keys key]
{
get
{
KeyBinding keyBinding;
keyTable.TryGetValue(key, out keyBinding);
return keyBinding;
}
}
#endregion
#region Classes
public class KeyBinding
{
public KeyBinding(Keys Target)
{
this.Pressed = false;
this.Time = TimeSpan.Zero;
this.Target = Target;
}
public static implicit operator bool(KeyBinding keyBinding)
{
return keyBinding.Pressed;
}
public TimeSpan Time { get; set; }
public Keys Target { get; set; }
public bool Pressed { get; set; }
public override string ToString()
{
return String.Format("'{0}' has been pressed for {1} seconds", Target, Time.TotalSeconds);
}
}
#endregion
}
}
[/code]
Anyone know the source of the key-holding problem...and was I right in my thinking about the raw input or is there some other way, thanks.
(NB. I am not using XNA, i'm NOT making a game i'm learning about how games work!)
[editline]1st December 2013[/editline]
Looking back I apologise for the code-turd Iv'e just gave birth too. Problems in descending order of seriousness:
- No comments
- I talked about exposing only the virtual keyboard, but the 'this' accesor just exposes the key of the dictionary
- The IODaemon constructor that takes the IEnumerable argument is horrible
- No comments
- The 'this' accesor does nothing but return null if the key didn't exist
Also, When I fix it, instead of accesing the dictionary by 'real' key, i will be accesing it my a property of the values in the dictionary. I can do this query with .any or elsewise, but is there a better, more efficient way of structuring the whole thing...
it's 2:00am, i'm getting tired of this .. :o
Hey guys, a potential employer has asked me to make something as a test before we move on.
I'm definitely not looking for anyone to do the work for me, that'd be dumb since it's an illustration of my own abilities not yours, but I would like advice if possible. The task is to create a C++ application (probably console, I might put in GUI) where I take in english names for colors (e.g. Dark Blue, Deep Crimson) and convert that into actual colors (e.g. web colors)
I'm allowed to use third party open source libraries for parsing english text, and I'm doing some research on my own, but I'm struggling a bit, I've never done anything with natural language analysis before so 90% of the stuff I'm reading is basically french to me.
Any suggestions for libraries that do this sort of thing would be wonderful, I'm gonna keep researching but it'd be nice to get a push in the right direction if someones done this before.
@Empty_Shadow
If you don't have to parse stuff like "dark blue", "Dark bLue" and so on then i'd suggest you to store colors in a hashtable.
You could also complicate a bit and add just "normal" colors to such a table and return modified one based on prefix e.g. if prefix is "Dark" then just return normal color - 100, 100, 100 or something.
You just:
1) lowercase it
2) split it into words
3) first word is a modifier ("dark", "crimson")
4) second is hue
That's approximately how you'd do it
[QUOTE=vombatus;43045971]
4) second is hue[/QUOTE]
Dumb internet keeps causing me to read that as "hway".
I've got a 2d array that's a grid, and I insert a value into it somewhere.. now I want it to diffuse naturally throughout the grid.
My code:
[Code]
gs = global.grid_size;
//all center cells
// for each cell not on the edge, we divide the last value by 5 then add on the surrounding cells values' also divided by 5
for(ix=1; ix<gs-1; ix+=1){
for(iy=1; iy<gs-1; iy+=1){
density[ix,iy] = last_density[ix,iy]*0.2 + 0.2*(last_density[ix-1,iy]+last_density[ix+1,iy]+last_density[ix,iy-1]+last_density[ix,iy+1])
}
}
//left and right
// same as before but with the left and right edges, as there's only 4 cells in play we divide by 4 now
for(iy=1; iy<gs-1; iy+=1){
density[0,iy] = last_density[0,iy]*0.25 + 0.25*(last_density[0,iy-1]+last_density[0,iy+1])+(0.2*last_density[1,iy])
density[gs-1,iy] = last_density[gs-1,iy]*0.25 + 0.25*(last_density[gs-1,iy-1]+last_density[gs-1,iy+1])+0.2*last_density[gs-2,iy]
}
//top and bottoms
// same as before
for(ix=1; ix<gs-1; ix+=1){
density[ix,0] = last_density[ix,0]*0.25 + 0.25*(last_density[ix-1,0]+last_density[ix+1,0])+0.2*last_density[ix,1]
density[ix,gs-1] = last_density[ix,gs-1]*0.25 + 0.25*(last_density[ix-1,gs-1]+last_density[ix+1,gs-1])+0.2*last_density[ix,gs-2]
}
//corners
density[0,0] = last_density[0,0]*(1/3) + 0.25*(last_density[0,1]+last_density[1,0])
density[gs-1,0] = last_density[gs-1,0]*(1/3) + 0.25*(last_density[gs-1,1]+last_density[gs-2,0])
density[gs-1,gs-1] = last_density[gs-1,gs-1]*(1/3) + 0.25*(last_density[gs-1,gs-2]+last_density[gs-2,gs-1])
density[0,gs-1] = last_density[0,gs-1]*(1/3) + 0.25*(last_density[0,gs-2]+last_density[1,gs-1])
for(ix=0; ix<gs; ix+=1){
for(iy=0; iy<gs; iy+=1){
last_density[ix,iy] = density[ix,iy]
}
}[/code]
Apologies for the horrible looking code... added some comments to explain what's happening.
Problem is the value added isn't conserved throughout the grid and eventually everything returns to 0, which is not the behaviour I want. I assume it's to do with the edge parts but I'm not certain why.
So I've been programming / developing for websites these past few years with no GitHub account. None of these sites are anything career-like but more for education ( learning networks and protocols and the logic of it all ).
Now that I am looking beyond websites towards desktop / mobile applications, I want to continue with a GitHub presence.
Does anyone have any advice or any documentation on how to get started and to stay active?
[QUOTE=chaz13;43049103]I've got a 2d array that's a grid, and I insert a value into it somewhere.. now I want it to diffuse naturally throughout the grid.
My code:
[Code]
gs = global.grid_size;
//all center cells
// for each cell not on the edge, we divide the last value by 5 then add on the surrounding cells values' also divided by 5
for(ix=1; ix<gs-1; ix+=1){
for(iy=1; iy<gs-1; iy+=1){
density[ix,iy] = last_density[ix,iy]*0.2 + 0.2*(last_density[ix-1,iy]+last_density[ix+1,iy]+last_density[ix,iy-1]+last_density[ix,iy+1])
}
}
//left and right
// same as before but with the left and right edges, as there's only 4 cells in play we divide by 4 now
for(iy=1; iy<gs-1; iy+=1){
density[0,iy] = last_density[0,iy]*0.25 + 0.25*(last_density[0,iy-1]+last_density[0,iy+1])+(0.2*last_density[1,iy])
density[gs-1,iy] = last_density[gs-1,iy]*0.25 + 0.25*(last_density[gs-1,iy-1]+last_density[gs-1,iy+1])+0.2*last_density[gs-2,iy]
}
//top and bottoms
// same as before
for(ix=1; ix<gs-1; ix+=1){
density[ix,0] = last_density[ix,0]*0.25 + 0.25*(last_density[ix-1,0]+last_density[ix+1,0])+0.2*last_density[ix,1]
density[ix,gs-1] = last_density[ix,gs-1]*0.25 + 0.25*(last_density[ix-1,gs-1]+last_density[ix+1,gs-1])+0.2*last_density[ix,gs-2]
}
//corners
density[0,0] = last_density[0,0]*(1/3) + 0.25*(last_density[0,1]+last_density[1,0])
density[gs-1,0] = last_density[gs-1,0]*(1/3) + 0.25*(last_density[gs-1,1]+last_density[gs-2,0])
density[gs-1,gs-1] = last_density[gs-1,gs-1]*(1/3) + 0.25*(last_density[gs-1,gs-2]+last_density[gs-2,gs-1])
density[0,gs-1] = last_density[0,gs-1]*(1/3) + 0.25*(last_density[0,gs-2]+last_density[1,gs-1])
for(ix=0; ix<gs; ix+=1){
for(iy=0; iy<gs; iy+=1){
last_density[ix,iy] = density[ix,iy]
}
}[/code]
Apologies for the horrible looking code... added some comments to explain what's happening.
Problem is the value added isn't conserved throughout the grid and eventually everything returns to 0, which is not the behaviour I want. I assume it's to do with the edge parts but I'm not certain why.[/QUOTE]
If the effect is very slow it may be floating point loss.
[editline]3rd December 2013[/editline]
[QUOTE=Epiclulz762;43049125]So I've been programming / developing for websites these past few years with no GitHub account. None of these sites are anything career-like but more for education ( learning networks and protocols and the logic of it all ).
Now that I am looking beyond websites towards desktop / mobile applications, I want to continue with a GitHub presence.
Does anyone have any advice or any documentation on how to get started and to stay active?[/QUOTE]
You just publish some of the things you make I suppose. If you program a lot your account will show activity automatically, otherwise it won't.
Getting started... just register and download the client. It's really self-explanatory.
In Visual Studio 2013 the cursor sometimes becomes big and grey and types over the text where the cursor is located:
[IMG]http://i.imgur.com/4NrS35N.png[/IMG]
How would I get the normal text input cursor?
Press insert I think.
Press the insert key
[editline]2nd December 2013[/editline]
give me lots of clocks
[QUOTE=WTF Nuke;43052019]Press insert I think.[/QUOTE]
[QUOTE=Banana Lord.;43052024]Press the insert key
[editline]2nd December 2013[/editline]
give me lots of clocks[/QUOTE]
Thank You! This fixed it.
Sorry, you need to Log In to post a reply to this thread.