Jump to content
  • Advertisement

JMatser

Member
  • Content Count

    6
  • Joined

  • Last visited

Community Reputation

103 Neutral

About JMatser

  • Rank
    Newbie
  1. JMatser

    Entity Screen bound checking

    Multiplying the value by 1000 does not makes a whole lot of sense, this would give you the time in Micro seconds, but would still not give you any extra precision. It is important to know what an bool, char, short, integer, float and double can store, and more importantly what it cannot store. an integer can store values from -2,147,483,648 to 2,147,483,647. It can however only store real numbers (1, 2, 42, 1337) but no rational numbers (3.1415, 4/3, ?[font="Arial"], etc), you can uses floats or doubles for that.[/font] [font="Arial"]For thinks like time, and distance floating point values are more natural, for graphics on a screen real numbers are more useful as it makes no sense to set half a pixel to a certain colour.[/font]
  2. JMatser

    Entity Screen bound checking

    Most likely this. I would advice to change both the position and the velocity as well as the deltaTime to floating point values. Right before you're doing the drawing, cast the (float) position to an integer. This way the the object stays moving even if the speed is <1. It might not look very nice, as sometimes the objects will move one pixel and the next frame it will move two or none at all. But at least the system keeps working. Also, I noticed that the deltaTime have a value between [8..10]. What do these values mean?? The only plausible meaning I can think if would be milliseconds; meaning that the game would run at around 100~125 fps. A much more accurate approach would be using the real time value (for example in seconds) as a float or double.
  3. I noticed that all the switch statements do not have a default label. Maybe this would be a nice opportunity to introduce the concept of an assertion (something which is also missing from the project) in combination with the default label? The assertions might also come in handy when for example loading an image. Do you really want to continue your code when you cannot load an image?
  4. JMatser

    Entity Screen bound checking

    Your dt is of the type Uint32, which I suppose means unsigned int (32 bits). Normally a deltaTime value (or any time value for that matter) is represented with a floating point value (float or double). In addition to that, you're applying deltaTime twice, once for the velocity and the once more when you're applying the velocity. This is probably not what you intended to do? What you're essentially doing is this: x -= xVel * dt *dt; meaning that your code is still not frame-rate independent. In fact it will go faster with a lower frame rate (as dt gets larger with a lower frame rate). One other small thing, assuming you're using c++, using "this->x" is not wrong but its generally accepted to just use "x". make sure to use something that works for you.
  5. JMatser

    Noob Question About Classes

    I have taken the liberty to slightly update your code. where you to compile and run this you would notice that it now works, but more importantly demonstrates the usage of the constructor and destructor. Please take a good look at the changes I made and try to understand how they work. I might have introduced some concepts which are yet unknown (such as "scope" and "pointers") to you but feel free to ask questions. If you are following some kind of tutorial or book you'll get to them eventually. I have also removed the "system( "cls );" functions as they make it harder to understand what the program is doing. Commonly classes are used in the same way that other data containers such as ints, floats and chars are used: they are used to store data. class member functions are often used to manipulate this data or to pass this data to another part of the code. You will also notice that I have used a different way to layout and indent the code. Make sure you use something that is easy to understand for you. #include <iostream> #include <string.h> #include <stdlib.h> using namespace std; int Warning() { //system("cls"); cout << "\n\n\n"; cout << "Invalid Input!" << endl; system("pause"); return 0; } class Island { private: char input[10]; public: Island() { cout << "[NOTICE] - The Island is now being created" << endl; Docks(); } ~Island() { cout << "[NOTICE] - The Island is now being destroyed" << endl; } int Docks() { //system("cls"); cout << "\n\n\n"; cout << "Welcome to the docks. Where would you like to go?" << endl; cout << "1 - Market" << endl; cout << "2 - Set Sail!" << endl; cin >> input; //check if we go to the market if( strcmp(input, "Market") == 0 || strcmp( input, "1" ) == 0 ) { Market(); } else if( strcmp( input, "Set Sail" ) == 0 || strcmp( input, "2" ) == 0) //check if we're going to set sail { cout << "The sails are lowered and away you go!" << endl; } else { Warning(); Docks(); } return 0; } int Market() { //system( "cls" ); cout << "\n\n\n"; cout << "We're now in the market" << endl; //add some option to go back to the docks? return 0; } }; int main() { cout << "Creating an island!" << endl; Island island1; { Island island2; } cout << "\n[NOTICE] second island is no more...\n" << endl; Island* pIsland = new Island(); delete pIsland; cout << "\n[NOTICE] the island created with by using a pointer is no more...\n" << endl; system("pause"); }
  6. Well, you know where your player is on the screen (playerX and PlayerY), you know where the mouse was clicked (mouseX and mouseY), and you have a general velocity for the bullet (float BulletVelocity) right? You have 2 different methods you can use to set the bullet's X and Y velocity: #1, The Trigonometry method (recommended), you set the position of the bullet at the player, and set velocity of the bullet to be: // get the angle from the player to the mouse clicked position float Angle = atan2(mouseX - playerX, mouseY - playerY); VelocityX = cos(Angle)*BulletVelocity; VelocityY = sin(Angle)*BulletVelocity; Or, you can do it the conceptual/geometric method, by determining how far the mouse is from the player, and the time it should take for the bullet to get there: // Get the distance of the mouse from the player (A^2 + B^2 = C^2) float distanceX = mouseX - playerX; float distanceY = mouseY - playerY; float distance = sqrt(distanceX*DistanceX + DistanceY*DistanceY); // get time it would take the bullet to get there travelling at BulletVelocity (assuming velocity is pixels/second) float Time = distance/BulletVelocity; // now give the X and Y speeds for the bullet to get to that point: VelocityX = distanceX/Time; VelocityY = distance/Time; [/quote] in the first example what is the atan2 supposed to mean. thanks for your reply. [/quote] Atan2 is a mathematical function, just like sin, cos, and tan, which composes an x and y offset into an angle (usually in radians). For example atan2(1, 0) returns 0 which equals 0 degrees, atan2( 0, 1) returns pi/2 which equals 90 degrees and atan( -1, 0) returns pi which equals 180 degrees. Atan2 can obviously be used for non integer numbers too, but the above is to (hopefully) illustrate how atan2 works. That being said. You problem is usually solved by using vectors. If you are not familiar with them, now would be a good time to look them up! If you were to represent the player position P as a 2-dimensional vector (Player.x, player.y) and the mouse position M as a 2d vector (mouse.x, mouse.y), you could compute the difference between these two by subtracting the mouse position from the player position. This creates a new vector D where D = M - P. You can then normalize this vector to get the directional vector for your bullet. After that you can scale this normalized vector by some amount based on the speed of the velocity. you can then apply this velocity to your bullet to get your bullet moving. [attachment=5278:FireAway.png] Obviously the above version also works! I hope this helps
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!