I am making a videogame in Blender Game Engline, and want to be able to adjust the direction gravity pulls objects. I can change scene Z gravity in a script, but that's one-dimensional along the Z axis.
I would be fine with the ability to set X, Y, and Z gravity, or an easy way to make everything rotate at the same time around the origin (or an arbitrary point).
I could also build a system if I could have a plane exert gravity or a force field and have it rotate around the center at a set distance (the entire gameworld is encased in a sphere).
Basically, I want to be able to, from a python script, cause a force on all dynamic objects, automatically and without . How can I do this?
I think you're looking for bge.constraints.setGravity(x, y, z)
To set the gravity.
and this one: bge.logic.getCurrentScene().gravity to get the actual gravity. According to this you should calculate the ideal rotation for your objects
Related
I am using vb.net for making a game. I have a ball (not actually, but supposedly) in the center of the screen. It moves towards another object, which has a circular surface and is supposed to reflect back in correct direction and angle.
Many games are based on the bouncing back of an object (most of them are based on advanced languages). But, I am not really able to make out how.
So, I tried to look at some things, like getting an angle from where it started by calculating the pixels from the initial point and sending it that many pixels in the opposite direction. A code which I tried for this is:
Dim x1, y1 as integer
x1 = object.left
y1 = object.top
This stores the object's co-ordinates at the starting, when it is in the center of the screen. While the following code tries to achieve what actually is required:
Dim a, b as integer
a = x1-object.left
b = x2-object.top
I have got the number of pixels it has moved, but I have no clue how to reflect back in the same angle using this, and whether this technique will even work or not.
So, basically what I want is:
Now, two situations arise:
How to reflect object from a plain surface?
How to reflect it from circular surface?
Bouncing a ball off of another ball works exactly as bouncing it off a flat surface. As the balls only touch at one point, there is no difference to the moving ball if that point is part of a curved or a flat surface.
The ball acts just as if it was bouncing off of a flat plane, which is the tangent of the reflecting objects curve at that point (and also the tangent of the moving objects curve):
The reflection movement is the angle mirrored around a line that is perpendicular to the plane, and goes through the center of the ball. (When the reflecting object is a ball, the line goes through the center of that too, which may help when you want to calculate it.)
Let's say I have a rectangle-shaped object. I want to move it along a path. Is it possible to position this object according to not only one point, but two points on it? For example, the point A on the object is at 125,220, in this case I want point B to be at 140,235.This way I want to set the direction of the object.
In Objective-c (and I assume in other languages too) when we say "Position of a graphical object" we think of only one point, which is usually the bottom-left corner. But positioning
an object according to only that point will just redraw the object with the lower left corner in another point, and the rest part will be determined according to the height and the width of the object, which does not do what I want.
EDIT:
As you can see (and probably it's what you naturally expect) the object will move as a box from one point to another, because there's only one point determining it's position. You ask why I need a different thing. Because I have a timer and a curved path. Each time the timer ticks I need my object to be at a different location(the next position in an array of dumped points). So, instead of adding to X and Y coordinates, I explicitly tell the object to be at certain place. This way I want to achieve normal movement of my object along the curved path. When the front part of the object moves to some point, I need the rear part to move to a certain point as well.
I finally found a way to do do it. I have to rotate the object according the prior and the next points. So, assume there are points A,B,C,D,E,F,G,H on the path that the object will travel along. If the car is at point D, to calculate the rotation angle I do the following:
myObject.rotation=-atan((D.y-C.y)/(D.x-C.x))/3.141592*180;
As you can see it's just maths. A fine tuning can be applied to get a better, smoother rotation. Here for instance I subtract the Y of the previous position from the Y of the current position, then I do the same thing for X and then I get the minus arctangance of their ratio. But you can do
-atan((E.y-C.y)/(E.x-C.x))/3.141592*180;
Choosing the right positions to subtract their x and y coordinates will result in the right and smooth rotation.
I think you can guess that 3.141592 is M_PI;
I'm trying to make a little archer game, and the problem I'm having has to do with 2 pixels in particular, I'll call them _arm and _arrow. When a real archer is pulling back an arrow, he doesn't immediately pull the arrow back as far as his strength allows him, the arrow takes a little bit of time to be pulled back.
The _arm's angle is equal to the vector from a point to where the user touched on the screen. The rotation is perfect, so the _arm is good. The _arrow needs to be on the same line as _arrow, they are 1 pixel wide each so it looks as though the _arrow is exactly on top of the _arm.
I tried to decrement from the x/y coordinates based on a variable that changes with time, and I set the _arrow's location equal to the _arm's location, and tried to make it look like the _arrow was being pulled back. however, if you rotated, the x/y would mess up because it is not proportional on the x and y axis, so basically _arrow will either be slightly above the arm or slightly below it depending on the angle of the vector, based on touch.
How could I used the x/y position of _arm and the vector of touch to make the arrow appear as though it was being pulled back by a small amount, yet keep the arrow on top of the _arm sprite so that it's position would be similar to the arm, but slightly off yet still on top of the _arm pixel at all times. If you need anymore info, just leave a comment.
I'm not sure I've fully understood, but I'll have a go at answering anyway:
To make the arrow move and rotate to the same place as the consider adding the arrow as a child of the arm. You can still render it behind if you like by making its z is less than one: [arm addChild:arrow z:-1]
To then make the arrow move away from the arm as the bow is drawn, you then just set the position of the arrow with respect to the arm.
The problem I do see with this solution however is that this grouping of the sprites may be a little unusual after the arrow leaves the bow. Here you probably don't want the arrow to be a child of the arm as the coordinate systems are no longer related.
Even though they're sure what I "suggested would have solved [the] problem" here is the
Poster's solution
I had to get the x and y coords of the arm based of angle, then I got the sin/cos of a number that was based of the same angle as the arm and subtraced from that.
I'm using a 3d engine and need to translate between 3d world space and 2d screen space using perspective projection, so I can place 2d text labels on items in 3d space.
I've seen a few posts of various answers to this problem but they seem to use components I don't have.
I have a Camera object, and can only set it's current position and lookat position, it cannot roll. The camera is moving along a path and certain target object may appear in it's view then disappear.
I have only the following values
lookat position
position
vertical FOV
Z far
Z near
and obviously the position of the target object.
Can anyone please give me an algorithm that will do this using just these components?
Many thanks.
all graphics engines use matrices to transform between different coordinats systems. Indeed OpenGL and DirectX uses them, because they are the standard way.
Cameras usually construct the matrices using the parameters you have:
view matrix (transform the world to position in a way you look at it from the camera position), it uses lookat position and camera position (also the up vector which usually is 0,1,0)
projection matrix (transforms from 3D coordinates to 2D Coordinates), it uses the fov, near, far and aspect.
You could find information of how to construct the matrices in internet searching for the opengl functions that create them:
gluLookat creates a viewmatrix
gluPerspective: creates the projection matrix
But I cant imagine an engine that doesnt allow you to get these matrices, because I can ensure you they are somewhere, the engine is using it.
Once you have those matrices, you multiply them, to get the viewprojeciton matrix. This matrix transform from World coordinates to Screen Coordinates. So just multiply the matrix with the position you want to know (in vector 4 format, being the 4ยบ component 1.0).
But wait, the result will be in homogeneous coordinates, you need to divide X,Y,Z of the resulting vector by W, and then you have the position in Normalized screen coordinates (0 means the center, 1 means right, -1 means left, etc).
From here it is easy to transform multiplying by width and height.
I have some slides explaining all this here: https://docs.google.com/presentation/d/13crrSCPonJcxAjGaS5HJOat3MpE0lmEtqxeVr4tVLDs/present?slide=id.i0
Good luck :)
P.S: when you work with 3D it is really important to understand the three matrices (model, view and projection), otherwise you will stumble every time.
so I can place 2d text labels on items
in 3d space
Have you looked up "billboard" techniques? Sometimes just knowing the right term to search under is all you need. This refers to polygons (typically rectangles) that always face the camera, regardless of camera position or orientation.
I do not really understand the way I'm suppose to render a side-scroller? How do I know what to render when my character move? What kind of positionning should I use for the characters?
I hope my question is clear
The easiest way i've found to do it is have a characterX and characterY variable [integer or float, whatever you want] Then have a cameraX and cameraY variable. Every object in the scene is drawn at theObjectX-cameraX, theObjectY-cameraY...
CameraX/CameraY are tweened by a similar-to-midpoint formula so eventually they'll reach playerx/playery[Cx = (Cx*99+Px)/100] ... yeah
By doing this, every object moves in the stage's space, and is transformed only on render [saving you from headaches]
Use a matrix to define a camera reference frame.
Use space partitioning to split up your level into screens/windows.
Think of your player sprite as any other entity, like enemies and interactive objects.
Now what you want is the abstraction of a camera. You can define a camera as a 3x3 matrix with this layout:
[rotX_X, rotY_X, 0]
[rotX_Y, rotY_Y, 0]
[transX, transY, 1]
The 2x2 sub-matrix in the top-left corner is a rotation matrix. transX and transY defines the translation part, i.e the origin. You also get scaling for free. Just simply scale the rotation part with a scalar, and you have yourself a zoom.
For this to work properly with rotation, your sprites need to be polygons/primitives, say like triangles or quads; you can't just apply the matrix to the positions of the sprites when drawing. If you don't need rotation, just transforming the center point will work fine.
If you want the camera to follow the player, use the player's position as the camera origin. That is the translation vector [transX, transY]
So how do you apply the matrix to entity positions and model vertices? You do a vector-matrix multiplication.
v' = vM^-1, where v' is the new vector, v is the old vector, and M^-1 is the matrix inverse. A camera needs to be an inverse transform because it defines a local coordinate system. An analogy could be: If you are in front of me and I turn left from my reference frame, I am turning your right. This applies to all affine and linear transformations, like scaling, rotation and translation.
Split up your level into sub-parts so you can cull objects and scenery which does not need to be rendered. Your viewport is of a certain size/resolution. Only render scenery and entities which intersect with your viewport. Instead of checking each and every entity against the viewport bounds, assign each entity to a certain sub-screen and test the bounds of the sub-screen against the viewport and camera bounds. If your divide your levels into parts which are the same size as your viewport, then the maximum number of screens visible
at any particular time is:
2 if your camera only scrolls left and right.
4 if your camera scrolls left, right, up and down.
4 if your camera scrolls in any direction, and additionally can be rotated.
A screen-change is an event you can use to activate entities belonging to that screen. That could be enemies, background animations, doors or whatever you like.
If this is your first foray into writing a side-scroller, I'd suggest considering using an already existing game engine (like Construct or Gamemaker or XNA or whatever fits your experience level) so you don't have to worry about what order to render things and how to make it all work. Mess with that a bit--probably exploring a few of them--to get a feel for how they do things then venture out to your own once you've gotten used to it.
Not that there's anything wrong with baptism by fire but it can get pretty overwhelming in my opinion.