Game Development Community

best way to determine on what side the player was hit on?

by rennie moffat · in Torque Game Builder · 03/24/2010 (10:58 am) · 21 replies

What would be the best way to determine on what side the player was hit on?








So if he is hit on the left side, I assure he bounces to the left?



*** edit. I am thinking it has something to do with contact points?

About the author

My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.

Page «Previous 1 2
#1
03/24/2010 (11:04 am)
You can determine what side of an object has received a collision with %normal parameter from onCollision. This picture might help with how a normal is defined:

tdn.garagegames.com/wiki/images/1/18/TGB_Platformer_Tutorial_Player_7.jpg
#2
03/24/2010 (11:17 am)
ok, so i can set up xy coordinates for contact points?


I am a little rusty on normals. I was looking at t2dVectorNormal the other day. I know a normal would be a perpendicular line, so i would imagine that if collided at this point, however, this is what I need to know... how do you best define a collision point.

looking in docs, but a quick example would be nice.

%contacts  = ?

#3
03/24/2010 (11:35 am)
Could someone help explain to me what is happening here?

function playerClass::updateVertical(%this)
{
   %yVelocity = %this.getLinearVelocityY();
   
   %this.setLinearVelocityY(5);
   %collision = %this.castCollision(0.005);
   
   %normalX = getWord(%collision, 4);
   %normalY = getWord(%collision, 5);


regarding collisionCasts.
The programmer has %collision = %this.castCollision(0.005);

then, normalX = getWord(%collision, 4);
this means normalX is the perpendicular vector to contact point 4 which would be set in the collision editor as point4?






then here he uses if, normalX == 1 && normal Y == 0. This is confusing to me as why would he want 2 vectors. In My case I am dealing with simple one way collisions. my player may be hit on 1 of 4 sides or 1 of 4 corners at any one time. If that is the case, my player is hit/collides, he will only have to bounce back in 1 of 8 ways, each however is only one direction. Why does he need to calculate 2 vectors?


// collides with wall to the left
   if (%normalX == 1 && %normalY == 0)
   {
      %this.againstLeftWall = true;
      %this.setLinearVelocityX(0);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }





To round it up, I seem to be unsure of why, how, the normal vectors are called.

The one thing I would like to better understand, and would think is possible, is can i use the points, and lines in collision editors, (square has collision points 1, 2, 3, 4) plus each side (1-2, 2, 3, 3-4, 4-1). So I need to know how to tell the computer to tell the player to recognize when it has been hit, and at which point or side. I am currently unsure how to do that. Any help would be appreciated.


Thanks,
Ren
#4
03/24/2010 (1:42 pm)
I guess I only have myself to blame for this mess. ;) Let's start...

Quote:regarding collisionCasts.
The programmer has %collision = %this.castCollision(0.005);

then, normalX = getWord(%collision, 4);
this means normalX is the perpendicular vector to contact point 4 which would be set in the collision editor as point4?

No, if a collision takes place castCollision returns a bunch of information about the collision. Best to look up castCollision in the reference (under methods for t2dSceneObject). %collision contains a list of information, as we are only interested in the normal we use getWord to select the X and Y component of the normal's direction (the 5th and 6th value in the list, remembering that we always start with 0 so 4 is the 5th value and 5 is the 6th). The point numbers in the collision editor have nothing to do with the normal and are in no way related.

Quote:then here he uses if, normalX == 1 && normal Y == 0. This is confusing to me as why would he want 2 vectors. In My case I am dealing with simple one way collisions. my player may be hit on 1 of 4 sides or 1 of 4 corners at any one time. If that is the case, my player is hit/collides, he will only have to bounce back in 1 of 8 ways, each however is only one direction. Why does he need to calculate 2 vectors?

I'm not pulling two vectors. normalX and normalY are the X and Y point values that define the direction of the normal (which is one vector).
#5
03/25/2010 (9:25 am)
I looked up castCollision reference and found nothing, I know someone will prove me wrong, but I swear to god I found nothing beyond the platformer tut. Any links appreciated.

Quote:
%collision contains a list of information, as we are only interested in the normal we use getWord to select the X and Y component of the normal's direction. The point numbers in the collision editor have nothing to do with the normal and are in no way related.

well, since %collision = %this.castCollision, it is I had had gathered previously a list of sorts. You said it returns a bunch of info... but how can that be if as declared here, castCollision(0.005);?




