Top down car game (game-maker) - game-maker

I'm new to programming on game-maker and programming in general. This is probably very easy but i'm unsure of how to go about things.
I am programming a simple top-down car game in which the car drives (forward) by it's self and is steered with the left and right mouse buttons. I attempted to get the car to drive on it's own with:
speed = 3
This, although making the car go forward, stopped the steering from working somehow and now the car rotates instead of actually turning around the corner.
How can i get the car to drive on it's own and still be able to turn the car?

You should not change image_angle, but direction instead. Image_angle is juste what you see, direction is the real physics direction.
Replace the code in your link by :
direction = direction + 2;
image_angle = direction;
Like this, you turn the car, and then align the image on the car orientation.

Related

How to apply physics force to speed ship vessel

i know buoyancy and apply ed it ; my ship is float on the water now. but i don't know how to apply force to control and navigate my speed boat ?
i'm using havok physics engine.
my code's like this
body->applyForce( stepInfo.m_deltaTime,forwardWorld, pointx );
pointx = my apply force point (-75,0,0); this point is a 3d point in back side of my ship
forwardWorld = force value and direction of it (100,0,0); apply 100Nm to back side of my ship
my pointx value is always static.
my forwardWorld values change every time for exam :
when i want to my ship go to front set it to (100,0,0)
when i want to my ship go to right i set it to (0,0,100)
when i want to my ship go to left i set it to (0,0,-100)
but this is'n a good way because my ship will drag and shift to left or right in upper speed
and this is mistake
please help me.
You didn't say what you need the model for. If it's a game then perhaps my advice will not satisfy you but if it's for some sort of engineering problem solving then I recommend building your own manoeuvring model. This may sound intimidating but in reality it boils down to solving three differential equations (roll, yaw and surge; you can also add sway depending on what you are interested in). You can easily solve them by integration using, say, Range-Kutta method.
Here is a paper giving a nice overview of what I'm talking about (there are lots on-line):
https://scl.snu.ac.kr/SCL_Research/data/research/science.pdf
You will need to find coefficients for your equations of motion. There are some in the paper I listed above, many more can be found on the web. For a start, I recommend going for the KRISO data, they are widely available and well presented in the literature.
Edit: I don't like MatLab but if you have access to it then you can solve your equations really easily by building a Simulink model.

2D Platform AI Jump

I'm working on a platform game at the moment. I have a problem with AI jump movement.
Path is already found with complete walk and jump points.
However jump action is problematic.
on example image:
AI should perform a jump at the red points and land on the next green point.
Do you know perform these curved jumps?
As huntsfromshadow said you can look at making a parabola for which the formula btw is
y = -x * x
You can also tweak the formula a bit with a few constants... best way to test this is using wolfram alpha http://www.wolframalpha.com
But I'll suggest different solution. Add a simple "jump simulation" which will look more realistic.
- add velocity to your entity
- in the moment of jump add large upwards impulse by modifying the velocity
- each frame add downwards velocity which suppose to be gravity
- each frame modify the position by adding velocity to it
Don't aim for the first green point accurately. Make the jump to feel right and if the creature overshoot some of the green points just make it walk towards the next one.
I would suggest looking at algebra and map the movement as a parabola.
Try different modifiers to the basic parabola equation of y = x (or y = -x as you are doing the upside down parabola).

Augmented Reality Demo

