Game Development Community

Example Request Thread (if you need an example- request it! :)

by Matthew Langley · in Torque Game Builder · 04/14/2005 (11:40 am) · 31 replies

Ok... well here is a thread anyone can post a function or even a concept and request an example or a way to apply it... this way we can all share what we've already done and help eachother learn more :)

About the author

Was a GG Associate and then joined GG in 2005. Lead tool dev for T2D and T3D. In 2011 joined mobile company ngmoco/DeNA and spent about 4 years working game and server tech. 2014 joined startup Merigo Games developing server technology.

Page «Previous 1 2
#1
04/14/2005 (11:51 am)
How about a function that takes an object (any sprite), captures the X/Y coords of the mouse, and returns the distance to defined object, the angle in degrees between the two, and a polar velocity difference as global variables? Function would also need to be running on a constant basis, probably needing to update on a per frame basis.
#2
04/14/2005 (11:56 am)
Are you thinking some sort of GUI that would show this info consantly ?

right now I have functions to return the distance between, angle between, and the velocity difference would be easy to do
#3
04/14/2005 (12:30 pm)
GUI display wouldn't be necessary, just have them stored in variables that could be referenced would be the biggest help for me...

I'll be needing this info to control the rotation of mounted turrets on my player sprite. So they can track the mouse, and then use the angle and velocity values for aiming.
#4
04/14/2005 (12:42 pm)
The best thing to do is get these values when you need them, vs always getting them even when your not using them... so you can have a schedule for the aiming, or put it in scene update
#5
04/14/2005 (1:54 pm)
Heh..you changed your name.
#6
04/14/2005 (1:55 pm)
Lol yeah... too bad King Tut(orial) wouldn't fit =/
#7
04/14/2005 (7:20 pm)
I'll work up an example for you tonight Barry :)
#8
04/14/2005 (11:21 pm)
I haven't thought about how to get the difference in polar velocities between two objects (i think i have it in my head, but my brain can't comprehend the math calculations right now :-p), but I have some code I've been working for my own purposes on that'll do most of what you need. This code gets the player position and the mouse cursor position.

function fxSceneGraph2D::onUpdateScene()
{
	%playerRot = $player.getRotation();
	%playerPos = $player.getPosition();
	%playerX = getWord(%playerPos, 0);
	%playerY = getWord(%playerPos, 1);

	// find out the current camera extents, and take zooms into account
	%extentX = getWord(sceneWindow2D.getCurrentCameraArea(), 2);
	%extentY = getWord(sceneWindow2D.getCurrentCameraArea(), 3);
	%zoom = sceneWindow2D.getCurrentCameraZoom();

	// get mouse coords. Returns in pixels. Next bit converts to t2d units
	%mouse = canvas.getCursorPos();

	// convert mouse coords to t2d world units
	// subtract half of extent from result due to t2d's default setup (-50 to 50, eg)
	%mouseX =(getWord(%mouse, 0) / $resX * %extentX / %zoom) - (%extentX / 2 / %zoom);
	%mouseY =(getWord(%mouse, 1) / $resY * %extentY / %zoom) - (%extentY / 2 / %zoom);

	// Calculate target angle.
	%tAngle = mRadToDeg(mAtan(%mouseX - %playerX, %playerY - %mouseY));

	// calculate difference between angles
	$dAngle = -(%tAngle - %playerRot);
	if($dAngle >= 180)
		$dAngle -= 360;
	if($dAngle <= -180)
		$dAngle += 360;

// get vector length
	$dDist = vectorDistance2D(%playerPos, %mouseX SPC %mouseY);   // returns a vector
	$dLen = vectorLength2D(%dDist);   // returns a length
}

Now, I'm sure there must be cleaner ways of doing some of the stuff in there, but this works for my purposes.

There's a bit of an oversight in my code, by the way, in that I'm not checking for a difference in angle of >180 degrees. I'll add that in tomorrow, since I only just found out the oversight. :-p

edit: added the code to check for angles over 180 degrees (T2D will return angles from -180 < x < 180). That check may or may not be needed, depending on what your code is doing, but it's needed for my purposes, which is having the player sprite follow and track the mouse cursor.

