Game Development Community

Creating a blank object for collisions

by Adam Albee · in Torque Game Builder · 05/31/2011 (6:39 am) · 5 replies

Alright, I know I saw a resource somewhere that told me how to do something like this. Well, not exactly like this, but it was close enough. Thought I bookmarked it, but apparently not, and now I can't seem to find what I need.

What I need to do is create a collision box with no image or animation in it. Essentially, I have a few globals:
$player.direction
$player.hitBoxSize
$player.hitBoxOffset
$player.hitBoxLife

What I want to have is a function I can call, lets say spawnHitBox(). I want this function to create a collision box in the direction the player is facing (which is stored as n, s, e, w, nw, sw, ne, se)at a distance equal to hitBoxOffset, of a size on each side equal to hitBoxSize, and then delete itself in the time of hitBoxLife.

I guess where I get lost is how to actually create the object in script and then how to remove it. I also need some way to give it a name or something so that other objects can check if they've collided with it.

I have a feeling this is super simple, but I can't seem to make my mind do the work here.

Any help?

Thanks,
Adam Albee

#1
05/31/2011 (11:40 am)
Yes you can do this. The way I did it was:
(this is high level and I'm not at my puter so I cant give you specifics but I'll outline it for you here)

A) create object you want.
1) create the object in the gui you want. then you can save the level.
2) you can then go to your level and see the object in your level file.
3) copy the object from your level file to your datablocks.cs (might be called something different) which allows you to access this from any level. 4) then you set that whole datablock to a name like:

$hitbox = datablock(new block)
{name = bleh
width = bleh
}

now you can access that $hitbox variable and attach it to your player.

B. setting up collision
Now your gonna have to handle the ::oncollision method for that new hitbox you attached so it only collides with objects you want it to.. otherwise it will collide with yourself maybe, or the ground or whever. so you put a switch case in there to handle the objects you want.

Feel free to reply to this thread with more questions.. I just wanted you to get some reply so that you know people are listening :D. I'll try and remember to give you more details from my game code later.
#2
05/31/2011 (12:01 pm)
Wow... I did finally get something sort of working in code (like 5 minutes ago actually, was coming back to say so). I have to say though, the idea of creating the object through the editor in a separate level and then copying the code over is pure genius and would have saved me boat loads of time! I'll have to make a note of that idea for the future. Also, thanks for the collision tips, I think that is just what I needed.

-Adam
#3
06/02/2011 (11:24 pm)
Rather than creating and deleting objects, why not just have a named sceneObject set up in the editor that you use for collisions. Configure its collision shape and settings in the editor. Then in your script you just need to put it in the right place at the right time, and call %this.setCollisionSuppress(false) to turn it on, and true to turn it off.
#4
06/03/2011 (5:09 pm)
That is a pretty good idea too, but with having multiple levels and possibly having multiple "hitboxes", creating them seems to be the better option for me in this specific case (of course, I wouldn't be surprised if Torque could "clone" these objects anyway, heh). Plus, now that I've figured it out, its just one more coding trick that I have in my toolbox :)

-Adam
#5
06/04/2011 (7:43 am)
Yes you can call %this.clone() to copy an object.