I'm trying to build an Augmented Reality Demonstration, like this iPhone App:
http://www.acrossair.com/acrossair_app_augmented_reality_nearesttube_london_for_iPhone_3GS.htm
However my geometry/math is a bit rusty nowadays.
This is what I know:
If i have my Android phone on the landscape mode (with the home button on the left), my z axis points to the direction I'm looking.
From the sensors of my phone i know what is the angle my z axis has with the North axis, let's call this angle theta.
If I have a vector from my current position to the point I want to show in my screen, i can calculate the angle this vector does with my z axis. Let's call this angle alpha.
So, based on the alpha angle I have a perception of where the point is, and I'm able to show it in the screen (like the Nearest Tube App).
This is the basic theory of a simple demonstration (of course it's nothing like the App, but it's the first step).
Can someone give me some lights on this matter?
[Update]
I've found this very interesting example, however I need to have the movement on both xx and yy axis. Any hints?
The basics are easy. You need the angle between your location and your destiny (arctangent), and the heading (from the digital compass in your phone). See this answer: Augmented Reality movement There is some objective-c code down there that you can read if you come from java.
What you want is a 3d-Space-Filling-Curve for example a hilbert-curve. That is a spatial index over 3 ccordinate. It is comparable to a octree. You want to store the object in that octree and do a depth-firat search on the coordinate you have recorded with your iphone as fixed coordinate probably the center of the screen. A octree subdivde the space continously in eigth directions and a 3d-Space-Filling-Curve is an hamiltonian path through the space which is like a fracta but it is clearly distinctable from the region of the octree. I use 2d-hilbert-curve to speed search in geospatial databases. Maybe you want to start with this first?

XNA Track rotated pixel positions

Im making a game in xna where a tank has to move over a landscape.
I need to be able find the bottom of the tank when it is rotated so I can make it move up and down as the player goes over the landscape.
for example if i have a sprite at with its top left corner at 400,300 and i rotate it around its center by 45 degrees around its center, how do i find the new locations of the bottom track.
Thanks
Thanks for the reply Langaurd.
I have looked at the article link before but didnt understand how it works.
Im making a 2d side scrolling game. As the player moves left and right, the tank has to also tilt to follow the contour of the terrain.
I have two vectors that store the back bottom of the track and one that stores the front bottom of the track.
I have tried
Vector2 backBottom = new Vector2(5, 25);
Vector2 frontBottom = new Vector2(5, 32);
backBottom = Vector2.Transform(backBottom+position, Matrix.CreateRotationZ(angle));
frontBottom = Vector2.Transform(frontBottom+position, Matrix.CreateRotationZ(angle));
but that gave me some strange values
Not 100% clear on exactly what it is you are trying to do. You mention a sprite, which is 2D, but your description is in 3D terms. If you are doing a 2D side view, then you can't tell the tank is rotated 45 degrees. If you are doing a 2D top-down view, then you shouldn't really care where the bottom of the tred is.
In any case, two suggestions. If you are die-hard on tracking rotated pixels, then read this article: 2D collision with Transformed Pixels from the creators.xna.com site. However I would recommend tracking vectors. Use two vectors to represent the track locations, and then use Vector2.Transform to rotate them with the tank. You could then use the vectors to check to see if the tracks have hit something, what angle they are at, ect.
You need to define a clearer orientation for you sprite. I would use a Front and Up Vector for the tank. Now you rotate both of them together with the angle your tank drives depending on the terrain. Lets say these vectors are at the center of your sprite. and your sprite is rotated, exactly like your up and front vectors. Now just multiply your Halfheight with -Up vector and you should have your local bottom center, add your tank position and you have your world bottom track position.
Important: Don't mix up a point, which can be expressed by a vector, or a real vector which has no position and only shows the direction. For directions its important to normalize the vector.
Sorry for the vague answer but you question is a little bit vague too.

XNA Collision Detection - Vector2.Reflect - Help Calculating the Normal of a Circle Sprite - C#

