Is it possible to simulate a candle for instance in cocos2d. So the scene will be black and then when the candle appears it will lighten up the room in a realistic way.
If possible, how would I achieve that effect? Any redirections, guidance is welcome.
Thanks.
Please note, that I don't want light reflection algorithms, as this is only for simple use. I just need it for a game where the players life is the abillity to see the room.
You should check out raycasting. If you set up Cocos2d to use Box2d, you can use Box2d's raycasting method to achieve this effect. You can make the walls in your room box2d rigid-bodies, and then raycast from your candle origin to a number of points on a circle around it. If the raycast intersects a wall, mark the location of the intersection. Then at the end you can fill in the area with light that is inside a polygon created by all of the raycast intersection points.
Related
I am new to Open GL. I am trying to navigate through a tubular structure, which was developed using Open GL primitives.I have done almost that. But now my problem is to stop the camera when it is hitting on the tube boundaries. Can anybody give suggestions to solve this problem?
OpenGL is not a scene graph or a game engine. It just draws points, lines or triangles, one at a time, as pixels to a framebuffer. That's it.
Anything that goes beyond that you have to implement yourself. You want collision detection? OpenGL won't do it for you.¹ You want a scene? You'll have to implement the data structures for that yourself.
In the case of a camera in a tube the boundary condition is simple: If the camera's distance from the tube centerline (most easily expressed in cylindrical coordinates) approaches the tube radius, stop the movement in that direction.
¹ Not entirely true: You can use OpenGL to implement things like collision detection, but this is not straightforward and usually less efficient than doing it with special purpose collision detection code.
In a game I am programming in Cocos2D and Box2D, I have a sprite that is in the shape of a hill, and I am trying to attach a body to it so it can respond to physics. My idea is to use a rotated rectangle shape so that it aligns with the slope, but I need to be able to move the sprite so that the body stays appropriately lined up. I can make the body and sprite work together in the case of a circle, but I can't figure out how to make it work for the case of the rotated rectangle. Could someone explain the code to do this?
Yes I agree to user1634707. CocosBuilder will be one potential choice.
What I'm trying to do is draw a gun for a fps game I am making but it always seems to be off. I know I am supposed to draw it last with depth test disabled. But i just can't seem to get it to follow along with the view of the camera. Assume we are just drawing a triangle to represent the gun right now, with the base being at the players end. I have access to player position and the point where he is looking (used for gluLookAt) and I also have access pitch and yaw. And ideas on what to do?
I'm not an expert but I would try something akin to the following for drawing the gun:
#If not already in modelview mode
glMatrixMode(GL_MODELVIEW)
#Push the current modelview matrix onto the stack, leaving us working with a copy
glPushMatrix()
#Translate forward to where we want to draw the gun
glTranslatef(0,0,dist)
#Draw the gun starting from this position, translation may need to account for offset so that it is centered correctly
gun.draw()
#Get rid of our modified modelview matrix and return to the original so that the camera is in the correct positon
glPopMatrix()
The best guide I have found for understanding OpenGL geometry is http://www.songho.ca/opengl/gl_transform.html
Good luck! :)
I'm making a 3D FPS with OpenGL and here is the basics of how it works. The game is a 3D array of cubes. I know the location of the player's current cube, aswell as the camera x,y,z and I know the x, y, z rotation of the camera too. Right now I just make a square around the player and render this and then add distant fog. The problem though, is that I'm still rendering everything that the player is in back of. How could I selectively only render what the player sees, not render everything within an X radius as Iam doing now.
Thanks
You are talking about frustum culling, if i get you right. I suggest that you take a look at this tutorial. They provide nice demos and explain everything in detail.
This sounds like you need to look into culling concepts.
Are the cubes rooms of a maze through which the player navigates? If so, and assuming the rooms are static over the course of the game, you could use a BSP tree to traverse the scene in order of depth, stopping when you pass the player.
I am currently working on designing my first FPS game using JOGL. (Java bindings for OpenGL).
So far I have been able to generate the 'world' (a series of cubes), and a player model. I have the collision detection between the player and the cubes working great.
Now I am trying to add in the guns. I have the gun models drawn correctly and loading onto player model. The first gun I'm trying to implement is a laser gun, which shoots and instantaneous line-of-sight laser at whatever you're aiming at. Before I work on implementing the enemy models, I would like to get the collision detection between the laser and the walls working.
My laser, currently, is drawn by a series of small cubes, one after the other. The first cube is drawn at the end of the players gun, then it draws continuously from there. The idea was to continue drawing the cubes of the laser until a collision was detected with something, namely the cubes in the world.
I know the locations of the cubes in the world. The problem is that I have to call glMatrixPush to draw my character model. The laser is then drawn within this modelview. Meaning that I have lost my old coordinate system - so I'm drawing the world in one system, then the lazer in another. Within this player matrix, I have go call glRotate and glTranslate several times, in order to sync everything up with the way the camera is rotating. The lazer is then built by translating along the z-axis of this new system.
My problem is that through all of these transformations, I no longer have any idea where my laser exists in the map coordinate system, primarily due to the rotations involving the camera.
Does anyone know of a method - or have any ideas, for how to solve this problem? I believe I need a way to convert the new coordinates of the laser into the old coordinates of the map, but I'm not sure how to go about undoing all of the transformations that have been done to it. There may also be some functionality provided by OpenGL to handle this sort of problem that I'm just unaware of.
You shouldn't be considering the laser as a spacial child of the character that fires it. Once its been fired, the laser is an entity of its own, so you should render as follows:
glPushMatrix(viewMatrix);
glPushMatrix(playerMatrix);
DrawPlayer();
glPopMatrix();
glPushMatrix(laserMatrix);
DrawLaser();
glPopMatrix();
glPopMatrix();
Also, be sure that you don't mix your rendering transformation logic with the game logic. You should always store the world-space position of your objects to be able to test for intersections regardless of your current OpenGL matrix stack.
Remember to be careful with spacial parent/child relationships. In practice, they aren't that frequent. For more information, google about the problems of scene graphs.
The point that was being made in the first answer is that you should never depend on the matrix to position the object in the first place. You should be keeping track of the position and rotation of the laser before you even think about drawing it. Then you use the translate and rotate commands to put it where you know it should be.
You're trying to do things backwards, and yes, that does mean you'll have to do the matrix math, and OpenGL doesn't keep track of that because the ModelView matrix is the ONLY thing that OpenGL does keep track of in regards to object positions. OpenGL has no concept of "world space" or "camera space". There is only the matrix that all input is multiplied by. It's elegantly simple... but in some cases I do prefer the way DirectX has a a separate view matrix and model matrix.
So, if you don't know where an object is located without matrix math, then I would consider that a fundamental design problem. If you don't need to know the object position, then matrix-transform to your hearts content, but if you do need it's position, start with the position.
(pretty much what the first answer says, just in a different way...)