How do I change a objects sprite in gamemaker based on its direction on movement in Game Maker? - game-maker

I am making a maze game in game maker v1.4.1763, and need some help changing a objects sprite based on its direction of movement. I am currently have an object following the mouse using game makers drag and drop 'move towards" action. I have sprites that are animated for the object from 4 different perspectives(front, back, left, and right.) The object can move in any direction (It doesn't have to stick to the cardinal directions.)
I want the object to change its sprite based on witch way it is "mostly" moving. I don't really know how to program using game makers language, but I know how to get it to execute code. I have tried almost everything using the drag and drop coding and this can only be accomplished using some code. If someone could help me come up with that code, that would be awesome.

Simplest way:
if direction > 45 and direction <= 135 and sprite_index != spr_up
sprite_index = spr_up;
else if direction > 135 and direction <= 225 and sprite_index != spr_left
sprite_index = spr_left;
else if direction > 225 and direction <= 315 and sprite_index != spr_down
sprite_index = spr_down;
else if sprite_index != spr_right
sprite_index = spr_right;
Where spr_left, spr_right, spr_up, spr_down is your sprites.
You can place this code in Step End event.

Related

Top down car game (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.

How to auto-create the sprite and move them vertically in the room? GameMaker developer

Guys I want to do this without GML I guess it'senter image description here easy to do it.
You can see in the image I have uploaded. there are three sprites and I want them to enter the room randomly at specific location.
Guys I'm new to game maker this is my first game.
I'm making a shooting game for Android. You can refer the image for clear idea about the game.
Things I did are: I made 3 sprite red, yellow, green
What I want to do is:
I want the red yellow green sprite moving in vertically and the player gonna shoot them.
I want the sprite to get created automatically at random location(at the top I mean) and move in vertical direction and should not overlap each other.
How should I do it?
Well, there's a lot of different way to do this. The simplest one is to create an object, assign it a sprite, spawn it at a random y and assign it a vspeed.
in the object create event :
sprite_index = your_sprite;
x = 0;
y = random(room_width);
vspeed = the_speed_you_want;
I suppose you want to be able to shoot at the sprites, so it is better to make an object than to simply draw them.
If you don't want them to overlap, you can define 3 possible starting positions and randomly chose one with the choose() function.

Capturing last location's object in a variable?

Right now I've got about a 10x10 grid of squares that the player can move 1 square at a time on.
When they hop to a square, I need an animation to play based on the sprite_index of the square they're jumping to and the one they just came from.
I've got the "jumping to" one sorted out.
in a collision event between the player and the square (other here being the square):
with(other){
if sprite_index = sGreenH {
instance_create(x,y,oGreenPlayerAni)
(also is there a better way to do the above? instead of spawning it ontop of what's there can I delete it/replace it THEN put something?)
So now I'm trying to get an animation to play from the square the character is leaving. I can do that with the player collision w/ square :
xx = xprevious and yy = yprevious
instance_create(xx,yy, someanimation)
problem there is that I can't customize which animation plays. There are 4 possible colors of animations to use for 4 diff color squares.
so I tried with the collision event in my square with the player making a variable like
if sprite_index = sGreen {
global.previousColor = 1
for each of my colors. and then in my player's collision event with the square again I have
if global.previousColor = 1 {
instance_create(xx,yy, oGreenHollowAni)
And then I get an error when I move.
Code square colliding with player (player on a square) : http://puu.sh/n9zCY/2f226b6d3c.png
Code player colliding with square : http://puu.sh/n9zK6/deac1a09f5.png
Error : http://puu.sh/n9zPj/ea84a9a943.png
I am not sure if I understand your question right. As far as I can see, you do always create a new instance when the player is moving?
If so, that is not good. When you create your 10x10 grid of squares I guess you create an array, in which you put the information which color square gets displayed, fe. array[x][y] = color.green ... you could then create an enum an define green = 1, blue = 2 ...
This array would be a global one.
From the player class you then check on which square you are currently and if you move you check on which square you will be. With these informations you can define the animation.
let me know if this was what you meant.
eric

Arduino algorithm - Compass direction

I've been working on a hobby project, and I need your help now! I'm using Arduino with a Uno board. My project is a moving vehicle, 4 wheels. I have a compass, that reads a x= 0-360. However, I want to be able to write "Kurs:" 0-360, and the vehicle will rotate so the vehicles' front is faced at that compass direction. I also want it to take the shortest way to that dorection. How can I write a algorithm in Arduino to do that?
If you can read the compass and have the desired heading in mind, the pseudo-code is as simple as:
# Start by defaulting to left rotation.
direction = left
rotation = readCompass() - desiredHeading
# Handle case where current heading was _less_ than desired heading.
if rotation < 0:
rotation = rotation + 360
if rotation > 180:
direction = right
rotation = 360 -rotation
# Now rotate <rotation> degrees in <direction> direction

How to Find global position of objects in a rotating scene THREE.JS

I am working on a 3D mesh manipulator using this : http://leapmotion.com. So far, I have been able manipulate the points just fine, by 'grabbing' and moving them, however I now want to be able to rotate the mesh and work on the opposite face. What I have done is add an extra object that is called 'rotatable' as Shown below:
scene=new THREE.Scene();
camera = new THREE.PerspectiveCamera(70,window.innerWidth/window.innerHeight,1,8000)
renderer=new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1, maxLights:5 } )
//This is the 'Mesh Scene'
rotatable = new THREE.Object3D()
scene.add(rotatable)
//Mesh we are altering
var material = new THREE.MeshNormalMaterial()
material.side=2
var geom = new THREE.SphereGeometry(200,10,10);
var sphere = new THREE.Mesh(geom, material)
rotatable.add(sphere)
I am then trying to change the vertices of this sphere, but to do so I need to do a 'collision test' in order to see if the vertex is being 'grabbed' This involves check the vertex position and see if it coincides with one of your finger position (psuedoCode below)
if(finger.x == vertex.x && finger.y == vertex.y && finger.z == vertex.z){
vertex.grabbed = true
}
This works fine when the rotatable's rotation is zero, however when it starts to rotate, the collision test will still be testing for the unrotated vertex position (which makes sense). My question is how to find the position of the vertex in its 'scene / global' position. The only way I can think of doing this so far is to calculate the rotation of the 'rotatable' and use this vector to calculate the new vertex position.
I know nothing about math, so this may not be the way to go, and even if it is I will have to struggle through it so hard that I won't ever know if I'm just doing the math incorrectly, or this isn't the way I should go about calculating it. Obviously I'm willing to go through this work, but just want to make sure this is the way to do it, rather then an other simpler method.
If there are any other questions about the code, please let me know, and Thanks in advance for your time!
Isaac
To get the world position of a vertex specified in local coordinates, apply the object's world transform to the vertex like so:
vertex.applyMatrix4( object.matrixWorld );
(I am not familiar with leapmotion, so hopefully it does not impact this answer.)
Tip: maxLights is no longer required. And it is best to avoid material.side = 2. Use material.side = THREE.DoubleSide instead.
You can find the constants here: https://github.com/mrdoob/three.js/blob/master/src/Three.js
three.js r.55

Resources