GameMaker Studio 2 - Object changes to different sprite without programming (GML) - gml

While programming my game, I came up on a significant issue. I am trying to create a tactical game, and I created grid cell objects for it. In the object's step event, it has the following code to change the sprite index when the mouse hovers over it.
if (position_meeting(mouse_x,mouse_y,OBJ_GridCell))
{
sprite_index = 1;
}
else
{
sprite_index = 0;
}
This is what the intended sprite looks like (set in object properties):
But then when I run the game, it changes to another sprite I have, and the code functions like it should. It just is the wrong sprite.
I have tried searching, but I can't find anything that matches my issue. Help?

Looks like you want to pick a different frame inside a sprite, so you should use image_index instead of sprite_index.
image_index decided which frame in the sprite will be picked, where sprite_index let you choose a different sprite.

Related

three.js - no update of scene lighting when light moving

I have a simple THREE.js scene where I want a point light to move dynamically with the camera. The movement works well but the lighting in the scene is not updated immediately. When I change camera position (and thus light position) it takes some time (approx. 2-3 seconds) till the lighting in the scene renders correctly in respect of the new light position. I have already updated the matrix/matrixWorld of the lights in every frame. Is a further update needed? How to tell Three.js that light position has changed?
There is no specific way needed to tell THREE.js lightsources have changed their positions. Here it is important how to make the update of transformation matrices. I have a lightGroup (instance of Object3D) which is the parent node for all lightsources in my scene. Updating the lightGroup transformation according to the camera works with this setup:
if (camera !== undefined)
{
lightGroup.position.x = camera.position.x;
lightGroup.position.y = camera.position.y;
lightGroup.position.z = camera.position.z;
lightGroup.lookAt(camera.target);
lightGroup.updateMatrix();
lightGroup.updateMatrixWorld();
}
The update of the transformation matrix is the point here. Another approach is to hang the camera into the THREE.js scenegraph and attach lights as its children, if you want to avoid a seperate lightGroup node.
By the way: Specific update of THREE.js scene is only needed if you change the number of lightsources in a scene. See https://github.com/mrdoob/three.js/wiki/Updates at Materials.
I know it is old post, however maybe someone will need other solutions.
To move lights dynamicly with camera You can just attach them to camera.
directionalLight = new THREE.DirectionalLight(0xffffff,0.6);
directionalLight.position.set(0,20,0);
directionalLight.rotation.set(+20/180*Math.PI,0,0);
directionalLight.castShadow = true;
camera.add(directionalLight);
scene.add(camera);

Box2d - mouse joint set to a specific point on an object

I have a Box2d iphone app (using cocos2d) that has a mouse joint in it, and whenever you use the mouse joint, it applies the force to the object at whatever point you clicked. I was wondering if there is a way to always have the force be applied to a certain point on the object, for example the center of it. The reason is that when my shape is a box, for instance, when I pick it up using the mouse joint, it spins so that wherever I clicked ends up toward the top. I would like to always pick up the object from, say the center of the object. I've tried amping up the inertial dampening, but wasn't quite getting the effect I wanted. Here is my current code:
b2Vec2 locationWorld = b2Vec2(newtouch.x/PTM_RATIO, newtouch.y/PTM_RATIO);
b2MouseJointDef md;
md.bodyA = physicsLayer.groundBody;
md.bodyB = touchedObject.body;
md.target = locationWorld;
md.maxForce = 2000;
b2World* w = [physicsLayer getWorld];
mouseJoint = (b2MouseJoint*)w->CreateJoint(&md);
touchedObject is the object I'm moving and it stores it's body as a property. This code works fine as is, it just has the nasty spinning.
You could change the target point to be the center of mass of the box:
md.target = touchedObject.body->GetWorldCenter();
This will make the joint pull the center of the box to where your finger is, instead of the point you touched. It will not stop it from spinning if it's already spinning though, so you might still want to use some angular damping on the body to make it feel a bit more like the user is actually touching it, rather than holding a pin stuck through the center of it :)

How to transition an isometric object from one tile to another without overlapping other tiles