Quote:
I'm not pulling two vectors. normalX and normalY are the X and Y point values that define the direction of the normal (which is one vector).

I got you here, but, if I have one x value and one y value how am I getting a direction out of this? Would this not be one point? I am confused as to how the vector is being created/defined.






#6
03/27/2010 (4:03 pm)
hi I just realised a few things, one I am a terrible skim reader I missed castCollision in the collision methods. so I only found out today what it does and is.


I am having a problem tho. I want my player to bounce any way he is hit, a bounce back if you will. However I am unable to get it to work currently. I have tried two ways, to use castCollision in an onUpdate and also, separately in a collision callback. Neither version I have gotten to work. I hope someone can tell me if and or what I am doing wrong.


Thanks.


function playerClassBehavior::updateCollision(%this)
{	
	%collision = %this.owner.castCollsion(0.005);
	%normalX = getWord(%collision, 4);
	%normalY = getWord(%collision, 5);
	

///all impulse powers are the same direction, that is currently irrelevant. 
///What I need is for it to work. 
	if(%normalX == 1 && %normalY == 1)
	%this.owner.setImpulseForce(%this.xImpulse, %this.yImpulse, false);
	
	if(%normalX == -1 && %normalY == -1)
	%this.owner.setImpulseForce(%this.xImpulse, %this.yImpulse, false);
	
	if(%normalX == 1 &&  %normalY == -1)
	%this.owner.setImpulseForce(%this.xImpulse, %this.yImpulse, false);
	
	if(%normalX == -1 && %normalY == 1)
	%this.owner.setImpulseForce(%this.xImpulse, %this.yImpulse, false);
	
	if(%normalX == 1 && %normalY == 0)
	%this.owner.setImpulseForce(%this.xImpulse, %this.yImpulse, false);
	
	if(%normalX == -1 && %normalY == 0)
	%this.owner.setImpulseForce(%this.xImpulse, %this.yImpulse, false);
	
	if(%normalX == 0 && %normalY == 1)
	%this.owner.setImpulseForce(%this.xImpulse, %this.yImpulse, false);
	
	if(%normalX == 0 && %normalY == -1)
	%this.owner.setImpulseForce(%this.xImpulse, %this.yImpulse, false);
}

/// the other method I tried but failed was in onCollision
if(%dstObject.class $= "enemyClass")
	{
		if(%normalX == 1 && %normalY == 1)
		%srcObject.bumpedDL();
		
		if(%normalX == 1 && %normalY == -1)
		%srcObject.bumpedUL();
		
		if(%normalX == -1 && %normalY == -1)
		%srcObject.bumpedUR();
		
		if(%normalX == -1 && %normalY == 1)
		%srcObject.bumpedDR();
	}

and the call
function playerClassBehavior::bumpedDL(%this)
{
	%this.owner.setAtRest();
	%this.owner.setImpulseForce(%this.xImpulse * -1, %this.yImpulse, false);
}










if anyone can help me figure this out I would very much appreciate it.

Thanks
Ren
#7
03/30/2010 (2:42 am)
Quote:I got you here, but, if I have one x value and one y value how am I getting a direction out of this? Would this not be one point? I am confused as to how the vector is being created/defined.

So you never did vectors in school? Trying looking them up on the Internet, that should clarify it for you. I know you like posting code (alot) but you need to grasp fundamentals first otherwise it seems as if you're asking for people to modify your code posts constantly.

You're using linear velocity but you don't understand vectors?

Sorry if you do understand vectors, if that's the case then I would suggest that you more carefully word your questions. I would also suggest that you post less code not more and more. Code can confuse more than it helps in some cases, especially in alot of your posts. I am not blaming you for not understand this stuff (honestly) I just don't understand how anyone can not use the Internet to get a better understanding of basic problems.

Break the problem down into fundamentals and ask that, don't post a bunch of code and say "what's wrong with this code and can you help me with it". I would suggested trying a different approach.

Honesly, I AM trying to help you but perhaps not in the way you expected! ;)
#8
03/30/2010 (3:08 am)
What you're asking is about how to reflect a vector. Here's one of many, many examples provided by a quick Google search.

Reflecting a vector/

If you don't understand basic vector math and stuff like dot-product then I would suggest you look it up, it's pretty simple and you really can't avoid this stuff when programming nor should you try.

