GameMaker studio 2. Player Knockback - collision

trying to create a player damage indication when a collision with an enemy occurs.
I used this code within a collision event of the player object:
direction = point_direction(other.x,other.y,x,y);
hsp = lengthdir_x(6,direction);
vsp = lengthdir_y(4,direction)-2;
if (sign(hsp) !=0) image_xscale = sign(hsp);
However, the player object is simply pushed upward vertically rather than backwards in a parabola.
Any, ideas on how to implement a basic knockback system?

If you want a parabola, you can add an upward force afterward, like so:
direction = point_direction(other.x, other.y, x , y);
speed = 6
motion_add(90, 3)
If you don't and you'd rather a more "repeatable" parabola that always look the same, maybe you should use another method, something like
if other.x>x {hdirection=1}else{hdirection=-1}
hspeed = hdirection*6
vspeed = -2
I believe this would work better for what you're trying to achieve, unless you want to implement knockback variable depending on the angle of collision.

So I would need to see all the rest of your player physics to be sure, but I can tell you right now that direction = point_direction(other.x,other.y,x,y); is probable not what you mean, and same goes for lengthdir(). The exact origins of the colliding objects at the moment of collision have a huge effect on what that direction is, which can cause a lot of screwiness. For example: if the line is perfectly horizontal (because other.y == y) then lengthdir_y() will always be equal to zero for any input value no matter how huge.
But more importantly direction is a built-in variable of GameMaker, so using it with a custom physics system can also cause some screwiness. Fox's answer might help if you are using built-ins, but based on the fact that you have an "hsp" and "vsp" instead of hspeed and vspeed, my guess is you want to avoid built-ins.
If you just want to get the horizontal direction of the collision, you should use sign(x - other.x). Then, instead of using lengthdir(), you can just use a constant. Here it is all together:
var dir = sign(x - other.x)
hsp = 6*dir; //I'm assuming that 6 is how much horizontal you wanted
vsp = -4;
if (sign(hsp) !=0) image_xscale = sign(hsp); //This line could just go in your step event
Hope that helps! Feel free to comment if you have more questions.

Related

Why in THREE.js the arithmetic expression in Object3D.translateX isn't evaluated as expected or Vector.copy doesn't work correctly in animate loop?

I'm trying to move an object called "car" via the dat.gui. If the user changes the x value using the dat.gui slider, the car should move along the x-axis of its local coordinate system.
here I have copied the part of the code that is causing me problems:
var m = new THREE.Vector3;
m.copy(car.position);
if (changed.key=='X') car.translateX(changed.value-car.worldToLocal(m).x);
My problem is that the expression in car.translateX always evaluates to the value that is in changed.value. The part after the minus has no effect at all or maybe is permanently 0. I have printed the values ​with console.log and the values ​​of car.position.x and m change in each step, but the subtraction still delivers in every step only the result that is already in changed.value anyway. Can someone help me and tell me why this happens?
Unfortunately, I am absolutely stuck.
car.worldToLocal(m)
I'm afraid this piece of code makes no sense since car.position (and thus m) already represents the car's position in local space.
Instead of using translateX() you achieve the same result by modifying car.position directly:
car.position.x = changed.value;

Ruby SketchUp ... add a measurment

still learning about Ruby + Sketchup!
Today,I would like to add a measurement (good english word ?) as I can do manually with the 'cotation' (french version) tool when I click to point then drag the measure text.
Can't find that in the docs to do with Ruby and API ...
Thanks for your help
You are probably looking for the Sketchup::Entities::add_dimension_linear method.
http://ruby.sketchup.com/Sketchup/Entities.html#add_dimension_linear-instance_method
Assuming a and b below are edges
voffset = [-20, 0, 0]
Sketchup.active_model.entities.add_dimension_linear(a.start, b.start, voffset)
The value of voffset controls not just how far offset the dimension is, but also the axis along which the measurement is made. You may need to experiment with different values to get a feeling for how that determination is done. As with many things in SketchUp, it often guesses (or 'infers') at what you want.

THREE.js Migration r60 to r70: now cannot write: mesh.position = myVector3()