I've created an isometric environment, all in Javascript and HTML5 (2D Canvas), which mostly works fine. The problem I'm facing is to do with having different height tiles and then sorting the indexes of objects on the tiles (in this case, while moving between two tiles side-by-side).
For example, one object may be behind a tile in front of it because the height of the tile it is on is -1. The solution I came up with was to draw each object of a tile directly after drawing the tile, starting at 0,0 and drawing each row and column from there.
This works well until I need to transition an object between two tiles. At this point, either the object must use an intermediate tile (this is what is implemented in the images below) or the tile will overlap the object as the tile is drawn after the object. Using an intermediate tile also gives a problem where the fence object on the same "row" gets draw over because the cube is using much higher z-index from the tile at 1,3 (this is slightly visible in image 1).
http://i.stack.imgur.com/PQJ0H.png
http://i.stack.imgur.com/DupM7.png
I think the tried and tested way of drawing isometric environments is just to have 1 layer for tiles and 1 layer for objects and then objects can never be behind tiles, but this is just a limitation that I don't want to adhere to.
So my question is, when drawing the entire environment from top to bottom (or any other way if it makes it possible), drawing each tile and it's objects in turn, is there a clever way to defer drawing of an object or create an array of objects to be drawn in the correct order? Has anyone else encountered similar issues and has anyone else found any solutions for this?
All help much appreciated.
An example of my tiling code:
// each column
for(y=0; y<totalColumns; y++){
// each row
for(x=0; x<totalRows; x++){
tile = tiles[y][x];
// draw tile
drawTile(tile);
objects = objects[y][x];
// draw objects for that tile
drawObjects(objects);
}
}
Edit:
One solution I have thought of (after reading the question back to myself) is to loop through all the tiles, get an array of tile heights, sort that, then do the traditional drawing. Like so:
var layers = [];
for(var y=0; y<cols; y++){
for(var x=0; x<rows; x++){
tile = tiles[y][x];
if(!layers.indexOf(tile.height)) layers.push(tile.height);
}
}
// sort layers
layers.sort(/*function here to sort layers*/);
for(l=0; l<layers.length; l++){
// draw tiles for this layer
// draw objects for this layer
}
Any other solutions possible?
Your doing the exact right thing in your solution. You want to do this for everything that doesn't have an alpha.
As far as other solutions go I would recommend looking at using an insertion sort rather than rebuilding the list time and time again. In your case there is very little change so just update the ones that need it.

Animating a marker pin on Bing Maps for Metro apps