edit2: updated the code to take zoom level into account
#9
04/15/2005 (6:15 am)
Awesome.. ty... I'll try plugging that in tomorrow and see if I can get it to work.

Special thanks on the code help guys... getting some good working examples can really help with getting a handle on the language.
#10
04/15/2005 (6:39 am)
No problem, since giving working examples also helps myself. :) I've found that a good way to learn is to find an interesting request, try to figure it out, and post it up. It's almost like a little race, since I want to post a working solution with enough time to feel good about it before someone like King Bob comes along and posts something really nice. :-p Also, it's a nice feeling to be able to give back to the community. :)
#11
04/15/2005 (6:23 pm)
Actually, the above code gets a little wonky with different zoom levels, as I just found out. I'm not entirely sure why, I'm getting weird mouse-follow results. I'll see if I can nail down what I screwed up.
#12
04/15/2005 (6:31 pm)
Yes you beat me :0... nice set of code though :) I can definately understand, its nice to contribute back to the community when its one as good as this one...

it seems T2D will take either -180 to 180 or 0 360 degrees in its rotations :)... so in my pathfinding I dont do any converting from the formula to get the angle between and seems to work fine... at least for what I've tested... following the mouse getting degree to face an other object, etc
#13
04/15/2005 (6:41 pm)
Quote:nice set of code though :)
Thanks! That means a lot, 'specially coming from the King. :-p

Yup, my first round of code used setRotation to point the object at the mouse, but never being satisfied with "it works!", I had to go off and complicate things for myself. :)

The code I'm using to point the player at the cursor uses setAngularVelocity(%dAngle * multiplier), which basically gives me some lag behind the cursor, and a freebie interpolation built in. The only problem comes about when crossing 180 degrees, since the player then turns the wrong way:
www.lotekk.com/files/angles.jpgHence the conversion to always return <180 degrees. :)

Using the vectorlength also allowed me to do the same with setLinearVelocityPolar, so I get another freebie interpolation. :)
#14
04/15/2005 (6:46 pm)
Lol not sure if it really does mean much coming from me, but very well thought out... Design often saves 80% of the coding lol...

ahh, that makes sense, though you can set it either way it will still just return rotations at -180 180... so a simple conversion to keep things in that format (or you could keep track of rotation your own way, though wouldn't be worth the trouble), good thinking

hmm, so this gets screwy at different zoom levels ?
#15
04/15/2005 (6:53 pm)
Yeah, I'm not sure what's causing it, though I'll probably try and take a look at it a little later tonight. From what I could tell, both the vectors (direction and length), and rotation to a lesser degree, get pretty screwy. I'm not sure if the screwup occurs at the cursor, or the player, or simply how I'm converting the world units.
#16
04/16/2005 (2:43 pm)
Bingo. I had made the erroneous assumption (and assumption, of course, is the mother of all foul-ups) that Zoom affected CameraArea, so my calculations were getting borked. I've updated my original code with the changes.
#17
04/16/2005 (3:05 pm)
Ahh... glad you figured it out :) That makes sense
#18
04/16/2005 (7:30 pm)
If you wouldn't mind, could you give me a few ideas on how to do optimized z-ordering in an isometric game? My current plan is to get the list of units currently colliding in an area and then sort them (right now, via bubble sort) by y-value, and then run through the new list setting all of them to the back of the layer using setLayerDrawOrder... but after writing it up, it didn't work, and before I go into extensive debug mode, I thought I'd ask if you had any ideas.

(note: I only have one layer set aside for units as of now, so it would be best if this could be done without changing layers. I thought of some ideas to set units onto a small range of layers... but the algorithms I thought of were way too complex.)
#19
04/17/2005 (12:40 am)
I've been thinking of the same thing, though haven't gotten to the point of doing the implementation for my teams game yet, we are making a Diablo style RPG... so this sort of thing will be needed... I was thinking of setting up things in an ISO grid... all logical, then when things pass these grid points they move up a layer... or an order point, etc... still have lots to work on this though... not sure if that helped any lol
#20
04/17/2005 (8:50 am)
No, not really, heh.
Page «Previous 1 2