Also, don't try to create code that checks various combination after combination as your code will get really complex really quick. Also, there is no reason why the components of the normals will only be either "-1", "0" or "+1", they could be any floating-point value from "-1" to "+1" as you'd know if you looked-up normalized vectors.

In other words, your solution is to use simple vector math and not try to create a whole bunch of conditional code checking for specific conditions.
#9
03/30/2010 (6:37 am)
I do understand vectors fine, it is just how to relate this into code so that when i player is hit he bounces in the appropriate direction. I will be looking into this more this week but as of right now, I am a little sketchy May look at more today. If I get stuck I will give an appropriate reply.



Thanks.


#10
03/31/2010 (12:50 am)
Quote:I do understand vectors fine
Fair enough but as I mentioned you then ask...
Quote:I got you here, but, if I have one x value and one y value how am I getting a direction out of this? Would this not be one point? I am confused as to how the vector is being created/defined.
... which indicates you don't understand how an X and Y can be a "vector", you think this is only a "point" which is why I spent time finding some vector stuff for you.

If you know "10 20" is a point then you're saying that you don't understand how "10 20" is a vector but you also understand vectors?

It's VERY hard to help someone if they don't CLEARLY indicate what they do understand and don't understand.
#11
03/31/2010 (4:15 am)
Well I can understand how any point can be a vector, in that it runs off from "0 0" but in collisions, coming from any angle I can not see how this is useful. I am not saying it isn't just that I need to better understand the mechanics of it all.
#12
03/31/2010 (4:21 am)
Type "2D collision normal" (or something similar) into Google and you'll have plenty of information that explain the mechanics.
#13
03/31/2010 (4:27 am)
Again, I guess my language is wrong but it is not what happens when you crash A into B but more how to program the engine to make sure the reaction of the 4 possible collision types (left right up down) and the correct reaction is applied. That's all. Maybe I am making more of this than need be. I will be looking into this tho as it will help my game a lot to get it right.

Thanks
#14
03/31/2010 (9:17 am)
@Melv,
I have a question,
I realize now.. call me a slow learner, but x, y contact points, produce a point, the normal x and y produce a point. The vector of the contact point and normal point will make a vector in the direction the object will need, in my case to bounce back. However in the platformer example normal x and y are == to 1, -1 or 0.



I am perplexed here as shouldn't this be a point, as in for ex. "32 48.950"?


I am going thru this this aft, but if you have a quick answer/explanation I would love to hear it.


Thanks, studying up.

#15
03/31/2010 (9:41 am)
Point: A location in space, be it two dimensional (X,Y) or three dimensional (X,Y,Z). It's just a location in a coordinate system.

Point Image

Vector: Though similarly structured, consists of magnitude and direction.

Vector Image

To an extent, it does not matter that they share the same structure. You are hung up on that.

Point = "56 32"

Vector = "0 1"

Image Resolution = "512 512"

MyOwnStructure = "32 Mich"

Stop coding, right now, and go read. Memorize this mantra, practice it, and thank me later:

Read. Read Code. Code

#16
03/31/2010 (9:41 am)
Perhaps you are getting collision "point" mixed up with collision "normal"? One states the position of the contact, the other the direction.
#17
03/31/2010 (9:51 am)
Yes I am...
I have done some beta tests,
I have placed in echos calling for time, contacts, normals and object.


I have a question regarding time tho. Time in this case is defined as..
The number of seconds into the future to check for collisions. If this number is 0.0, the collision will be done based on the current position of all objects. If it is greater than 0.0, the engine will perform the collision detection across a simulated time period of length %time and return the first collision detected.

all of my checks for time, were negative. so this means what exactly? I am just unsure of how to best use this feature to my advantage.

#18
03/31/2010 (9:53 am)
This is another habbit of yours ... changing subject. I would suggest dealing with one thing at a time and respond to what people post, not just move onto something else.
#19
03/31/2010 (8:20 pm)
Don't worry about it. I thought I answered it clearly.



Sorry for all the hub bub bub. but I figured out how to cast, a collision, angles normals etc today. I even got it working across one class. It didn't work for two tho, tho I am sure it is a simple bug.



Looking good tho, thanks!
#20
04/01/2010 (2:56 am)
No need to be sorry and I know I was being abrupt but trying to help is a voluntary thing and quite often you want (at least) for someone to reply to what you posted else you just go away and give up helping that person.

Maybe it's just a language thing here. ;)
Page «Previous 1 2