[QUOTE=war_man333;46757399]Anyone experienced with SFML?
trying to get the player to move when I hold down any of the WASD buttons. right now it just responds on clicking a button rather than holding it down:
[b]main.cpp[/b]
[code] while (window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
input.update(player, event);
break;
}
}[/code]
it calls input, which is my inputmanager, which then sets the position of the player once:
[b]InputManager.cpp[/b]
[code]
void InputManager::update(sf::Sprite &player, sf::Event event)
{
this->event_ = event;
keyHandle(player, event.key.code);
}
void InputManager::keyHandle(sf::Sprite &player, int key)
{
float playerMoveSpeed = 48;
sf::Vector2f playerPos = player.getPosition();
switch(key)
{
case sf::Keyboard::W:
player.setPosition(playerPos.x, playerPos.y-playerMoveSpeed);
break;
case sf::Keyboard::A:
player.setPosition(playerPos.x-playerMoveSpeed, playerPos.y);
break;
case sf::Keyboard::S:
player.setPosition(playerPos.x, playerPos.y+playerMoveSpeed);
break;
case sf::Keyboard::D:
player.setPosition(playerPos.x+playerMoveSpeed, playerPos.y);
break;
default:
player.setPosition(playerPos.x, playerPos.y);
break;
}
}[/code]
of course, changing the movespeed will be necesarry if I manage to change the functionality.
I need to change it in main.cpp, but I'm not sure how.
Live input doesn't work because sf::Keyboard::isKeyPressed rarely works.[/QUOTE]
sf::Keyboard::isKeyPressed() is working fine for me, but you could also use boolean to keep track of the button state and perform the player movement in a continuous update call, something like that:
[code]
bool forward = false;
void Update(float dt)
{
static sf::Event event;
while (m_window.pollEvent(event))
{
switch (event.type) {
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::W) {
forward = true;
}
break;
case sf::Event::KeyReleased:
if (event.key.code == sf::Keyboard::W) {
forward = false;
}
break;
}
}
if (forward)
{
player.setPosition(playerPos.x + (velocity * dt), playerPos.y);
}
}[/code]
This is kinda embarrassing for me to ask, but I don't have any other programming friends to bounce ideas off of, and sometimes I have trouble with seemingly minor things.
If you want to help out a programmer or just have someone to talk to, send me a friend request on Steam. [url]http://steamcommunity.com/id/dilbertbutgay/[/url]
[QUOTE=Cold;46761980]Consider joining [url]http://steamcommunity.com/groups/best_fpp[/url][/QUOTE]
Kinda had this in mind. Thanks!
[QUOTE=HiredK;46758839]sf::Keyboard::isKeyPressed() is working fine for me, but you could also use boolean to keep track of the button state and perform the player movement in a continuous update call, something like that:
[code]
bool forward = false;
void Update(float dt)
{
static sf::Event event;
while (m_window.pollEvent(event))
{
switch (event.type) {
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::W) {
forward = true;
}
break;
case sf::Event::KeyReleased:
if (event.key.code == sf::Keyboard::W) {
forward = false;
}
break;
}
}
if (forward)
{
player.setPosition(playerPos.x + (velocity * dt), playerPos.y);
}
}[/code][/QUOTE]
Found the source of the problem, had this dumb line:
[code]window.setKeyRepeatEnabled(false);[/code]
it was supposed to alleviate the problem, not be the source of it. weird.
What's a good resource for time complexity and notations? How do you know that bubble sort has a best case scenario of O(n) and a worst case scenario of O(n^2)?
I made some basic Windows Forms application
[img]http://i.imgur.com/EY6R7fE.png[/img]
How can I make it so nobody can interact with browser and how can I make it move to x,y position after loading it (since it loads top left corner of the browser when I try to run it)? ((Control)webBrowser1).Enabled = false does not work and placing it in panel and trying to set panel to read only didn't work either.
[QUOTE=Zephyz;46764119]How do you know that bubble sort has a best case scenario of O(n) and a worst case scenario of O(n^2)?[/QUOTE]
I think this image explains bubble sort well:
[img]http://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif[/img]
If you consider a best-case example, where the list is already sorted, it will only check the list, thus taking O(N) time, and no further iterations occur. Otherwise check this example:
[url]http://en.wikipedia.org/wiki/Bubble_sort#Step-by-step_example[/url]
It's not bad for wikipedia.
What do you need this for? Bubble sort isn't that great.
[QUOTE=war_man333;46764706]I think this image explains bubble sort well:
[img]http://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif[/img]
If you consider a best-case example, where the list is already sorted, it will only check the list, thus taking O(N) time, and no further iterations occur. Otherwise check this example:
[url]http://en.wikipedia.org/wiki/Bubble_sort#Step-by-step_example[/url]
It's not bad for wikipedia.
What do you need this for? Bubble sort isn't that great.[/QUOTE]
Been reading Algorithms Unlocked by Thomas Cormen as I wanted to learn more about Algorithms. I got confused by Time Complexity in the book.
[QUOTE=Zephyz;46764763]Been reading Algorithms Unlocked by Thomas Cormen as I wanted to learn more about Algorithms. I got confused by Time Complexity in the book.[/QUOTE]
The worst case is n^2 because if the list is sorted in reverse you need n steps to get the first element to the last position, then n - 1 to get the next one to the second to last position, then n - 2... The sum of all those is n * (n + 1) / 2 which is (n^2) * (1/2) + n * (1/2). Now, since we're using the O notation, we only case about the highest polynomial power and we don't care about constants so you're just left with n^2.
Hey guys, silly question here. In C#, how would I relegate this follow snippet to a function, with the noted elements definable as parameters?
[code] StreamReader dataFile = new StreamReader(freelancerDataRoot + "Data/Equipment/Market_Misc.ini"); // The "Data/..." as parameter 1
while ((line = dataFile.ReadLine()) != null) {
string[] keyvalue = line.Split('=');
if (keyvalue.Length > 1) {
switch (keyvalue[0].Trim().ToLower()) {
case "base":
string bname = keyvalue[1].Trim().ToLower();
if (!bases.ContainsKey(bname)) bases[bname] = new Base(bname);
curBase = bases[bname];
break;
case "marketgood":
if (curBase == null) continue;
string[] values = keyvalue[1].Split(',');
BaseGood item = new BaseGood(values[0], values[5] == "0" ? true : false, Convert.ToSingle(values[6]));
curBase.goodsEquipment.Add(item); // The goodsEquipment (a HashSet) as parameter 2.
break;
}
}
}[/code]
Basically, the INI file and the correlating list are the only things changing, and it seems grossly inefficient to copy-paste this entire segment just to change those two. I would imagine there has to be a way to make it a function, but the fact that the HashSet is an element of objects created within the loop complicates that.
The only way I can think of doing it is passing the "goodsEquipment" et all in as a string, and use reflection to get the related HashSet, but that feels really dirty. I try to avoid reflection when I don't need to use it.
Does anyone know of any alternatives?
[editline]21st December 2014[/editline]
Big thanks to my friend Wearwolf, who pointed out delegates and how to use them.
I now have a function of the form:
[code]static void LoadDataMarket(string iniFile, Action<Base,BaseGood> addFunc) {
StreamReader dataFile = new StreamReader(iniFile);
...
BaseGood item = new BaseGood(values[0], values[5] == "0" ? true : false, Convert.ToSingle(values[6]));
addFunc(curBase,item);
}[/code]
Which I call like so:
[code] LoadMarketData(freelancerDataRoot + "Data/Equipment/Market_Misc.ini", (Base baseObj,BaseGood item) => baseObj.goodsEquipment.Add(item));
LoadMarketData(freelancerDataRoot + "Data/Equipment/Market_Commodities.ini", (Base baseObj, BaseGood item) => baseObj.goodsCommodities.Add(item));
LoadMarketData(freelancerDataRoot + "Data/Equipment/Market_Ships.ini", (Base baseObj, BaseGood item) => baseObj.goodsShips.Add(item));[/code]
Which is a much, [b]much[/b] cleaner solution than the nasty reflection I had temporarily implemented.
Why do I see big O notation written with theta sometimes and with a big O other times?
[QUOTE=proboardslol;46766649]Why do I see big O notation written with theta sometimes and with a big O other times?[/QUOTE]
Big Theta notation is a different thing: Big O notation gives an asymptotic [I]upper[/I] bound while Big Theta gives both an upper [I]and[/I] a lower bound.
[editline]22nd December 2014[/editline]
...Specifically, f belongs to Theta(g(n)) means "for sufficiently large n, k1 * g(n) <= f(n) <= k2 * g(n) where k1 <= k2 are some positive numbers.
[editline]22nd December 2014[/editline]
Well, that was confusing.
Anyway, I guess Theta(x) means O(x) ( k*x is the upper bound for some positive k) and Omega(x) ( k*x is the lower bound for some positive k)
[QUOTE=esalaka;46766760]Big Theta notation is a different thing: Big O notation gives an asymptotic [I]upper[/I] bound while Big Theta gives both an upper [I]and[/I] a lower bound.
[editline]22nd December 2014[/editline]
...Specifically, f belongs to Theta(g(n)) means "for sufficiently large n, k1 * g(n) <= f(n) <= k2 * g(n) where k1 <= k2 are some positive numbers.
[editline]22nd December 2014[/editline]
Well, that was confusing.
Anyway, I guess Theta(x) means O(x) ( k*x is the upper bound for some positive k) and Omega(x) ( k*x is the lower bound for some positive k)[/QUOTE]
Put more succintly:
O(F) -> The function has a worst-case run-time of F.
ω(F) -> The function has a best-case run-time of F.
Θ(F) -> The function is both O(F) and ω(F), or has a consistent run-time of F.
These run-times are, of course, asymptotic, meaning that F is always a magnitude. So O(n) means linear time (the time scales on a 1:1 ratio with the input size), O(n^2) means quadratic time, O(n^3) is cubic, etc etc.
Hello guys, I'm working on a space simulator thing (spawn planets/stars etc) and i'm trying to make realistic physics. I managed to make the gravitional force but i'm having some issues with orbiting a planet around the star. I tought that if i give it a "boost" a base velocity/speed it will stay in orbit but i can't really find a good formula for it. Any ideas? (And yes I would like to make it orbit by realistic physics so not a simple sine cosine math)
I haven't tested this but it should work.
You should be able to add a bit of velocity to the object until it orbits. It won't be a perfect circle but it should start orbiting at some point. If you want a perfect circle you want the force of gravity to equal the centripetal force of moving around the star in a circle.
For gravity the force is:
[code]F = G*m*m2 / r^2[/code]
And for centripetal force it's:
[code]F = m * v^2 / r[/code]
If you set those equal to each other you can solve for velocity. Set the smaller object's velocity to this and it should orbit in a perfect circle:
[code]G*m*m2 / r^2 = m * v^2 / r
v = sqrt(G * m2 / r)[/code]
You'll want to have a total momentum of 0 for the 2 bodies to keep them in the same local area. Set the star's velocity to this to balance the momentums:
[code]0 = m*v + m2*v2
v2 = -m/m2 * v[/code]
I've got the same things written in my code but it doesn't work
This part manages the gravitional force for all the objects(i copied only a part)
[code]
function Vector GetForce(PB_Object obj)
{
local float dist;
local Vector Force;
local Vector dir;
dir = Normal(obj.Location - Location);
dist = GetDist(obj);
Force = dir * obj.Weight * Weight / dist;
return Force;
}
Acc = Force / Weight;
Vel = Acc * delta;
SetLocation(Location + (Vel*delta));
[/code]
This is where i give a "boost" after i spawned the planet
[code]
Dir = PBP.Planets[0].Location - planet.Location;
Dir = Dir >> makerot(0,16384,0);
Dir = Normal(Dir);
vel = Sqrt(PBP.Planets[0].Weight / planet.GetDist(PBP.Planets[0]));
planet.Vel = vel * Dir;
[/code]
Some explanation : PBP.Planets[0] is always the sun and Force is equal to the return value of the GetForce function with the argument of the sun
also, GetDist :
[code]
function float GetDist(PB_Object obj)
{
local float dist;
dist = Sqrt(Square(obj.Location.X - Location.X) + Square(obj.Location.Y - Location.Y) + Square(obj.Location.Z - Location.Z));
return dist;
}
[/code]
[editline]22nd December 2014[/editline]
Oh the problem itself btw : The boost is too small, the acceleration is far greater so the planet crashes into the star
It looks like you aren't squaring the distance in the force calculation.
[code]Force = dir * obj.Weight * Weight / (dist * dist);[/code]
[QUOTE=Fredo;46771214]It looks like you aren't squaring the distance in the force calculation.
[code]Force = dir * obj.Weight * Weight / (dist * dist);[/code][/QUOTE]
Oh yeah sorry, I've just rewrote the code to make it clearer for you and forgot that. Even tho I added that again just now and tested it, it still doesn't work, the planet still crashes into the star :/
If by any faint chance you are not busy would you add me on Steam, it would be easier to talk there and i'd be able to show you the problem itself. Of course only if you are not busy.
[editline]22nd December 2014[/editline]
Oh sorry, my name is [vB] Gabeee xd
Ok I added you. Another thing, are you adding to the velocity each time? It looks like you're resetting it each time. You want something like this:
[code]force = getForce(...);
vel += force / mass * delta;
location += vel * delta;[/code]
You also want to update every object's velocity and then update every object's position (not update velocity and then position for each object)
[code]//Do this:
for each object:
update_velocity(obj);
for each object:
update_position(obj);
//Not this:
for each object:
update_velocity(obj);
update_position(obj);[/code]
This makes the state of all objects the same when each one calculates their new velocity.
Any good ideas to do player animation walking states?
Right now if you for example go diagonally towards the right bottom it will struggle between whether it should choose animation-state 'down' or 'right'
[code] void walk(std::string direction)
{
int timeElapsed = animationClock.getElapsedTime().asMilliseconds();
int walkSpeed = velocity*4;
std::string filename = "player_" + direction;
if(timeElapsed >= walkSpeed)
{
texture.loadFromFile(filename + "0.png");
animationClock.restart();
}
if(timeElapsed == walkSpeed/2)
{
texture.loadFromFile(filename + "1.png");
}
}
[/code]
Is it possible to let a method be called only 1 at a time?
How would you go about getting every two elements from an array, averaging them and then placing the result in another array in C++?
[QUOTE=Adam.GameDev;46778971]How would you go about getting every two elements from an array, averaging them and then placing the result in another array in C++?[/QUOTE]
[code]
int i = 0;
int j = 0;
while( i < arraysize ) {
int result = (array[i] + array[i+1])/2;
array_result[j] = result;
i = i + 2;
j++;
}
[/code]
[QUOTE=mastersrp;46779923][code]
int i = 0;
int j = 0;
while( i < arraysize ) {
int result = (array[i] + array[i+1])/2;
array_result[j] = result;
i = i + 2;
j++;
}
[/code][/QUOTE]
That compiles fine, but when I run it it returns "Segmentation fault"
[editline]23rd December 2014[/editline]
If I run it on Windows it just crashes
[QUOTE=Adam.GameDev;46780484]That compiles fine, but when I run it it returns "Segmentation fault"
[editline]23rd December 2014[/editline]
If I run it on Windows it just crashes[/QUOTE]
This is assuming you replace the C++-like pseudocode with your own actual code, and that the array of elements is of static of known size. It really shouldn't segfault. Keep in mind that the above code isn't complete. You'll still have to define your own arrays, include your headers, and shit like that. If this is a school assignment, then I've helped you more than you should need anyway.
[editline]23rd December 2014[/editline]
Wrote the following C program ( [b]NOT C++[/b] ) to demonstrate a simple implementation of what it sounds like you wanted
[code]
#include <stdio.h>
#include <stdlib.h>
int
main( int argc, char **argv ) {
int array[254];
for( int i = 0; i < 254; i++ ) {
array[i] = i;
}
int array_result[128];
int i = 0;
int j = 0;
while( i < 254 ) {
array_result[j] = (array[i] + array[i+1])/2;
i = i + 2;
j++;
}
for( int i = 0; i < 127; i++ ) {
printf( "%d: %d\n", i, array_result[i] );
}
return EXIT_SUCCESS;
}
[/code]
Compiles, links, and runs just fine with TinyCC, so it should work with GCC and Clang too.
This is in C however, [b]NOT[/b] C++.
I don't know why, but what you originally suggested works fine in C#. No segfaults, and it outputs correctly.
[editline]24th December 2014[/editline]
Okay it outputs correctly until the output is a decimal, because then it just removes the decimal. I'm using [code]System.Console.WriteLine("{0}", AverageArray[0]);[/code] to output.
Let's say you have a Car class, and other vehicle subclasses inherit from this Car class.
I want to get the wheel-size on all these subclasses, and they all have a public 'wheel' attribute.
Can I group these car subclasses somehow?
[code]vector<Car> cars;[/code]
This doesn't work, because when I push a subcar into this, it just initializes the wheel as a default value, instead of the instance-value I passed upon constuction of for example a motorcycle:
[code]Motorcycle mcycle(10);
vector<Car> cars;
cars.push_back(mcycle);[/code]
When checking the vector cars, the wheel size is default initialized. Why? What do I do?
I'm not actually making a program about cars and wheels, but it's to illustrate the problem.
Your vector stores Car objects. If you add a Motorcycle it will slice it.
Say a Car has a single int. sizeof(Car) == 4
A Motorcycle has an additional int. sizeof(Motorcycle) == 8.
Your vector only knows about Car. Every element in the vector is 4 bytes. If you add a Motorcycle it slices the Motorcycle part off and only adds the Car.
You can get around this by using pointers, since a pointer is always 8 bytes (or 4 for 32-bit machines).
[code]vector<Car*> cars;
cars.push_back(new Motorcycle(10));[/code]
You also need a virtual function to be able to call an overridden function in Motorcycle.
This is just an example, but you should use smart pointers instead of raw pointers.
Also, if your Motorcycle inherits from Car it doesn't need its own wheel member since it inherits it from Car. You can make it protected if you want subclasses to be able to access it.
[QUOTE=mastersrp;46779923][code]
int i = 0;
int j = 0;
while( i < arraysize ) {
int result = (array[i] + array[i+1])/2;
array_result[j] = result;
i = i + 2;
j++;
}
[/code][/QUOTE]
surely you could do
[code]for(int i = 0; i < arraysize; i = 2 * ++j){
//code
}[/code]
[QUOTE=proboardslol;46799269]surely you could do
[code]for(int i = 0; i < arraysize; i = 2 * ++j){
//code
}[/code][/QUOTE]
In what language is that even a thing?
[editline]26th December 2014[/editline]
sneaky basterd
Sorry, you need to Log In to post a reply to this thread.