I have a Bing Map control and I want to animate the location of a pin drawn on the map.
Ideally I'd want to write code like this:
// Image myPin = some Image on the map layer;
var sb = new Storyboard();
var duration = DurationHelper.FromTimeSpan(System.TimeSpan.FromMilliseconds(1000));
sb.Duration = duration;
LocationAnimation ani = new LocationAnimation();
ani.To = new Location(33.3, 11.1); // destination lat, lng
ani.Duration = duration;
sb.Children.Add(ani);
Storyboard.SetTarget(ani, myPin);
Storyboard.SetTargetProperty(ani, "MapLayer.Position");
Resources.Add("moveMyPin", sb);
sb.Begin();
Of course there is no LocationAnimation class in the framework. There are a couple similar: PointAnimation works with the Point type, while DoubleAnimation works with individual double values.
I assumed I have to implement my Timeline-derived class but the documentation I found so far is not helpful in this regard and I don't know where to start.
How to animate a property for which there is no *Animation class ready? Am I missing something?
Update: I think I can do something like
// let aniX and aniY be two DoubleAnimation objects
StoryBoard.SetTargetProperty(aniX, "(MapLayer.Position).Latitude")
StoryBoard.SetTargetProperty(aniY, "(MapLayer.Position).Longitude")
but now I have another problem as I don't see how to animate the attached property and always get an exception like "Cannot resolve TargetProperty (MapLayer.Position).Latitude on specified object."
So I guess the question now becomes: how to specify a property path for an attached property?
Further update: I may be on a wrong lead with the "SetTargetProperty" thing as that would make a dependent animation (see link in comments). Could there be a better way to animate a marker pin to a coordinate destination?
Update 3: as Jared pointed out I could try to animate RenderTransform, or use a transition to achieve my result. I had already gone this way and it doesn't seem to be my solution. This is what I found out:
Using RepositionThemeAnimation, it seems I have to give the new pixel position of the pin. This is problematic for a couple reasons: first, I know the lat/lng destination of the pin, but would have to work out the projection (as far as I can tell there is no public interface to work with projection). Secondly the projection itself can change as the map zooms/pans. The pixel destination at the time of the end of the animation could not be the same as the initial pixel destination. In my scenario it's a major problem as it's very likely the map is moving while the animation is occurring.
Using RepositionThemeTransition all the weird pixel/projection problems went away. It's the only way so far I was able to see the animation I expected. I just set the transition and set the new lat/lng position of the tag (via MapLayer.SetPosition). It has two major problems though. I can't set the duration (nor the easing functions) for the animation. And most importantly I can see no way to execute code when the pin gets to its final position.
Attached properties can be animated, but you must enclose their names in parenthesis. See an example here:
http://www.charlespetzold.com/blog/2007/09/080231.html
However, I believe this only works for setting the whole value (Location) and potentially not sub properties (Latitude and Longitude).
I'm wondering if you can provide more information about what you're trying to do. Is this a temporary animation? i.e. are you wanting the pin to animate in on entrance or departure? If so you can just do an animation on the RenderTransform X and Y instead. You won't actually be changing the location of the pin but you can use the RenderTransform to temporarily change where it's being drawn.
Even better, you might be able to get away with the free animations now built into the framework. You don't even need a storyboard to use them, you just set them right on the element like this:
<Button Content="Transitioning Button">
<Button.Transitions>
<TransitionCollection>
<EntranceThemeTransition/>
</TransitionCollection>
</Button.Transitions>
</Button>
For more examples of the animation library and even video previews for many of the animations, see:
http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh452703.aspx
Okay, man, your problem isn't related to Bing Maps. It's related to dependency properties that can't be animated UNLESS you specify that they can. Here's how: http://blog.jerrynixon.com/2012/06/windows-8-animated-pie-slice.html
That link in the other answer with Charles' site is not up to date.

cocoa collision detection question