I am migrating a THREE.js app from r60 to r70. Amongst other changes I notice that r60 constructs of the following form no longer work in r70.
mesh.position.set(0,0,0);
myVector3 = new THREE.Vector3 (100,200,300);
mesh.position = myVector3;
This applies to meshes, pointLights, presumably to all Object3D's but I havent tested further.
In the above example the mesh.position x,y,z values remain unchanged at (0,0,0). For illustration see this JSFiddle and compare lines 70 and 73.
//...The next line DOES NOT update Sphere_mesh.position.x
Sphere_mesh.position = NewPos_vector3;//...
//...The next line DOES update Sphere_mesh.position.x
Sphere_mesh.position.x = NewPos_vector3.x
In a debugger no console warning is given during execution that the assignment has not worked. In the very brief THREE.js migration notes for (r68 --> r69) I see something about an Object3D's position no longer being immutable but I don't know what that means.
Anyway, my question
Is there a standard THREE construct or function I can use to copy x,y,z values from a Vector3 object to the x,y,z values of a mesh.position rather than the effective, but verbose, form such as
mesh.position.x = myVector3.x;
mesh.position.y = myVector3.y;
mesh.position.z = myVector3.z; ?
e.g. something such as
mesh.position = F_Get_Object3DPosition_from_Vector3(myVector3); ?
I know it would be easy to write my own function but a standard THREE function would be more likely to evolve smoothly with future versions of THREE.
position beeing immutable means that the position property can not be changed.
so
mesh.position = anything;
won't work (but you already discovered this)
what you can do is not change the position, but you have to change position values.
in your case, the easiest way is
mesh.position.copy (myVector3);
I think you meant myVector3 not myVector3() in the last line... Anyway, I though that would work too but the thing is, you are applying a Vector to something that supposed to be a point/Vertex. Even if that worked in my opinion it wasn't the right way to do it. How about using a simple array:
mesh.position.set(0,0,0);
new_position = [100,200,300]
mesh.position.fromArray(new_position,0)
in which 0 is the starting index. So you can have multiple position sets in one array

SCNPhysicsHingeJoint not rotating if shape is complex

I'd found a strange behaviour in joint rotation of collada object in SceneKit.
Imagine, I use this code for make rotating desk (complex form desk made in cheeta3d):
SCNScene *heroScene = [SCNScene sceneNamed:#"scene.dae"];
b = [heroScene.rootNode childNodeWithName:#"desk" recursively:YES];
b.physicsBody = [SCNPhysicsBody dynamicBody];
b.physicsBody.physicsShape=[SCNPhysicsShape shapeWithNode:b options:#{SCNPhysicsShapeTypeKey:SCNPhysicsShapeTypeConcavePolyhedron,SCNPhysicsShapeKeepAsCompoundKey:#YES}]; //**HERE we need YES only, if NO it is not rotating**!
[scene.rootNode addChildNode:b];
SCNPhysicsHingeJoint *joint = [SCNPhysicsHingeJoint jointWithBody:b.physicsBody axis:SCNVector3Make(0,1,0) anchor:SCNVector3Make(0,5,0)];
[scene.physicsWorld addBehavior:joint];
Look at comment //HERE we need YES only, if NO it is not rotating!
I tested all cases, all joints. SCNPhysicsShapeKeepAsCompoundKey must be YES although I think it is not so important here.
Why is it?
SCNPhysicsShapeKeepAsCompoundKey defaults to YES. From what I can tell that turns off the " flattenedCone " version of the physics object. Most likely you have something in your model that is keeping it from rotating when the hinge joint attaches to only one section of the model instead of the whole thing.

Find crossing lines in drawing engine

i am working on a little multimedia - project in which the user, amongst other things, can draw shapes on a canvas (2d) by connecting points with a line.
but i need to prohibit the user from crossing lines.
what i am looking for would be a small algorithm that can find intersecting lines.
the project is done in AS3/Flash but i guess the answer would be universal.
anyone can give me a clue?
thanks
Here is an example in Java but I think you can easily adapt to AS3 :
public static boolean intersects(double ax, double ay, double bx, double by,
double cx, double cy, double dx, double dy) {
double denum = ((bx-ax)*(dy-cy)-(by-ay)*(dx-cx));
if (denum == 0) return false; // parallel segments
double r = ((ay-cy)*(dx-cx)-(ax-cx)*(dy-cy)) / denum;
double s =
((ay-cy)*(bx-ax)-(ax-cx)*(by-ay)) / denum;
return 0<=r && r<=1 && 0<=s && s<=1;
}
It should return true if the segments [AB] and [CD] intersect.
You can find reference here
You can use the methods Point, hitTest, hitTestObject or hitTestPoint. I don't know if Super Chafouin's answer is helpful, but I believe that the code looks quite different in AS3 though.
To explain what they mean, I'll quote Aaron Beall from actionscript.org, giving a nice explanation:
hitTestObject checks the bounding box rectangle of two display
objects. It always checks rectangle-vs-rectangle.
hitTestPoint checks a point (x,y) against a display object:
- With shapeFlag true it checks point-vs-shape
- With shapeFlag false it checks point-vs-rectangle
BitmapData/hitTest checks a bitmap against a point, rectangle, or
other bitmapdata. Using a check of bitmapdata-vs-bitmapdata means you
can perform alpha channel based shape-vs-shape checks. See here:
http://www.mikechambers.com/blog/200...ion-detection/
(Also, not to nitpick but just to make sure you understand, neither
hitTestObject or hitTestPoint are "triggered" -- you must manually
call them when you want to check something.)
Asking for a whole algorithm is quite something, it depends on your app too.
See for more examples and documentation here, here
I hope this will help!

Resources