How do you make an object in Scratch have horizontal momentum when you release dragging it? - mit-scratch

I'm trying to implement this in Physica, my Scratch-based physics engine. I tried adding it and the ball glitched out (couldn't move on the x axis at all). Does anyone know how to do this?

If it's a physics engine, then every object should have added variables for speed, and then you manipulate the speed:
To change the speed:
When ... (whenever your drag release is)
set Vx to 10
this example has a drag function:
https://en.scratch-wiki.info/wiki/Draggable_Sprite_Feature
In the physics engine, speed constantly moves your object:
When green flag
forever
add Vx to x
add Vy to y

#boisvert pretty much answered your question. But if you want to sense drag releasing, use this code:
when green flag
set drag mode to draggable
forever
wait until touching mouse pointer and mouse down
wait until not mouse down
point toward mouse pointer
set V to distance to mouse pointer * 2
along with this:
when green flag
forever
set V to V*0.9
move V steps
if on edge, bounce
It's not the most perfect code, but it will work
Example: https://scratch.mit.edu/projects/558479500/

Related

How to detect exact location of mouse click on sprite in scratch

I would like to detect the exact location of the mouse click within the 3x3 grid displayed on the screen. How can this be done in MIT scratch? Any suggestions?
There are two ways to do this.
You could create 9 sprites, hide, and use the When this sprite clicked event...
...but it would be a whole lot of unnecessary sprites.
Or you can do the following:
As #PullJosh said, you can use the mouse x and y blocks. Just do some math:
You know that the stage goes from X: -240 to 240, Y: -180 to 180.
Just put that into some code to detect ranges, below is a link to a project that is an example of this:
This project
(Note: This assumes the grid boxes are the same size.)
when greenFlag pressed
forever
if <mouse down?> then
set (lastMouseClickX) to (mouse x)
set (lastMouseClickY) to (mouse y)
end
wait until <not<mouse down?>>
end```
This is pretty simple if you want to involve 2 variables in this:
All you really need to do is to set the mousex position to a variable and the mousey position to a variable after the sprite detects a click on it. Here is an example:

SpriteKit move node with physics rather than updating position

My main character moves by touching and holding him and then moving your finger left or right to move the character. To do this I just simply update the node's x position (walks left and right on a flat surface) in touchesMoved() with the x position of the touch location, and apply an animation depending on the direction he's moving.
I want to kind of take this to the next level and accomplish the same effect, but using physics, so that when I'm done moving him and release my finger, he may slide a little bit in the direction he was moving given the speed I was moving him at, if that makes sense. Does anyone know how I can accomplish this effect?
Would I have to do as I'm currently doing and update the position as it moves, but also apply a force/impulse at the same time? Kind of confused on how to approach this
Moving the physics body via force, impulse, or velocity will automatically update the player position.
So you will have to play around with the correct way to accomplish your goal. But based on this, what I would suggest is replace your .position code with .physicsBody!.velocity code in your touchesMoved. Then, on your touchesEnded, you can apply a bit of an impulse to give the player that little bit of an "on ice" effect to keep them going a tad.
You will need to experiment with how much velocity you want to get the character to move at the correct speed, and then you will need to play with the impulse figures as well to get it just right.
This could get a bit tricky though, in touchesMoved... because at some point you will want to reset the velocity to 0 (so they stop moving when your finger stops).
To do that, you will need to to use the .previousLocation from your touch object, and compare the distance of X moved. if the distance X moved is >0 (or some deadzone threshold) then apply the velocity; if the deltaX is 0, then set the velocity to 0.
This may actually be more complicated than just using .position to move the character, then having the character slide a bit with physics on touchesEnded.
You will have to play with it to get it right.

How to make object fall in line with the target when applying wind effect?

I am making a paper toss kind of game in Unity3d. I am implementing wind effect using constant force. I wanted to know how to make object fall in line with the target ie, if the object gets over the target, it should go in or fall in line with the target, not go behind or in front of the target. At present when I swipe applying a constant force, for different angles of swipe the distance moved by the object differs. Help would be much appreciated.
In FixedUpdate, use Physics.Raycast to check to see if the object is over the target. If so, set the x and z values of rigidbody.velocity to be zero (assuming y is the up/down axis in your game world) and disable the ConstantForce component (i.e. gameObject.GetComponent<ConstantForce>().enabled = false). Note that this won't be most realistic of movements, as it will seem like the object suddenly moves straight down when it goes over the target -- but it sounds like that's what you want.

Mouse position not consistent with HTML canvas ondrag

I am trying to drag some shapes in HTML canvas but I am encountering a problem with respect to determining the change in mouse coordinates [dx,dy]
First of all, there is no problem in the coordinates themselves, stored in mousePos as the rollover effects work flawlessly. What I am doing is, upon first entering the shape, saving the mouse coordinates.
pos = {x : mousePos[0] , y : mousePos[1]};
Then, onMotion updates the coordinates everytime the mouse moves as well as recording the current position
dx=mousePos[0]-pos.x;
dy=mousePos[1]-pos.y;
pos = {x : mousePos[0] , y : mousePos[1]};
Then I add the dx and dy values to the shapes coordinates (lets take a simple rectangle as an example)
ctx.fillRect(0+this.dx,0+this.dy,100+this.dx,100+this.dy);
as long as the mouse doesn't move too fast, it works relatively well (not perfect though). If I move the mouse very quickly, without going out of the window, the rectangle does not catch up with the mouse. I can understand if there is a delay catching up to the mouse, but how can the delta values be off? Clearly we know where we started, and even if dozens/hundreds of pixels are skipped in the process, eventually the mouse should stop and the correct delta values should be calculated.
Any help would be greatly appreciated as I have hit a conceptual wall here.
You might try to get e.layerX-Y when the onMotion is fired to get the real position instead of the delta. This way it can't be "off".
To use this, place your shape into a div with style="padding:0px;margin=0px;" , because the position is relative to the parent block.

SneakyJoystick question in Cocos2d

I currently have a SneakyJoystick up and running. It works fine, it moves the sprite around the screen. I already have it so it will flip the sprite's image when the joysticks degrees is to the left. But how do i make it so if it was moving left and then becomes inactive, the sprite won't automatically flip back? This is really confusing to me. Any help is appreciated. Thanks.
You must have a scheduled selector function in your program that checks the movement of your joystick after every second (or whatever the interval). I mean the code where you are checking if the joystick is towards left (joystick.velociy). So this selector will be called continuously, no matter your joystick is active or not. So when your joystick moves to left, you can flip the sprite and you can set define a boolean flag "isFlipped=true". And in the same selector method that you can check if joystick is not moving and "isFlipped=true" then you can flip back your sprite and set the flag false.
Generally speaking, it is advised to multiple the velocity by an arbitary amount and the delta value passed in to the update routine so that things move more smoothly. That will ensure that the final movement of the player is OK. I have seen people use a value between 50 and 200 for average movement.
eg,
CGPoint velocity = ccpMult(moveStick.velocity, 200 * delta);