[QUOTE=garychencool;39303642]quick question: where should I place main()? Top or bottom of code?
[editline]21st January 2013[/editline]
Im pretty sure i should place it at the bottom[/QUOTE]
I've always had my main method at the top. It seems logical since that's where the program starts, no?
I was able to write an FPS calculator and an FPS limiter, and it calculated the correct approx FPS when I added a framelimit.
But after I added an OpenGL context and started "drawing", the FPS calculator started returning odd numbers like "2.15912" and "0.00215" though the numbers seem to be constant depending on the focus of the window. The actual FPS seems to be more like 60 fps, but the calculated FPS is lower than it should be.
Heres my code to calculate fps:
[code]
float realtime = getTimeInternal(); // get the time using glfwGetTime
storedtime = realtime - starttime; // get the time from the start of the program.
delta = storedtime - lasttime; // get the delta from the last frame
lasttime = storedtime; // store the time in "lasttime"
fps -= fpsSamples[0] / NUM_FPS_SAMPLES; // FPS is an integer, we remove the value of first item in the arrays of fpsSamples.
memmove( &fpsSamples[0], &fpsSamples[1], sizeof(fpsSamples) - sizeof(float) ); // we shift the array over to the left by one.
if ( delta == 0 ) { // there is now a free slot in the array at the end
fpsSamples[NUM_FPS_SAMPLES-1] = 0; // if delta is zero then we fill the free slot with zero
} else {
fpsSamples[NUM_FPS_SAMPLES-1] = 1.0f / (delta * 1000); // if it is not zero, we fill it with the reciprocal of delta * 1000
fps += (1.0f / (delta * 1000)) / NUM_FPS_SAMPLES; // we also add this calculation into FPS
} // the "fps" integer should theoretically hold the average of all the samples[/code]
To get the internal time I use:
[code]glfwGetTime()[/code]
[editline]z[/editline]
I added comments to my code.
[QUOTE=robmaister12;39301899]There's a workaround by using System.Windows.Forms.Mouse.Position... [url]http://www.opentk.com/node/640[/url]
If you're trying to set up an FPS-type camera, I would recommend you grab the latest OpenTK from SVN and use the new input API (OpenTK.Input.Mouse.GetState()) that gives you unaccelerated mouse deltas, which is perfect for an FPS.
As for hiding the cursor, I'm not sure if this was part of the OpenTK 1.0 release or added later, but the GameWindow.CursorVisible property works for me on both Windows and Arch Linux.
Also with the new input API, there's a lot of stuff still missing (especially on the Mac side) and some stuff that's buggy. If you want to help out with it, try using this version of OpenTK on GitHub and report any errors you notice: [url]https://github.com/andykorth/opentk[/url][/QUOTE]
It was just to build Monogame but im just going to stick with SFML
Hey guys, I'm still an extreme beginner at coding, but I am using C# with XNA to create a small game right now and I am running into a problem that won't go away.
I have 3 classes, one which is Game1 (main), Ninjas (player class), and AI (AI class). I'm trying to code the AI currently and have derived the player's main entity into the AI class to read the X position so that the AI will walk towards the player when needed to.
The problem is is that when I derive the redNinja class into my AI class and read the X position, I'm getting an "object was never assigned to, and will always have it's default value null" error.
Here's my code for the AI class:
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Ninja_DM
{
class AI
{
Texture2D blueNinja;
Ninjas redNinja;
float timer = 0f;
float interval = 130f;
int currentFrame = 0;
int spriteSpeed = 2;
int spriteWidth = 32;
int spriteHeight = 28;
public Rectangle sourceRect;
Vector2 position_b;
Vector2 origin;
public Vector2 Position
{
get { return position_b; }
set { position_b = value; }
}
public Vector2 Origin
{
get { return origin; }
set { origin = value; }
}
public Texture2D Texture
{
get { return blueNinja; }
set { blueNinja = value; }
}
public Rectangle SourceRect
{
get { return sourceRect; }
set { sourceRect = value; }
}
public AI(Texture2D texture, int currentFrame, int spriteWidth, int spriteHeight)
{
this.blueNinja = texture;
this.currentFrame = currentFrame;
this.spriteWidth = spriteWidth;
this.spriteHeight = spriteHeight;
}
public void AIMovement(GameTime gameTime)
{
sourceRect = new Rectangle(30 * currentFrame, 0, 30, 37);
if (position_b.X > redNinja.Position.X)
{
AnimateLeftAI(gameTime);
if (position_b.X < 20)
{
position_b.X += spriteSpeed;
}
}
if (position_b.X < redNinja.Position.X)
{
AnimateRightAI(gameTime);
if (position_b.X < 1100)
{
position_b.X += spriteSpeed;
}
}
}
public void AnimateLeftAI(GameTime gameTime)
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (timer > interval)
{
currentFrame++;
if (currentFrame > 4)
{
currentFrame = 3;
}
timer = 0f;
}
}
public void AnimateRightAI(GameTime gameTime)
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (timer > interval)
{
currentFrame++;
if (currentFrame > 4)
{
currentFrame = 3;
}
timer = 0f;
}
}
}
}[/code]
Thanks.
[QUOTE=garychencool;39303642]quick question: where should I place main()? Top or bottom of code?
[editline]21st January 2013[/editline]
Im pretty sure i should place it at the bottom[/QUOTE]
Personal preference, but I think everyone puts it at the top.
[QUOTE=garychencool;39303642]quick question: where should I place main()? Top or bottom of code?
[editline]21st January 2013[/editline]
Im pretty sure i should place it at the bottom[/QUOTE]
Since I usually keep it in it's own file that's not an issue.
I'm writing a little program that allows you to build a plane in 2D and it then let's you glide it with very accurate flight physics. I have everything working, but I've run into a problem.
My plane class has a function called calculate forces, in the first part it loops through all the wings on the plane and calculates the torque they exert on changing the attitude based on their distance from the center of mass.
The wings calculate the lift they produce with a lift equation that goes up exponentially with velocity and linearly with attitude, at the end of the function it turns all the forces into accelerations.
The problem happens when the plane's velocity goes past some spazz point where the computer overcompensates a tiny amount of angular velocity, resulting in an even bigger reaction which then starts oscillating with the result that the plane's tail flutters till it slows down enough to glide smoothly again.
Here's a sample of the code:
vector2df TotalForce = vector2df(0,0);
vector3df TotalTorque = vector3df(0,0,0);
for (int i = 0; i < 2;i++)
{
double SweepMag = AngularVelocity*PI/180*Vlerke[i].Position;
vector2df SweepVel = -vector2df(1,0).rotateBy(Attitude-90,vector2df(0,0))*SweepMag;
////////////////////
vector2df ActualVel = (Velocity+(SweepVel));
TotalForce += Vlerke[i].Force(Attitude,ActualVel);
vector3df ForceArm = vector3df(Vlerke[i].Position,0,0);
vector3df Force;
TotalTorque += ForceArm.crossProduct(Force);
}
Also, I'm sorry I don't know how to do that fancy ass code block thing.
[QUOTE=mkp;39300873][thumb]http://i.imgur.com/34Gne.jpg[/thumb]
I'm having a problem with unity and shaders :<
I tried to make foam with the particle system. Something is wrong with the shader (AlphaBlend). As I read it is because unity calculates the distance and decides what to render first. In this case it renders parts of the level before the foam particle.
Anyone got an idea on how to fix this?[/QUOTE]
Use the Transparent [url=http://docs.unity3d.com/Documentation/Components/SL-SubshaderTags.html]renderqueue in your shader[/url] and the Default queue in your level geometry
[QUOTE=garychencool;39303642]quick question: where should I place main()? Top or bottom of code?
[editline]21st January 2013[/editline]
Im pretty sure i should place it at the bottom[/QUOTE]
Does it really matter if it's on line 5 or line 15?
Am I doing anythng wrong here?
[cpp]
b2BodyDef myBodyDef;
myBodyDef.type = b2_kinematicBody; //this will be a dynamic body
myBodyDef.position = Box2dUC::ToSimUnits(50, 288); //set the starting position
myBodyDef.angle = 0;
body = world->CreateBody(&myBodyDef);
b2Vec2 vertices[12];
vertices[0] = Box2dUC::ToSimUnits(25,-5);
vertices[1] = Box2dUC::ToSimUnits(24.5, -15.0);
vertices[2] = Box2dUC::ToSimUnits(23.5, -25.0);
vertices[3] = Box2dUC::ToSimUnits(22.5, -35.0);
vertices[4] = Box2dUC::ToSimUnits(20, -55);
vertices[5] = Box2dUC::ToSimUnits(-5, -52);
vertices[6] = Box2dUC::ToSimUnits(-5, 52);
vertices[7] = Box2dUC::ToSimUnits(20, 55);
vertices[8] = Box2dUC::ToSimUnits(22.5, 35.0);
vertices[9] = Box2dUC::ToSimUnits(23.5, 25.0);
vertices[10] = Box2dUC::ToSimUnits(24.5, 15.0);
vertices[11] = Box2dUC::ToSimUnits(25,5);
b2PolygonShape polygon;
polygon.Set(vertices, 12);
b2FixtureDef boxFixtureDef;
boxFixtureDef.shape = &polygon;
boxFixtureDef.density = 1;
body->CreateFixture(&boxFixtureDef);[/cpp]
I keep getting a segfault when setting the verticies to the polygon
Doesn't look like you initialized it, it may not be required, and I'm stupid, but usually objects need to be initialized.
Why isn't this doing anything? Shouldn't it at least open a blank window for 2 seconds?
[cpp]#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
int main(int argc, char **argv)
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 1)
{
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
SDL_Surface *screen;
screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
if (screen == NULL)
{
fprintf(stderr, "Unable to set video: %s\n", SDL_GetError());
exit(1);
}
SDL_Delay(2000);
SDL_FreeSurface(screen);
return 0;
}[/cpp]
[QUOTE=Topgamer7;39308082]Doesn't look like you initialized it, it may not be required, and I'm stupid, but usually objects need to be initialized.[/QUOTE]
Turns out the max amount of verticies you can have is 8
[editline]21st January 2013[/editline]
Im just wondering, what is the most efficient way of creating a floor in box2d?
I dont want to use lots of small blocks as it will make things snag on them, and they dont recomment making anything bigger than 10meters
[QUOTE=account;39308178]Why isn't this doing anything? Shouldn't it at least open a blank window for 2 seconds?
[cpp]#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
int main(int argc, char **argv)
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 1)
{
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
SDL_Surface *screen;
screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
if (screen == NULL)
{
fprintf(stderr, "Unable to set video: %s\n", SDL_GetError());
exit(1);
}
SDL_Delay(2000);
SDL_FreeSurface(screen);
return 0;
}[/cpp][/QUOTE]
SDL_Init returns 0 on success
Can someone help me out with maths?
To draw stuff I use a vertexarray, so im trying to draw balls.
what I do is create a vector from the radius, rotate that by 30 degrees 12 times and all 12 times I add the rotated vectr + the ball center to the vertexarray
[cpp]
case b2Shape::e_circle:
{
b2CircleShape* circleShape = (b2CircleShape*)f->GetShape();
sf::Vector2f prad(Box2dUC::ToDisplayUnits(circleShape->m_radius) + 50,0);
sf::Vector2f rotatedVert;
for(int i = 0; i < 12; i++)
{
int ang = 30*i;
std::cout << ang << " _ ";
rotatedVert.x = prad.x * cos(ang) - prad.y * sin(ang);
rotatedVert.y = prad.y * cos(ang) + prad.x * sin(ang);
sf::Vertex vert( pos + rotatedVert, shapeColor );
std::cout << vert.position.x << ":" << vert.position.y << std::endl;
va2.append(vert);
}
sf::Vertex vert( pos + sf::Vector2f(Box2dUC::ToDisplayUnits(circleShape->m_radius) + 50,0), shapeColor );
va2.append(vert);
std::cout << std::endl;
}
break;[/cpp]
but this is what gets drawn
[IMG]http://i.imgur.com/NY6ZNi8.png[/IMG]
and thiss is the cout
[code]
0 _ 582:288
30 _ 522.798:218.838
60 _ 445.331:266.663
90 _ 480.635:350.58
120 _ 568.993:328.643
150 _ 560.948:237.959
180 _ 470.108:231.919
210 _ 450.129:320.74
240 _ 534.805:354.181
270 _ 580.907:275.677
300 _ 510.453:218.017
330 _ 442.616:278.733
[/code]
[QUOTE=ThePuska;39308954]SDL_Init returns 0 on success[/QUOTE]
No, it wasn't even printing the error message - and I changed it and it still doesn't do anything.
I'm linking with mingw32, SDLmain, and SDL, by the way
Im getting a annoying problem with collisions on SFML.
Im using a tilemap (32px) with a bi dimensional vector. The player moves around the map in any velocity
between 1 and 20.
The problem is when I jump, when he lands on the ground, I check if the position behind him (in the vector) is "solid", if his
feets is in the middle of the "solid" block, I just move him back to the position he should be multiplied by 32, but I dont know
why in one frame he is drawed in the middle of the block.
thats the code of the function who do that.
[CODE]
struct unit {
short int dirx; short int diry;
int posx; int posy;
unsigned short int tilex; unsigned short int tiley;
unsigned short int tamtilex; unsigned short int tamtiley;
unsigned short int passo;
unsigned short int velox;
float veloy;
sf::Sprite sprite;
unsigned short int tipo;
sf::IntRect rect;
};
float GRAVIDADE = 1;
void atualizaPlayer(struct unit *T) {
int x,y,z;
x=int(T->posx/32); y=int(T->posy/32);
if (T->diry==0) {
if ((mapa[x][y+1].id!=28) || (mapa[x+1][y+1].id!=28)) {
T->diry=1;
}
}
if (T->diry != 0) {
T->veloy=T->veloy+(GRAVIDADE*T->diry);
if(T->veloy>20) { T->veloy=20; }
if(T->veloy < 1) { T->diry=1; }
}
x=int(T->posx/32); y=int(T->posy/32);
if (T->diry > 0) {
if (mapa[x][y+1].id==28) {
T->diry=0;
T->posy=y*32;
T->veloy=20;
}
}
// Atualiza as posições no mapa
T->posy=T->posy+(int(T->veloy)*T->diry);
T->posx=T->posx+(T->dirx*T->velox);
x=T->posx+TELAX;
y=T->posy+TELAY;
T->sprite.setPosition(x,y);
T->rect.top = y;
T->rect.left = x;
}
[/CODE]
I lost a day trying to figure that, this is driving me crazy.
If someone has a better way to handle with gravity and collisions, I appreciate.
[QUOTE=WeltEnSTurm;39303370]Are those globals defined as static or extern?[/QUOTE]
tried both
also tried to just put them there, didn't work
[editline]22nd January 2013[/editline]
[QUOTE=ief014;39303683]If you're using staticly linked libs, you need to add SFML_STATIC in the preprocesser defines.[/QUOTE]
nope, not using static libs.
You know I get all excited about writing really basic programs then come here or go look at the AI thread and get all depressed.
But then when I successfully program something I'm not sure if I want to be an Aerospace Engineer, Robotics Coder, or a mix of the two :/
Or maybe game development. I am confuse
Okay guys please answer this.
I want to start out in a easy language, and learn 2-3 languages more, and then move on to Lua.
The reason for not starting with Lua now, is because I've talked to a lot of Lua coders, and they all said they started with something else, before moving to Lua.
One guy said he did C# first, then he scripted in SourcePawn, and THEN he started Lua.
What do you guys recommend? :)
Thanks!
[QUOTE=Dario z;39313455]Okay guys please answer this.
I want to start out in a easy language, and learn 2-3 languages more, and then move on to Lua.
The reason for not starting with Lua now, is because I've talked to a lot of Lua coders, and they all said they started with something else, before moving to Lua.
One guy said he did C# first, then he scripted in SourcePawn, and THEN he started Lua.
What do you guys recommend? :)
Thanks![/QUOTE]
Just start with Lua if that's your goal. I'm sure they switched languages because of what they were working with. If you want to work with something that uses Lua just learn Lua.
I mean like, the reason for why I also want to start with something else before is because, I can get into the habit of programming, and then I also know how to program.
IMO Lua is a great first language
I started with Lua and it was fine
Something is wrong with my project...
The coordinate system is messed up but I am not sure why. I am guessing the player or something uses an inverted coordinate system and the objects don't, but using glScalef to invert it didn't help any.
As you can see in the image, in the console it is outputting the walls position. in the game ( top left ) it is showing the player position. They are both the same, but for some reason the wall is being drawn over there, at -10,0,-10. The obvious solution to this would just be to do this with the drawing of the walls:
glTranslatef(-position.x,-position.y,-position.z);
But I feel that I will run into problems with this in the future. I have to somehow figure out how to fix the players coordinates :L
[IMG]http://i.imgur.com/sgNtHgW.png?1[/IMG]
[QUOTE=Duskling;39314352][IMG]http://i.imgur.com/sgNtHgW.png?1[/IMG][/QUOTE]
Are all the italicized members static?
[QUOTE=Mozartkugeln;39314612]Are all the italicized members static?[/QUOTE]
[I]ply[/I] and [I]dungeon[/I] are static yes.
[QUOTE=WeltEnSTurm;39314173]I started with Lua and it was fine[/QUOTE]
So did I but it took me years to move from Lua and VB.
[QUOTE=account;39308178]Why isn't this doing anything? Shouldn't it at least open a blank window for 2 seconds?
[cpp]#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
int main(int argc, char **argv)
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 1)
{
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
SDL_Surface *screen;
screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
if (screen == NULL)
{
fprintf(stderr, "Unable to set video: %s\n", SDL_GetError());
exit(1);
}
SDL_Delay(2000);
SDL_FreeSurface(screen);
return 0;
}[/cpp][/QUOTE]
I may be wrong, but SDL requires you to have a main() function declared fully, like in this example:
[cpp]
#include <SDL.h>
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Delay(2000);
SDL_Quit();
return 0;
}
[/cpp]
You can also try to see if this one doesn't work then error is definitely somewhere else.
Oh and when SDL_Init fails it returns a value that is < 0.
[QUOTE=vombatus;39315295]I may be wrong, but SDL requires you to have a main() function declared fully, like in this example:
[cpp]
#include <SDL.h>
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Delay(2000);
SDL_Quit();
return 0;
}
[/cpp]
You can also try to see if this one doesn't work then error is definitely somewhere else.
Oh and when SDL_Init fails it returns a value that is < 0.[/QUOTE]
Thanks!
Sorry, you need to Log In to post a reply to this thread.