I get the concept of the NSIntersectionRect for collision detection but I can't seem to think of how to implement it for my project. It's nothing fancy, click a button and a view subclass is called and places a circle in the window at a random location. Click within the that view and the circle pulses (this makes it the active view). If you have an active view, clicking anywhere outside it (but not on another circle) will move that view to the click point.
I'm using [activeView animator setFrame: NSMakeRect(x, y, w, h)] to move the active view. Can I use this for collision detection or do I have to go with CABasicAnimation? Basically what I am looking to do is detect collisions with other circles (no physics needed at this point, just stop the movement) and/or with the bounds of the app window.
If someone could nudge me in the right direction (tutorial link, code snippet) I'd appreciate it.
Edit: Based on the well detailed answer below I need to be a bit more clear. I'm confused on where to implement the collision detection. The animator method of a view class is one line of code. How would I iterate through every static circle on the screen to run a collision check? Which is why I am wondering first, if I need to go with CoreAnimation, OpenGL or something like Chipmunk and then if I could get a nudge or assist that would be great.
Later .. in answer to your recent questions:
Which is why I am wondering first, if I need to go with CoreAnimation, OpenGL or something like Chipmunk and then if I could get a nudge or assist that would be great.
Answer - you absolutely definitely do not need OpenGL :) Next, you definitely do not need a physics library like Box2D or Chipmunk ... you could go that way if you wanted to, but, it would be a huge amount of unnecessary work. To be clear: until you are totally familiar with using DrawRect, things like Chipmunk are useless for you anyway, so just forget that.
Core Animation will not really help you. To be clear, you possibly want to interrupt an animation as it is happening. Is that correct?
I'm confused on where to implement the collision detection. The animator method of a view class is one line of code. How would I iterate through every static circle on the screen to run a collision check?
Bad news... if you actually want to interrupt the animation, if there is a collision, forget about Core Animation. Core Animation will let you send it from A to B as "one unit" of animation. It will NOT let you stop it in the middle (in case of a collision). So, that's that.
To be clear, that is what you want to do right? You set the circle in motion, and IF it hits something along the way, you want it to stop. Is this correct? if so, completely forget about Core Animation and throw away any work you've done so far.
You are going to have to dive in to "real" programming (whatever that means) and start using drawRect. Are you up for it?!
At this point I might just mention: consider buying a copy of Corona (it's like $100 -- I'm sure the demo is free). You can do everything you are describing in, literally, five minutes using Corona. (1/10th the time taken to write this post??) I always recommend this to people who are iPhone Curious. If you don't really want to spend 6 to 18 months becoming a gun iPhone programmer - just click to Corona for iPhone and in a fraction of the time it's taken you to use Stack Overflow, you can have your circles bouncing wildly on the iPhone screen.
So, failing that, you're gonna have to get to work and learn how to launch a timer (NSTimer) and use drawRect in a UIView.
Craate a new class called MyFirstView (.h and .m file) that is a subclass of UIView (not UIViewController, which is for wimps!). You'll need a drawRect method in MyFirstView and proceed from there!
Original answer..
I'm not sure I understand what you are saying, but to be clear:
You want to detect a collision between two circles. In your case, all the circles are the same diameter. Is that correct?
If so, fortunately it is very easy to do.
Write a routine that gets the distance between two CGPoints. (If you don't know how to do that I included it below.)
Next step, you must know the DIAMETER of your circles. Let's say it is 50.0 for the example.
Next, here is a routine that checks if two circles are colliding:
static inline bool areTwoCirclesColliding( CGPoint aa, CGPoint bb )
{
return ( distanceBetweenTwoCGPoints(aa,bb) < 50.0 );
}
(Note... if you are new to Objective C, note that the above is totally valid code. Simply paste it in at the top of your file. OK?)
Finally, you just have to check all your circles, one against the other, to see if any are colliding.
If you have a simple fixed number of circles, let's say three or so, just write out all the lines of code to check if any of the combinations are colliding, hence:
areTwoCirclesColliding(a,b)
areTwoCirclesColliding(a,c)
areTwoCirclesColliding(b,c)
If you have an array or some sort of list of circles, you just have to go through them all and check that each one is not touching any other. In pseudocode it might be something like this...
for n in 1 to numberCircles
oneCircle = circles[n]
for m in n+1 to numberCircles
anotherCircle = circles[m]
if ( areTwoCirclesColliding(oneCircle,anotherCircle) )
.. break, you found a collision
Alternately you could write it like this, makes no difference..
for each aa in circles
for each bb in circles
if (aa != bb) if (areTwoCirclesColliding(aa,bb)) .. break, found collision
{Aside - For the record it is utterly unimportant that you check each pair twice in that pseudocode, no problem.}
It is impossible for me to write the actual code for you as i have no idea what structure you are using, sorry.
Of course if your circle is an object, you could sensibly make it test itself against all the other circles, same idea. If you have an SQL database of circles, test them all against each other.
So fortunately you can see it is one (1) line of code to check if two circles are colliding. And it's about 3 or 4 lines of code to check all your circles for collisions. So fortunately about 5 lines in total!
So, that is an incredibly simple tutorial on video game physics, part 1.1.1.1 !!!! Hope it helps! If that is not what you were trying to achieve, it was a complete waste of typing! :)
For the record here's a routine to get the distance between two CGPoints:
static inline float rawDistance(float x, float y, float p, float q)
{
return sqrt( ((x-p)*(x-p)) + ((y-q)*(y-q)) );
}
static inline float distanceBetweenTwoCGPoints( CGPoint a, CGPoint b )
{
return rawDistance( a.x, a.y, b.x, b.y );
}
(Note... if you are new to Objective C, note that the above is totally valid code. Simply paste it in at the top of your file. OK? It's exactly like using any everyday function supplied by Apple such as x=CGLayerGetContext(), for example. Enjoy!)
Later .. and by popular demand, for an object, Circle...
-(bool)isTouchingOtherCircle:(circle)c
{
return areTwoCirclesColliding(self.center, c.center);
}
-(bool)isTouchingAnyOtherCircle
{
for oc in yourCircles
if (oc != self)
if ( [self isTouchingOtherCircle:oc] )
return false;
return true;
}