I'm having trouble wrapping my mind around how to calculate the normal for a moving circle in a 2d space. I've gotten as far as that I'm suppose to calculate the Normal of the Velocity(Directional Speed) of the object, but that's where my college algebra mind over-heats, any I'm working with to 2d Circles that I have the centerpoint, radius, velocity, and position.
Ultimately I'm wanting to use the Vector2.Reflect Method to get a bit more realistic physics out of this exercise.
thanks ahead of time.
EDIT: Added some code trying out suggestion(with no avail), probably misunderstanding the suggestion. Here I'm using a basketball and a baseball, hence base and basket. I also have Position, and Velocity which is being added to position to create the movement.
if ((Vector2.Distance(baseMid, basketMid)) < baseRadius + basketRadius)
{
Vector2 baseNorm = basketMid - baseMid;
baseNorm.Normalize();
Vector2 basketNorm = baseMid - basketMid;
basketNorm.Normalize();
baseVelocity = Vector2.Reflect(baseVelocity, baseNorm);
basketVelocity = Vector2.Reflect(basketVelocity, basketNorm);
}
basePos.Y += baseVelocity.Y;
basePos.X += baseVelocity.X;
basketPos.Y += basketVelocity.Y;
basketPos.X += basketVelocity.X;
basketMid = new Vector2((basketballTex.Width / 2 + basketPos.X), (basketballTex.Height / 2 + basketPos.Y));
baseMid = new Vector2((baseballTex.Width / 2 + basePos.X), (baseballTex.Height / 2 + basePos.Y));
First the reflection. If I'm reading your code right, the second argument to Vector2.Reflect is a normal to a surface. A level floor has a normal of (0,1), and a ball with velocity (4,-3) hits it and flies away with velocity (4,3). Is that right? If that's not right then we'll have to change the body of the if statement. (Note that you can save some cycles by setting basketNorm = -baseNorm.)
Now the physics. As written, when the two balls collide, each bounces off as if it had hit a glass wall tangent to both spheres, and that's not realistic. Imagine playing pool: a fast red ball hits a stationary blue ball dead center. Does the red ball rebound and leave the blue ball where it was? No, the blue ball gets knocked away and the red ball loses most of its speed (all, in the perfect case). How about a cannonball and a golf ball, both moving at the same speed but in opposite directions, colliding head-on. Will they both bounce equally? No, the cannonball will continue, barely noticing the impact, but the golf ball will reverse direction and fly away faster than it came.
To understand these collisions you have to understand momentum (and if you want collisions that aren't perfectly elastic, like when beanbags collide, you also have to understand energy). A basic physics textbook will cover this in an early chapter. If you just want to be able to simulate these things, use the center-of-mass frame:
Vector2 CMVelocity = (basket.Mass*basket.Velocity + base.Mass*base.Velocity)/(basket.Mass + base.Mass);
baseVelocity -= CMVelocity;
baseVelocity = Vector2.Reflect(baseVelocity, baseNorm);
baseVelocity += CMVelocity;
basketVelocity -= CMVelocity;
basketVelocity = Vector2.Reflect(basketVelocity, basketNorm);
basketVelocity += CMVelocity;
The normal of a circle at a given point on its edge is going to be the direction from its center to that point. Assuming that you're working with collisions of circles here, then one easy "shorthand" way to work this out would be that at the time of collision (when the circles are touching), the following will hold true:
Let A be the center of one circle and B the center of the other. The normal for circle A will be normalize(B-A) and the normal for circle B will be normalize(A-B). This is true because the point where they touch will always be colinear with the centers of the two circles.
Caveat: I'm not going to assume that this is completely correct. Physics are not my specialty.
Movement has no effect on a normal. Typically, a normal is just a normalized (length 1) vector indicating a direction, typically the direction that a poly faces on a 3d object.
What I think you want to do is find the collision normal between two circles, yes? If so, one of the cool properties of spheres is that if you find the distance between the centers of them, you can normalize that to get the normal of the sphere.
What seems correct for 2d physics is that you take the velocity * mass (energy) of a sphere, and multiply that by the normalized vector to the other sphere. Add the result to the destination sphere's energy, subtract it from the original sphere's energy, and divide each, individually, by mass to get the resulting velocity. If the other sphere is moving, do the same in reverse. You can probably simplify the math down from there, of course, but it's late and I don't feel like doing it :)
If both spheres are moving, repeat the process for the other sphere (though you could probably simplify that equation to get some more efficient math).
This is just back-of-the-napkin math, but it seems to give the correct results. And, hey, I once derived Euler angles on my own, so sometimes my back-of-the-napkin math actually works out.
This also assumes perfectly elastic collisions.
If I'm incorrect, I'd be happy to find out where :)

Resources